Design and Implementation of an Online Course Purchase System(08)

SSM framework integration

Posted by Chen Xingxu on June 3, 2020

易课寄在线购课系统开发笔记(八)–SSM框架整合

数据库

数据库使用 MySQL 数据库,要求5.5以上版本。

易课寄在线购课系统的数据库详细设计请参阅下面的笔记:

易课寄在线购课系统开发笔记(三)–数据库设计

MyBatis 逆向工程

使用 MyBatis 官方提供的 mybatis-generator 生成 pojo、mapper 接口及映射文件,并且将 pojo 放到 ecourses-bms-pojo 工程中。将 mapper 接口及映射文件放到 ecourses-bms-dao 工程中。

如何使用逆向工程请参阅下面的笔记:

易课寄在线购课系统开发笔记(四)–逆向工程 Mybatis Generator代码生成

整合思路

Dao 层

SqlMapConfig.xml

MyBatis 的配置文件,不需要配置任何内容,需要有文件头,文件必须存在。

applicationContext-dao.xml

MyBatis 整合 Spring,通过由 Spring 创建数据库连接池,Spring 管理 SqlSessionFactory、mapper 代理对象。需要 MyBatis 和 Spring 的整合包。

Service 层

applicationContext-service.xml

所有的 Service 实现类都放到 Spring 容器中管理。并由 Spring 管理事务。

表现层

Spring MVC 框架,由 Spring MVC 管理 Controller。

Spring MVC 的三大组件:

  • HandlerMapping (处理器映射):负责处理 Web 请求和具体的 Controller 之间的映射关系匹配
  • Controller (处理器):DispatherServlet 的次级控制器,Web 请求的具体处理者。DispatherServlet 获得 handlerMapping 的返回结果后,调用 Controller 的处理方法处理当前的业务请求,处理完成后返回 ModelAndView 对象。
  • ViewResolver (视图解析器):用来处理视图名与具体的 View 实例之间的映射对应关系。根据 ModelAndView 中的视图名查找相应的 View 实现类,然后将查找的结果返回给 DispatcherServlet,DispatcherServlet 最终会将 ModelAndView 中的模型数据交给返回的 View 处理最终的视图渲染工作。

Dao整合

创建 SqlMapConfig.xml 配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
		PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
		"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->   
        	<property name="dialect" value="mysql"/>
		</plugin>
	</plugins>
</configuration>

Spring 整合 MyBatis

创建 applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	<!-- 数据库连接池 -->
	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:config/*.properties" />
	<!-- 数据库连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="maxActive" value="10" />
		<property name="minIdle" value="5" />
	</bean>
	<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.ecourses.mapper" />
	</bean>
</beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ecourses?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

备注:Druid 是目前最好的数据库连接池之一,在功能、性能、扩展性方面,都超过其他数据库连接池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource 等。

Druid 已经在阿里巴巴部署了超过 600 个应用,经过多年生产环境大规模部署的严苛考验。

Service整合

管理 Service

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	<!-- 配置包扫描器 -->
	<context:component-scan base-package="cn.ecourses.service"/>
	<!-- 使用dubbo发布服务 -->
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="ecourses-bms" />
	<dubbo:registry protocol="zookeeper" address="zookeeper:2181" />
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />
	<!-- 声明需要暴露的服务接口 -->
	<dubbo:service interface="cn.ecourses.service.ItemService" ref="itemServiceImpl" timeout="600000"/>
	<dubbo:service interface="cn.ecourses.service.ItemCatService" ref="itemCatServiceImpl" timeout="600000"/>
	<dubbo:service interface="cn.ecourses.service.ItemDescService" ref="itemDescServiceImpl" timeout="600000"/>
	<dubbo:service interface="cn.ecourses.service.ItemParamService" ref="itemParamServiceImpl" timeout="600000"/>
	<dubbo:service interface="cn.ecourses.service.ItemParamItemService" ref="itemParamItemServiceImpl" timeout="600000"/>
	<dubbo:service interface="cn.ecourses.service.UserService" ref="userServiceImpl" timeout="600000"/>
	<dubbo:service interface="cn.ecourses.service.AdminService" ref="adminServiceImpl" timeout="600000"/>
	<dubbo:service interface="cn.ecourses.service.LoginService" ref="loginServiceImpl" timeout="600000"/>
	<dubbo:service interface="cn.ecourses.service.TokenService" ref="tokenServiceImpl" timeout="600000"/>
	<dubbo:service interface="cn.ecourses.service.BmsOrderUserService" ref="bmsOrderUserServiceImpl" timeout="600000"/>
	<dubbo:service interface="cn.ecourses.service.BmsOrderListService" ref="bmsOrderListServiceImpl" timeout="600000"/>
</beans>

事务管理

创建 applicationContext-trans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源 -->
		<!-- Data sources are configured in applicationContext-dao.xml -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* cn.ecourses.service..*.*(..))" />
	</aop:config>
</beans>

表现层整合

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:conf/resource.properties" />
	<context:component-scan base-package="cn.ecourses.controller" />
	<mvc:annotation-driven />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<!-- 配置资源映射 -->
	<mvc:resources location="/css/" mapping="/css/**"/>
	<mvc:resources location="/js/" mapping="/js/**"/>
	<!-- 配置多媒体解析器 -->
	<!-- 定义文件上传解析器 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 设定默认编码 -->
		<property name="defaultEncoding" value="UTF-8"></property>
		<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
		<property name="maxUploadSize" value="5242880"></property>
	</bean>
	<!-- 配置拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<!-- 不进行拦截 -->
            <mvc:exclude-mapping path="/page/login"/>
            <mvc:exclude-mapping path="/user/login"/>
            <mvc:exclude-mapping path="/js/**"/>
			<bean class="cn.ecourses.bms.interceptor.LoginInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
	<!-- 引用dubbo服务 -->
	<dubbo:application name="ecourses-bms-web"/>
	<dubbo:registry protocol="zookeeper" address="zookeeper:2181"/>	
	<dubbo:reference interface="cn.ecourses.service.ItemService" id="itemService" />
	<dubbo:reference interface="cn.ecourses.service.ItemCatService" id="itemCatService" />
	<dubbo:reference interface="cn.ecourses.service.ItemDescService" id="itemDescService" />
	<dubbo:reference interface="cn.ecourses.service.ItemParamItemService" id="itemParamItemService" />
	<dubbo:reference interface="cn.ecourses.service.ItemParamService" id="itemParamService" />
	<dubbo:reference interface="cn.ecourses.service.UserService" id="userService" />
	<dubbo:reference interface="cn.ecourses.service.AdminService" id="adminService" />
	<dubbo:reference interface="cn.ecourses.service.LoginService" id="loginService" />
	<dubbo:reference interface="cn.ecourses.service.TokenService" id="tokenService" />
	<dubbo:reference interface="cn.ecourses.service.BmsOrderUserService" id="bmsOrderUserService" />
	<dubbo:reference interface="cn.ecourses.service.BmsOrderListService" id="bmsOrderListService" />
	<dubbo:reference interface="cn.ecourses.content.service.ContentCategoryService" id="contentCategoryService" />
	<dubbo:reference interface="cn.ecourses.content.service.ContentService" id="contentService" />
	<dubbo:reference interface="cn.ecourses.search.service.SearchItemService" id="searchItemService" />
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
  <display-name>ecourses-bms-web</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
	<!-- 解决post乱码 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- springmvc的前端控制器 -->
	<servlet>
		<servlet-name>ecourses-bms</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>ecourses-bms</servlet-name>
		<!-- 拦截所有请求,不包括jsp -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>