SSM整合

整合思路

1、Dao层:

mybatis整合spring,通过spring管理SqlSessionFactory、mapper代理对象。需要mybatis和spring的整合包。

整合内容

对应工程

Pojo

Taotao-mangaer-pojo

Mapper映射文件

Taotao-mangaer-mapper

Mapper接口

Taotao-mangaer-mapper

sqlmapConfig.xml

Taotao-manager-web

applicationContext-dao.xml

Taotao-manager-web

 

2、Service层:

所有的实现类都放到spring容器中管理。由spring创建数据库连接池,并有spring管理实务。

整合内容

对应工程

Service接口及实现类

Taotao-mangaer-service

applicationContext-service.xml

Taotao-manager-web

applicationContext-trans.xml

Taotao-manager-web

 

3、表现层:

Springmvc整合spring框架,由springmvc管理controller。

整合内容

对应工程

springmvc.xml

Taotao-manager-web

Controller

Taotao-manager-web


一、dao整合

1. 创建SqlMapConfig.xml配置文件

[html]  view plain  copy
  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.Spring整合mybatis

创建applicationContext-dao.xml

[html]  view plain  copy
  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.0.xsd  
  6.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  7.     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  
  8.     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">  
  9.   
  10.     <!-- 数据库连接池 -->  
  11.     <!-- 加载配置文件 -->  
  12.     <context:property-placeholder location="classpath:properties/*.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="com.taotao.mapper" />  
  32.     </bean>  
  33. </beans>  

db.properties
[html]  view plain  copy
  1. jdbc.driver=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8  
  3. jdbc.username=root  
  4. jdbc.password=root  

备注:

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

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


二、Service整合

1. 管理Service实现类

[html]  view plain  copy
  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.0.xsd  
  6.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  7.     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  
  8.     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">  
  9.   
  10.         <context:component-scan base-package="com.taotao.service"/>  
  11.   
  12. </beans>  

2.事务管理

创建applicationContext-trans.xml

[html]  view plain  copy
  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.0.xsd  
  6.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  7.     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  
  8.     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.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(* com.taotao.service.*.*(..))" />  
  34.     </aop:config>  
  35. </beans>  


三、表现层整合

1.Springmvc.xml

[html]  view plain  copy
  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.taotao.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. </beans>  

2.web.xml

[html]  view plain  copy
  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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  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>taotao-manager-web</display-name>  
  7.     <welcome-file-list>  
  8.         <welcome-file>login.html</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.   
  19.     <!-- 解决post乱码 -->  
  20.     <filter>  
  21.         <filter-name>CharacterEncodingFilter</filter-name>  
  22.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  23.         <init-param>  
  24.             <param-name>encoding</param-name>  
  25.             <param-value>utf-8</param-value>  
  26.         </init-param>  
  27.         <!-- <init-param>  
  28.             <param-name>forceEncoding</param-name>  
  29.             <param-value>true</param-value>  
  30.         </init-param> -->  
  31.     </filter>  
  32.     <filter-mapping>  
  33.         <filter-name>CharacterEncodingFilter</filter-name>  
  34.         <url-pattern>/*</url-pattern>  
  35.     </filter-mapping>  
  36.   
  37.   
  38.     <!-- springmvc的前端控制器 -->  
  39.     <servlet>  
  40.         <servlet-name>taotao-manager</servlet-name>  
  41.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  42.         <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->  
  43.         <init-param>  
  44.             <param-name>contextConfigLocation</param-name>  
  45.             <param-value>classpath:spring/springmvc.xml</param-value>  
  46.         </init-param>  
  47.         <load-on-startup>1</load-on-startup>  
  48.     </servlet>  
  49.     <servlet-mapping>  
  50.         <servlet-name>taotao-manager</servlet-name>  
  51.         <url-pattern>/</url-pattern>  
  52.     </servlet-mapping>  
  53. </web-app>  


四、整合静态页面

使用方法:

把静态页面添加到taotao-manager-web工程中的WEB-INF下:



由于在web.xml中定义的url拦截形式为“/”表示拦截所有的url请求,包括静态资源例如css、js等。

所以需要在springmvc.xml中添加资源映射标签:

[html]  view plain  copy
  1. <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>  
  2. <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>  


五、修改taotao-manager-mapper的pom文件:

在pom文件中添加如下内容:

[html]  view plain  copy
  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>  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值