SSM框架整合

心已赠人 2022-06-09 00:48 397阅读 0赞

1、Dao层:

Mybatis的配置文件:SqlMapConfig.xml

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

applicationContext-dao.xml:

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

创建SqlMapConfig配置文件

  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. </configuration>

整合mybatis,创建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.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. <context:property-placeholder location="classpath:conf/db.properties" />
  13. <!-- 数据库连接池 -->
  14. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
  15. destroy-method="close">
  16. <property name="url" value="${jdbc.url}" />
  17. <property name="username" value="${jdbc.username}" />
  18. <property name="password" value="${jdbc.password}" />
  19. <property name="driverClassName" value="${jdbc.driver}" />
  20. <property name="maxActive" value="10" />
  21. <property name="minIdle" value="5" />
  22. </bean>
  23. <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
  24. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  25. <!-- 数据库连接池 -->
  26. <property name="dataSource" ref="dataSource" />
  27. <!-- 加载mybatis的全局配置文件 -->
  28. <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
  29. </bean>
  30. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  31. <property name="basePackage" value="cn.e3mall.mapper" />
  32. </bean>
  33. </beans>

db.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/e3mall?characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=root

备注:

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

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

2、Service层:

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

管理service

applicationContext-service.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. <context:component-scan base-package="cn.e3mall.service"/>
  11. </beans>

事务管理

applicationContext-trans.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  7. 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
  8. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  9. <!-- 事务管理器 -->
  10. <bean id="transactionManager"
  11. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  12. <!-- 数据源 -->
  13. <property name="dataSource" ref="dataSource" />
  14. </bean>
  15. <!-- 通知 -->
  16. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  17. <tx:attributes>
  18. <!-- 传播行为 -->
  19. <tx:method name="save*" propagation="REQUIRED" />
  20. <tx:method name="insert*" propagation="REQUIRED" />
  21. <tx:method name="add*" propagation="REQUIRED" />
  22. <tx:method name="create*" propagation="REQUIRED" />
  23. <tx:method name="delete*" propagation="REQUIRED" />
  24. <tx:method name="update*" propagation="REQUIRED" />
  25. <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
  26. <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
  27. <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
  28. </tx:attributes>
  29. </tx:advice>
  30. <!-- 切面 -->
  31. <aop:config>
  32. <aop:advisor advice-ref="txAdvice"
  33. pointcut="execution(* cn.e3mall.service.*.*(..))" />
  34. </aop:config>
  35. </beans>

3、表现层:

Springmvc框架,由springmvc管理controller。Springmvc的三大组件(处理器适配器,处理器映射器,前端控制器)。

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-4.2.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
  9. <context:component-scan base-package="cn.e3mall.controller" />
  10. <mvc:annotation-driven />
  11. <bean
  12. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  13. <property name="prefix" value="/WEB-INF/jsp/" />
  14. <property name="suffix" value=".jsp" />
  15. </bean>
  16. </beans>

4、web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <display-name>e3-manager</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <!-- 加载spring容器 -->
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:spring/applicationContext*.xml</param-value>
  14. </context-param>
  15. <listener>
  16. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  17. </listener>
  18. <!-- 解决post乱码 -->
  19. <filter>
  20. <filter-name>CharacterEncodingFilter</filter-name>
  21. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  22. <init-param>
  23. <param-name>encoding</param-name>
  24. <param-value>utf-8</param-value>
  25. </init-param>
  26. </filter>
  27. <filter-mapping>
  28. <filter-name>CharacterEncodingFilter</filter-name>
  29. <url-pattern>/*</url-pattern>
  30. </filter-mapping>
  31. <!-- springmvc的前端控制器 -->
  32. <servlet>
  33. <servlet-name>e3-manager</servlet-name>
  34. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  35. <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
  36. <init-param>
  37. <param-name>contextConfigLocation</param-name>
  38. <param-value>classpath:spring/springmvc.xml</param-value>
  39. </init-param>
  40. <load-on-startup>1</load-on-startup>
  41. </servlet>
  42. <servlet-mapping>
  43. <servlet-name>e3-manager</servlet-name>
  44. <url-pattern>/</url-pattern>
  45. </servlet-mapping>
  46. </web-app>

发表评论

表情:
评论列表 (有 0 条评论,397人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SSM框架整合

    1.创建maven项目,准备实体表 idea创建一个maven项目,选用骨架webapp。创建后边验证要用的数据库和实体表。 create database s

    相关 SSM框架整合

    最近接触了不少的项目,从SSM到SSH都接触到了,然而对于这几个项目的架构还是一知半解,我自己花费了一些时间,整理下,写了一个SSM的框架的demo,后期我会给出SSH的一个d

    相关 SSM框架整合

    1、Dao层: Mybatis的配置文件:SqlMapConfig.xml 不需要配置任何内容,需要有文件头。文件必须存在。 applicationContext-d

    相关 ssm框架整合

    整合思路 1、Dao层: Mybatis的配置文件:SqlMapConfig.xml 不需要配置任何内容,需要有文件头。文件必须存在。 application