b2cTest分布式项目搭建流程:2-整合spring-springmvc-mybatis框架。
整合思路:
分层整合,由dao-service-controller.
1.mybatis框架,创建SqlMapConfig.xml
2.创建applicationContext-Dao.xml
1.配置数据源。
2.管理SqlsessionFactory.
3.把mapper类放入spring容器,扫描包加载mapper。
3.创建applicationContext-Service.xml
1.事务管理。
2.把service类放入spring容器,扫描包加载service包
4.创建springMVC.xml
1.配置注解驱动。
2.配置视图解析器。
3.扫描包controller
5.修改web.xml
1.配置spring容器。
2.配置springMVC前端控制器。
3.post乱码过滤
分布式项目工程的配置问家放在war包项目工程内。
1.新建SqlMapConfig.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 </configuration>
2.创建applicationContext-Dao.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 8 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 9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 10 11 <!-- 数据库连接池 --> 12 <!-- 加载配置文件 --> 13 <context:property-placeholder location="classpath:mysql.properties" /> 14 <!-- 数据库连接池 --> 15 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 16 destroy-method="close"> 17 <property name="url" value="${jdbc.url}" /> 18 <property name="username" value="${jdbc.username}" /> 19 <property name="password" value="${jdbc.password}" /> 20 <property name="driverClassName" value="${jdbc.driver}" /> 21 <property name="maxActive" value="10" /> 22 <property name="minIdle" value="5" /> 23 </bean> 24 <!-- 配置sqlsessionFactory --> 25 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 26 <property name="configLocation" value="classpath:SqlMapConfig.xml"></property> 27 <property name="dataSource" ref="dataSource"></property> 28 </bean> 29 <!-- 配置扫描包,加载mapper代理对象 --> 30 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 31 <property name="basePackage" value="com.b2cTest.mapper"></property> 32 </bean> 33 </beans>
3.service
1.创建applicationContext-Service.xml 扫描service包
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 8 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 9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 10 11 <!-- 扫描包加载Service实现类 --> 12 <context:component-scan base-package="com.b2cTest.service"></context:component-scan> 13 </beans>
2.创建applicationContext-trans.xml 管理事务,通知
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 8 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 9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 10 11 <!-- 事务管理器 --> 12 <bean id="transactionManager" 13 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 14 <!-- 数据源 --> 15 <property name="dataSource" ref="dataSource" /> 16 </bean> 17 <!-- 通知 --> 18 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 19 <tx:attributes> 20 <!-- 传播行为 --> 21 <tx:method name="save*" propagation="REQUIRED" /> 22 <tx:method name="insert*" propagation="REQUIRED" /> 23 <tx:method name="add*" propagation="REQUIRED" /> 24 <tx:method name="create*" propagation="REQUIRED" /> 25 <tx:method name="delete*" propagation="REQUIRED" /> 26 <tx:method name="update*" propagation="REQUIRED" /> 27 <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> 28 <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> 29 <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> 30 </tx:attributes> 31 </tx:advice> 32 <!-- 切面 --> 33 <aop:config> 34 <aop:advisor advice-ref="txAdvice" 35 pointcut="execution(* com.b2cTest.service.*.*(..))" /> 36 </aop:config> 37 </beans>
4.创建springMVC.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <context:component-scan base-package="com.b2cTest.controller" /> 11 <mvc:annotation-driven /> 12 <bean 13 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 14 <property name="prefix" value="/WEB-INF/jsp/" /> 15 <property name="suffix" value=".jsp" /> 16 </bean> 17 18 <!-- 资源映射 --> 19 <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> 20 <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/> 21 </beans>
5.修改web.xml
1 <!-- 加载spring容器 --> 2 <context-param> 3 <param-name>contextConfigLocation</param-name> 4 <param-value>classpath:applicationContext-*.xml</param-value> 5 </context-param> 6 <listener> 7 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 8 </listener> 9 <!-- 解决post乱码 --> 10 <filter> 11 <filter-name>CharacterEncodingFilter</filter-name> 12 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 13 <init-param> 14 <param-name>encoding</param-name> 15 <param-value>utf-8</param-value> 16 </init-param> 17 </filter> 18 <filter-mapping> 19 <filter-name>CharacterEncodingFilter</filter-name> 20 <url-pattern>/*</url-pattern> 21 </filter-mapping> 22 <!-- springmvc的前端控制器 --> 23 <servlet> 24 <servlet-name>b2cTest-manager</servlet-name> 25 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 26 <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" --> 27 <init-param> 28 <param-name>contextConfigLocation</param-name> 29 <param-value>classpath:springMVC.xml</param-value> 30 </init-param> 31 <load-on-startup>1</load-on-startup> 32 </servlet> 33 <servlet-mapping> 34 <servlet-name>b2cTest-manager</servlet-name> 35 <url-pattern>/</url-pattern> 36 </servlet-mapping>
6.启动项目,没有报错,项目则成功启动
7.打jar包过程中,默认是不把xml文件加入jar包的,所以我们需要在mapper的pom文件下加入
1 <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 --> 2 <build> 3 <resources> 4 <resource> 5 <directory>src/main/java</directory> 6 <includes> 7 <include>**/*.properties</include> 8 <include>**/*.xml</include> 9 </includes> 10 <filtering>false</filtering> 11 </resource> 12 </resources> 13 </build>
浙公网安备 33010602011771号