1、整合准备
整合内容:
整合架构:Spring、SpringMVC以及MyBatis的整合。
数据库:MySQL
连接池:Druid(阿里巴巴研发)
整合工具:
整合工具为:Eclipse
Jar管理工具:Maven
项目类型为:Maven Pproject
2、SSM整合
2.1、导入整合相关的jar包(Maven)
pom.xml文件


1 <project 2 xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 5 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 6 <modelVersion>4.0.0modelVersion> 7 <parent> 8 <groupId>com.woo0nisegroupId> 9 <artifactId>woo0nise-parentartifactId> 10 <version>0.0.1-SNAPSHOTversion> 11 parent> 12 <groupId>SSMgroupId> 13 <artifactId>SSMartifactId> 14 <version>0.0.1-SNAPSHOTversion> 15 <packaging>warpackaging> 16 <dependencies> 17 18 <dependency> 19 <groupId>mysqlgroupId> 20 <artifactId>mysql-connector-javaartifactId> 21 dependency> 22 23 <dependency> 24 <groupId>com.alibabagroupId> 25 <artifactId>druidartifactId> 26 dependency> 27 28 29 <dependency> 30 <groupId>org.springframeworkgroupId> 31 <artifactId>spring-coreartifactId> 32 dependency> 33 <dependency> 34 <groupId>org.springframeworkgroupId> 35 <artifactId>spring-contextartifactId> 36 dependency> 37 <dependency> 38 <groupId>org.springframeworkgroupId> 39 <artifactId>spring-beansartifactId> 40 dependency> 41 <dependency> 42 <groupId>org.springframeworkgroupId> 43 <artifactId>spring-webartifactId> 44 dependency> 45 46 <dependency> 47 <groupId>org.springframeworkgroupId> 48 <artifactId>spring-jdbcartifactId> 49 dependency> 50 <dependency> 51 <groupId>org.springframeworkgroupId> 52 <artifactId>spring-aspectsartifactId> 53 dependency> 54 55 <dependency> 56 <groupId>org.springframeworkgroupId> 57 <artifactId>spring-ormartifactId> 58 dependency> 59 60 <dependency> 61 <groupId>org.springframeworkgroupId> 62 <artifactId>spring-webmvcartifactId> 63 dependency> 64 65 <dependency> 66 <groupId>com.fasterxml.jackson.coregroupId> 67 <artifactId>jackson-databindartifactId> 68 dependency> 69 70 <dependency> 71 <groupId>junitgroupId> 72 <artifactId>junitartifactId> 73 <scope>testscope> 74 dependency> 75 76 <dependency> 77 <groupId>org.slf4jgroupId> 78 <artifactId>slf4j-log4j12artifactId> 79 dependency> 80 81 <dependency> 82 <groupId>org.mybatisgroupId> 83 <artifactId>mybatisartifactId> 84 dependency> 85 86 <dependency> 87 <groupId>org.mybatisgroupId> 88 <artifactId>mybatis-springartifactId> 89 dependency> 90 91 <dependency> 92 <groupId>com.github.pagehelpergroupId> 93 <artifactId>pagehelperartifactId> 94 dependency> 95 <dependency> 96 <groupId>com.github.miemiedevgroupId> 97 <artifactId>mybatis-paginatorartifactId> 98 dependency> 99 100 <dependency> 101 <groupId>jstlgroupId> 102 <artifactId>jstlartifactId> 103 dependency> 104 <dependency> 105 <groupId>javax.servletgroupId> 106 <artifactId>servlet-apiartifactId> 107 <scope>providedscope> 108 dependency> 109 <dependency> 110 <groupId>javax.servletgroupId> 111 <artifactId>jsp-apiartifactId> 112 <scope>providedscope> 113 dependency> 114 dependencies> 115 116 <build> 117 <resources> 118 <resource> 119 <directory>src/main/javadirectory> 120 <includes> 121 <include>**/*.propertiesinclude> 122 <include>**/*.xmlinclude> 123 includes> 124 <filtering>falsefiltering> 125 resource> 126 resources> 127 build> 128 project>
2.2、创建整合目录
Mybatis:存放mybatis配置文件
Spring:存放spring以及整合文件
Resource:数据库连接以及log4配置信息
2.3、创建MyBatis配置文件
创建mybatis-config.xml文件位于mybatis目录下
mybatis-config.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 configuration>
该文件主要存mybatis配置。(该配置文件可以不创建)
2.4、创建数据库配置信息
创建db.properties文件位于resource文件夹下
db.properties
1 #数据库连接驱动 2 jdbc.driver=com.mysql.jdbc.Driver 3 #数据库连接url 4 jdbc.url=jdbc:mysql://localhost:3306/woo0nise?characterEncoding=utf-8 5 #数据库用户名 6 jdbc.username=root 7 #数据库密码 8 jdbc.password=root
该配置文件主要存放数据库的配置,也可以存放mybatis配置文件的属性。
2.5、Spring整合mybatis
创建applicationContext-dao.xml文件位于spring文件夹下
applicationContext-dao.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" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 7 xmlns:p="http://www.springframework.org/schema/p" 8 xmlns:tx="http://www.springframework.org/schema/tx" 9 xmlns:util="http://www.springframework.org/schema/util" 10 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 11 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 13 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 15 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 16 17 <context:property-placeholder location="classpath:resource/db.properties" /> 18 19 20 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 21 destroy-method="close"> 22 <property name="url" value="${jdbc.url}" /> 23 <property name="username" value="${jdbc.username}" /> 24 <property name="password" value="${jdbc.password}" /> 25 <property name="driverClassName" value="${jdbc.driver}" /> 26 <property name="maxActive" value="10" /> 27 <property name="minIdle" value="5" /> 28 bean> 29 30 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" > 31 32 <property name="dataSource" ref="dataSource" >property> 33 34 <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" >property> 35 bean> 36 37 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" > 38 <property name="basePackage" value="com.test.dao" >property> 39 bean> 40 beans>
该配置文件主要是spring整合mybatis文件。
2.6、Spring事务管理
创建applicationContext-trans.xml位于spring文件夹下
applicationContext-trans.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" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 6 xmlns:p="http://www.springframework.org/schema/p" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:util="http://www.springframework.org/schema/util" 9 xmlns:aop="http://www.springframework.org/schema/aop" 10 xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd 11 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 13 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 15 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 16 17 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > 18 19 <property name="dataSource" ref="dataSource" >property> 20 bean> 21 22 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 23 <tx:attributes> 24 <tx:method name="save*" propagation="REQUIRED" /> 25 <tx:method name="insert*" propagation="REQUIRED" /> 26 <tx:method name="add*" propagation="REQUIRED" /> 27 <tx:method name="create*" propagation="REQUIRED" /> 28 <tx:method name="delete*" propagation="REQUIRED" /> 29 <tx:method name="update*" propagation="REQUIRED" /> 30 <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> 31 <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> 32 <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> 33 tx:attributes> 34 tx:advice> 35 36 <aop:config> 37 <aop:advisor advice-ref="txAdvice" 38 pointcut="execution(* com.taotao.service.impl.*.*(..))" /> 39 aop:config> 40 beans>
该配置文件主要配置事务以及事务管理和切面(AOP)配置。
2.7、Spring扫描Service的实现类
创建applicationContext-service.xml位于spring文件夹下
applicationContext-service.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" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 7 xmlns:p="http://www.springframework.org/schema/p" 8 xmlns:tx="http://www.springframework.org/schema/tx" 9 xmlns:util="http://www.springframework.org/schema/util" 10 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 11 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 13 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 15 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 16 <context:component-scan base-package="com.test.service">context:component-scan> 17 beans>
该配置文件主要是用来扫描Service的实现类。
2.8、创建springmvc.xml
创建springmvc.xml文件位于spring文件夹下
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" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:util="http://www.springframework.org/schema/util" 8 xmlns:mvc="http://www.springframework.org/schema/mvc" 9 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 10 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 11 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 13 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 14 <context:component-scan base-package="com.test.controller">context:component-scan> 15 16 <mvc:annotation-driven /> 17 <bean 18 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 19 <property name="prefix" value="/WEB-INF/jsp/" /> 20 <property name="suffix" value=".jsp" /> 21 bean> 22 beans>
2.9、配置web.xml
修改web.xml


1 xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 7 <display-name>taotao-managerdisplay-name> 8 <welcome-file-list> 9 <welcome-file>index.jspwelcome-file> 10 welcome-file-list> 11 12 <context-param> 13 <param-name>contextConfigLocationparam-name> 14 <param-value>classpath:spring/applicationContext-*.xmlparam-value> 15 context-param> 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> 18 listener> 19 20 <filter> 21 <filter-name>CharacterEncodingFilterfilter-name> 22 <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> 23 <init-param> 24 <param-name>encodingparam-name> 25 <param-value>utf-8param-value> 26 init-param> 27 31 filter> 32 <filter-mapping> 33 <filter-name>CharacterEncodingFilterfilter-name> 34 <url-pattern>/*url-pattern> 35 filter-mapping> 36 37 <servlet> 38 <servlet-name>dispatcherServletservlet-name> 39 <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> 40 41 <init-param> 42 <param-name>contextConfigLocationparam-name> 43 <param-value>classpath:spring/springmvc.xmlparam-value> 44 init-param> 45 <load-on-startup>1load-on-startup> 46 servlet> 47 <servlet-mapping> 48 <servlet-name>dispatcherServletservlet-name> 49 <url-pattern>/url-pattern> 50 servlet-mapping> 51 web-app>
3、测试整合
3.1、创建实体类以及dao层
1、导入mybatis逆向工程
创建com.test.dao和com.test.entity包用于存放生成的文件
2、修改generatorConfig.xml文件


1 xml version="1.0" encoding="UTF-8"?> 2 DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 <context id="testTables" targetRuntime="MyBatis3"> 8 <commentGenerator> 9 10 <property name="suppressAllComments" value="true" /> 11 commentGenerator> 12 13 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 14 connectionURL="jdbc:mysql://localhost:3306/woo0nise" userId="root" 15 password="root"> 16 jdbcConnection> 17 19 <javaTypeResolver> 20 <property name="forceBigDecimals" value="false" /> 21 javaTypeResolver> 22 23 24 <javaModelGenerator targetPackage="com.test.entity" 25 targetProject=".\src"> 26 27 <property name="enableSubPackages" value="false" /> 28 29 <property name="trimStrings" value="true" /> 30 javaModelGenerator> 31 32 <sqlMapGenerator targetPackage="com.test.dao" 33 targetProject=".\src"> 34 35 <property name="enableSubPackages" value="false" /> 36 sqlMapGenerator> 37 38 <javaClientGenerator type="XMLMAPPER" 39 targetPackage="com.test.dao" 40 targetProject=".\src"> 41 42 <property name="enableSubPackages" value="false" /> 43 javaClientGenerator> 44 45 <table schema="" tableName="user">table> 46 context> 47 generatorConfiguration>
3、运行GeneratorSqlmap生成实体文件以及mapper文件
4、将com.test.dao以及com.tes.entity到本工程中
3.2、创建业务逻辑层
1、创建com.test.service包用于存放业务逻辑层接口类
创建UserService接口文件
1 package com.test.service; 2 3 import com.test.entity.User; 4 5 /** 6 *Title:UserService
7 *Description:
8 *Company:www.hack-gov.com
9 * @author 0nise 10 * @date 2017年1月1日 上午12:47:42 11 * @Email [email protected] 12 * @version 1.0 13 */ 14 public interface UserService { 15 /** 16 * 新增用户 17 * @author 0nise 18 * @date 2017年1月1日 上午12:48:12 19 * @Email [email protected] 20 * @param user 21 */ 22 public int add(User user); 23 }
2、创建com.testservice.impl包用于存放业务逻辑层接口的实现类
创建UserServiceImpl类用于实现UserService接口文件
1 package com.test.service.impl; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 6 import com.test.dao.UserMapper; 7 import com.test.entity.User; 8 import com.test.service.UserService; 9 /** 10 *Title:UserServiceImpl
11 *Description:
12 *Company:www.hack-gov.com
13 * @author 0nise 14 * @date 2017年1月1日 上午12:51:34 15 * @Email [email protected] 16 * @version 1.0 17 */ 18 @Service 19 public class UserServiceImpl implements UserService { 20 //注入UserMapper 21 @Autowired 22 private UserMapper userMapper; 23 24 @Override 25 public int add(User user) { 26 try { 27 userMapper.insert(user); 28 return 1; 29 } catch (Exception e) { 30 e.printStackTrace(); 31 return -1; 32 } 33 } 34 }
3.3、创建Controller层
3.3.1、创建com.test.controller包用户存放controller
创建UserController
1 package com.test.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import com.test.entity.User; 7 import com.test.service.UserService; 8 9 /** 10 *Title:UserController
11 *Description:
12 *Company:www.hack-gov.com
13 * @author 0nise 14 * @date 2017年1月1日 上午12:55:57 15 * @Email [email protected] 16 * @version 1.0 17 */ 18 @Controller 19 public class UserController { 20 //注入UserService 21 @Autowired 22 private UserService userService; 23 @RequestMapping("ok") 24 public String add(){ 25 User user = new User(); 26 user.setUsername("admin99"); 27 user.setUserpwd("admin99"); 28 int i = userService.add(user); 29 if(i > 0){ 30 System.out.println("ok"); 31 }else{ 32 System.err.println("error"); 33 } 34 return null; 35 } 36 }