三大框架整合的思路

1、Dao层:

Mybatis的配置文件:SqlMapConfig.xml

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

applicationContext-dao.xml

mybatis整合spring,通过由spring创建数据库连接池,spring管理SqlSessionFactorymapper代理对象。需要mybatisspring的整合包。

2、Service层:

applicationContext-service.xml

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

 

3、表现层:

Springmvc框架,由springmvc管理controller

Springmvc的三大组件。

(1)使用maven管理jar包 (这里面除了ssm框架的jar包之外,其他的可以根据需要选择删除)

  1. <!--这里只是锁定了一些版本号,不存在依赖管理-->
  2.  
  3. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>nyist.e3</groupId>
  7. <artifactId>e3-parent</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>pom</packaging>
  10. <!-- 集中定义依赖版本号 -->
  11. <properties>
  12. <junit.version>4.12</junit.version>
  13. <spring.version>4.2.4.RELEASE</spring.version>
  14. <mybatis.version>3.2.8</mybatis.version>
  15. <mybatis.spring.version>1.2.2</mybatis.spring.version>
  16. <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
  17. <mysql.version>5.1.32</mysql.version>
  18. <slf4j.version>1.6.4</slf4j.version>
  19. <jackson.version>2.4.2</jackson.version>
  20. <druid.version>1.0.9</druid.version>
  21. <httpclient.version>4.3.5</httpclient.version>
  22. <jstl.version>1.2</jstl.version>
  23. <servlet-api.version>2.5</servlet-api.version>
  24. <jsp-api.version>2.0</jsp-api.version>
  25. <joda-time.version>2.5</joda-time.version>
  26. <commons-lang3.version>3.3.2</commons-lang3.version>
  27. <commons-io.version>1.3.2</commons-io.version>
  28. <commons-net.version>3.3</commons-net.version>
  29. <pagehelper.version>3.4.2-fix</pagehelper.version>
  30. <jsqlparser.version>0.9.1</jsqlparser.version>
  31. <commons-fileupload.version>1.3.1</commons-fileupload.version>
  32. <jedis.version>2.7.2</jedis.version>
  33. <solrj.version>4.10.3</solrj.version>
  34. <dubbo.version>2.5.3</dubbo.version>
  35. <zookeeper.version>3.4.7</zookeeper.version>
  36. <zkclient.version>0.1</zkclient.version>
  37. <activemq.version>5.11.2</activemq.version>
  38. <freemarker.version>2.3.23</freemarker.version>
  39. <quartz.version>2.2.2</quartz.version>
  40. </properties>
  41. <dependencyManagement>
  42. <dependencies>
  43. <!-- 时间操作组件 -->
  44. <dependency>
  45. <groupId>joda-time</groupId>
  46. <artifactId>joda-time</artifactId>
  47. <version>${joda-time.version}</version>
  48. </dependency>
  49. <!-- Apache工具组件 -->
  50. <dependency>
  51. <groupId>org.apache.commons</groupId>
  52. <artifactId>commons-lang3</artifactId>
  53. <version>${commons-lang3.version}</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.apache.commons</groupId>
  57. <artifactId>commons-io</artifactId>
  58. <version>${commons-io.version}</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>commons-net</groupId>
  62. <artifactId>commons-net</artifactId>
  63. <version>${commons-net.version}</version>
  64. </dependency>
  65. <!-- Jackson Json处理工具包 -->
  66. <dependency>
  67. <groupId>com.fasterxml.jackson.core</groupId>
  68. <artifactId>jackson-databind</artifactId>
  69. <version>${jackson.version}</version>
  70. </dependency>
  71. <!-- httpclient -->
  72. <dependency>
  73. <groupId>org.apache.httpcomponents</groupId>
  74. <artifactId>httpclient</artifactId>
  75. <version>${httpclient.version}</version>
  76. </dependency>
  77. <!-- quartz任务调度框架 -->
  78. <dependency>
  79. <groupId>org.quartz-scheduler</groupId>
  80. <artifactId>quartz</artifactId>
  81. <version>${quartz.version}</version>
  82. </dependency>
  83. <!-- 单元测试 -->
  84. <dependency>
  85. <groupId>junit</groupId>
  86. <artifactId>junit</artifactId>
  87. <version>${junit.version}</version>
  88. <scope>test</scope>
  89. </dependency>
  90. <!-- 日志处理 -->
  91. <dependency>
  92. <groupId>org.slf4j</groupId>
  93. <artifactId>slf4j-log4j12</artifactId>
  94. <version>${slf4j.version}</version>
  95. </dependency>
  96. <!-- Mybatis -->
  97. <dependency>
  98. <groupId>org.mybatis</groupId>
  99. <artifactId>mybatis</artifactId>
  100. <version>${mybatis.version}</version>
  101. </dependency>
  102. <dependency>
  103. <groupId>org.mybatis</groupId>
  104. <artifactId>mybatis-spring</artifactId>
  105. <version>${mybatis.spring.version}</version>
  106. </dependency>
  107. <dependency>
  108. <groupId>com.github.miemiedev</groupId>
  109. <artifactId>mybatis-paginator</artifactId>
  110. <version>${mybatis.paginator.version}</version>
  111. </dependency>
  112. <dependency>
  113. <groupId>com.github.pagehelper</groupId>
  114. <artifactId>pagehelper</artifactId>
  115. <version>${pagehelper.version}</version>
  116. </dependency>
  117. <!-- MySql -->
  118. <dependency>
  119. <groupId>mysql</groupId>
  120. <artifactId>mysql-connector-java</artifactId>
  121. <version>${mysql.version}</version>
  122. </dependency>
  123. <!-- 连接池 -->
  124. <dependency>
  125. <groupId>com.alibaba</groupId>
  126. <artifactId>druid</artifactId>
  127. <version>${druid.version}</version>
  128. </dependency>
  129. <!-- Spring -->
  130. <dependency>
  131. <groupId>org.springframework</groupId>
  132. <artifactId>spring-context</artifactId>
  133. <version>${spring.version}</version>
  134. </dependency>
  135. <dependency>
  136. <groupId>org.springframework</groupId>
  137. <artifactId>spring-beans</artifactId>
  138. <version>${spring.version}</version>
  139. </dependency>
  140. <dependency>
  141. <groupId>org.springframework</groupId>
  142. <artifactId>spring-webmvc</artifactId>
  143. <version>${spring.version}</version>
  144. </dependency>
  145. <dependency>
  146. <groupId>org.springframework</groupId>
  147. <artifactId>spring-jdbc</artifactId>
  148. <version>${spring.version}</version>
  149. </dependency>
  150. <dependency>
  151. <groupId>org.springframework</groupId>
  152. <artifactId>spring-aspects</artifactId>
  153. <version>${spring.version}</version>
  154. </dependency>
  155. <dependency>
  156. <groupId>org.springframework</groupId>
  157. <artifactId>spring-jms</artifactId>
  158. <version>${spring.version}</version>
  159. </dependency>
  160. <dependency>
  161. <groupId>org.springframework</groupId>
  162. <artifactId>spring-context-support</artifactId>
  163. <version>${spring.version}</version>
  164. </dependency>
  165. <!-- JSP相关 -->
  166. <dependency>
  167. <groupId>jstl</groupId>
  168. <artifactId>jstl</artifactId>
  169. <version>${jstl.version}</version>
  170. </dependency>
  171. <dependency>
  172. <groupId>javax.servlet</groupId>
  173. <artifactId>servlet-api</artifactId>
  174. <version>${servlet-api.version}</version>
  175. <scope>provided</scope>
  176. </dependency>
  177. <dependency>
  178. <groupId>javax.servlet</groupId>
  179. <artifactId>jsp-api</artifactId>
  180. <version>${jsp-api.version}</version>
  181. <scope>provided</scope>
  182. </dependency>
  183. <!-- 文件上传组件 -->
  184. <dependency>
  185. <groupId>commons-fileupload</groupId>
  186. <artifactId>commons-fileupload</artifactId>
  187. <version>${commons-fileupload.version}</version>
  188. </dependency>
  189. <!-- Redis客户端 -->
  190. <dependency>
  191. <groupId>redis.clients</groupId>
  192. <artifactId>jedis</artifactId>
  193. <version>${jedis.version}</version>
  194. </dependency>
  195. <!-- solr客户端 -->
  196. <dependency>
  197. <groupId>org.apache.solr</groupId>
  198. <artifactId>solr-solrj</artifactId>
  199. <version>${solrj.version}</version>
  200. </dependency>
  201. <!-- dubbo相关 -->
  202. <dependency>
  203. <groupId>com.alibaba</groupId>
  204. <artifactId>dubbo</artifactId>
  205. <version>${dubbo.version}</version>
  206. </dependency>
  207. <dependency>
  208. <groupId>org.apache.zookeeper</groupId>
  209. <artifactId>zookeeper</artifactId>
  210. <version>${zookeeper.version}</version>
  211. </dependency>
  212. <dependency>
  213. <groupId>com.github.sgroschupf</groupId>
  214. <artifactId>zkclient</artifactId>
  215. <version>${zkclient.version}</version>
  216. </dependency>
  217. <dependency>
  218. <groupId>org.apache.activemq</groupId>
  219. <artifactId>activemq-all</artifactId>
  220. <version>${activemq.version}</version>
  221. </dependency>
  222. <dependency>
  223. <groupId>org.freemarker</groupId>
  224. <artifactId>freemarker</artifactId>
  225. <version>${freemarker.version}</version>
  226. </dependency>
  227.  
  228. </dependencies>
  229. </dependencyManagement>
  230.  
  231. </project>

(2)配置文件的整合

将mybatis的核心配置文件交给spring工厂集中管理

  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.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  8. 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
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  10.  
  11. <!-- 数据库连接池 -->
  12. <!-- 加载配置文件 ,只能加载一个配置文件,因此通过通配符加载conf下所有的配置文件-->
  13. <context:property-placeholder location="classpath:conf/*.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. <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
  25. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  26. <!-- 数据库连接池 -->
  27. <property name="dataSource" ref="dataSource" />
  28. <!-- 加载mybatis的全局配置文件 -->
  29. <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
  30. </bean>
  31. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  32. <property name="basePackage" value="nyist.e3.mapper" />
  33. </bean>
  34. </beans>

View Code

配置事务(这里使用的是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.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  8. 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
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  10.  
  11. <!-- spring和service层和事务整合 -->
  12. <!-- 注册事务管理器 -->
  13. <bean id="transactionManager"
  14. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  15. <!-- 注入数据源 -->
  16. <property name="dataSource" ref="dataSource"></property>
  17. </bean>
  18.  
  19. <!-- 配置事务通知 -->
  20. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  21. <tx:attributes>
  22. <!-- 配置事务的传播属性 -->
  23. <tx:method name="save*" propagation="REQUIRED" />
  24. <tx:method name="insert*" propagation="REQUIRED" />
  25. <tx:method name="add*" propagation="REQUIRED" />
  26. <tx:method name="create*" propagation="REQUIRED" />
  27. <tx:method name="delete*" propagation="REQUIRED" />
  28. <tx:method name="update*" propagation="REQUIRED" />
  29. <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
  30. <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
  31. <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
  32. </tx:attributes>
  33. </tx:advice>
  34.  
  35.  
  36. <!-- 组装切面 -->
  37. <aop:config>
  38. <!-- 配置切点表达式 -->
  39. <aop:pointcut expression="execution(* nyist.e3.service..*.*(..))"
  40. id="pc" />
  41. <!-- 将事务的传播属性作用到切点函数中 -->
  42. <aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
  43. </aop:config>
  44. </beans>

View Code

 

(3)在web.xml中配置spring监听,加载spring配置文件

配置springmvc前端控制器,拦截请求

  1. <!-- 加载spring容器 -->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:spring/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.  
  23.  
  24. <!-- springmvc的前端控制器 -->
  25. <servlet>
  26. <servlet-name>e3-manager</servlet-name>
  27. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  28. <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
  29. <init-param>
  30. <param-name>contextConfigLocation</param-name>
  31. <param-value>classpath:spring/springmvc.xml</param-value>
  32. </init-param>
  33. <load-on-startup>1</load-on-startup>
  34. </servlet>
  35. <servlet-mapping>
  36. <servlet-name>e3-manager</servlet-name>
  37. <url-pattern>/</url-pattern>
  38. </servlet-mapping>

 

版权声明:本文为shuai-server原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/shuai-server/p/8974562.html