分布式电商项目(02)--后台管理系统SSM框架整合
前言:上一篇博客讲了此次分布式电商项目后台管理系统的工程的搭建,这一篇就讲一下ssm框架的整合
1.整合思路
下面说到的配置文件都需要放到manager-web工程下,因为此工程为war工程,而其它的工程都只是一个jar包,具体如下图所示
1.1 dao层
mybatis整合spring,通过spring管理sqlsessionfactory、mapper代理对象。需要mybatis和spring的整合包。
整合内容 |
对应工程 |
pojo |
mangaer-pojo |
mapper映射文件 |
mangaer-mapper |
mapper接口 |
mangaer-mapper |
sqlmapconfig.xml |
manager-web |
applicationcontext-dao.xml |
manager-web |
1.2 service层
所有的实现类都放到spring容器中管理。由spring创建数据库连接池,并由spring管理事务。
整合内容 |
对应工程 |
service接口及实现类 |
mangaer-service |
applicationcontext-service.xml |
manager-web |
applicationcontext-trans.xml |
manager-web |
1.3表现层
springmvc整合spring框架,由springmvc管理controller。
整合内容 |
对应工程 |
springmvc.xml |
manager-web |
controller |
manager-web |
2. dao层整合
2.1 创建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> </configuration>
2.2 spring整合mybatis
创建applicationcontext-dao.xml文件,该文件的配置内容主要包含数据源的配置、让spring管理sqlsessionfactory、把mapper代理对象放到spring容器中,具体的配置如下:
<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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 数据库连接池 --> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:properties/*.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="com.taotao.mapper" /> </bean> </beans>
创建jdbc.properties文件,该文件是用来连接数据库的配置文件,其中的文件内容主要有,数据库的名字连接信息等!
jdbc.driver=com.mysql.jdbc.driver jdbc.url=jdbc:mysql://localhost:3306/taotao?characterencoding=utf-8 jdbc.username=user jdbc.password=user
3.service层整合
3.1 管理service实现类
创建applicationcontext-service.xml文件,管理service实现类,将其放入spring容器中,具体配置如下:
<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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <context:component-scan base-package="com.taotao.service"/> </beans>
3.2 事务管理
创建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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 事务管理器 --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <!-- 数据源 --> <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(* com.taotao.service.*.*(..))" /> </aop:config> </beans>
4.表现层整合
4.1 创建springmvc.xml文件
srping-mvc.xml 为 springmvc 核心配置文件,主要包括 controller 层的包扫描、视图模式配置(跳转路径的前后缀)、文件上传配置、静态资源处理等
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.taotao.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> </beans>
4.2 修改web.xml文件
web.xml 文件是创建 web 项目所需要的配置文件,其主要用来初始化配置信息,主要包含拦截器、过滤器、servlet 等的配置,它的位置在项目 web-inf 目录下。打开web.xml文件,添加如下内容
<!-- 加载spring容器 --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:spring/applicationcontext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- 解决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> <!-- <init-param> <param-name>forceencoding</param-name> <param-value>true</param-value> </init-param> --> </filter> <filter-mapping> <filter-name>characterencodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- springmvc的前端控制器 --> <servlet> <servlet-name>taotao-manager</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>taotao-manager</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
5. 修改pom文件
打开聚合工程下的mapper工程的pom.xml文件,并在该文件中添加一下内容
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 --> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
至此,后台管理系统的ssm框架的整合的配置文件的编写也就完成了~~~