一、整合準備前端
整合架構:Spring、SpringMVC以及MyBatis的整合。java
數據庫:MySQLmysql
鏈接池:Druid(阿里巴巴研發)git
整合工具爲:Eclipsegithub
Jar管理工具:Mavenweb
項目類型爲:Maven Pprojectspring
pom.xml文件sql
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.0</modelVersion> 7 <parent> 8 <groupId>com.woo0nise</groupId> 9 <artifactId>woo0nise-parent</artifactId> 10 <version>0.0.1-SNAPSHOT</version> 11 </parent> 12 <groupId>SSM</groupId> 13 <artifactId>SSM</artifactId> 14 <version>0.0.1-SNAPSHOT</version> 15 <packaging>war</packaging> 16 <dependencies> 17 <!-- MySql --> 18 <dependency> 19 <groupId>mysql</groupId> 20 <artifactId>mysql-connector-java</artifactId> 21 </dependency> 22 <!-- 鏈接池 --> 23 <dependency> 24 <groupId>com.alibaba</groupId> 25 <artifactId>druid</artifactId> 26 </dependency> 27 <!-- Spring --> 28 <!-- spring核心包 --> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-core</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework</groupId> 35 <artifactId>spring-context</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework</groupId> 39 <artifactId>spring-beans</artifactId> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-web</artifactId> 44 </dependency> 45 <!-- Spring對JDBC的封裝 --> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-jdbc</artifactId> 49 </dependency> 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-aspects</artifactId> 53 </dependency> 54 <!-- 關係型數據庫整合時需配置 如hibernate jpa等 --> 55 <dependency> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-orm</artifactId> 58 </dependency> 59 <!-- Spring MVC核心 --> 60 <dependency> 61 <groupId>org.springframework</groupId> 62 <artifactId>spring-webmvc</artifactId> 63 </dependency> 64 <!-- Jackson Json處理工具包(非必須) --> 65 <dependency> 66 <groupId>com.fasterxml.jackson.core</groupId> 67 <artifactId>jackson-databind</artifactId> 68 </dependency> 69 <!-- 單元測試 --> 70 <dependency> 71 <groupId>junit</groupId> 72 <artifactId>junit</artifactId> 73 <scope>test</scope> 74 </dependency> 75 <!-- 日誌處理(非必須) --> 76 <dependency> 77 <groupId>org.slf4j</groupId> 78 <artifactId>slf4j-log4j12</artifactId> 79 </dependency> 80 <!-- Mybatis --> 81 <dependency> 82 <groupId>org.mybatis</groupId> 83 <artifactId>mybatis</artifactId> 84 </dependency> 85 <!-- mybatis的整合包 --> 86 <dependency> 87 <groupId>org.mybatis</groupId> 88 <artifactId>mybatis-spring</artifactId> 89 </dependency> 90 <!-- mybatis分頁插件(非必須) --> 91 <dependency> 92 <groupId>com.github.pagehelper</groupId> 93 <artifactId>pagehelper</artifactId> 94 </dependency> 95 <dependency> 96 <groupId>com.github.miemiedev</groupId> 97 <artifactId>mybatis-paginator</artifactId> 98 </dependency> 99 <!-- JSP相關 --> 100 <dependency> 101 <groupId>jstl</groupId> 102 <artifactId>jstl</artifactId> 103 </dependency> 104 <dependency> 105 <groupId>javax.servlet</groupId> 106 <artifactId>servlet-api</artifactId> 107 <scope>provided</scope> 108 </dependency> 109 <dependency> 110 <groupId>javax.servlet</groupId> 111 <artifactId>jsp-api</artifactId> 112 <scope>provided</scope> 113 </dependency> 114 </dependencies> 115 <!-- 若是不添加此節點mybatis的mapper.xml文件都會被漏掉。 --> 116 <build> 117 <resources> 118 <resource> 119 <directory>src/main/java</directory> 120 <includes> 121 <include>**/*.properties</include> 122 <include>**/*.xml</include> 123 </includes> 124 <filtering>false</filtering> 125 </resource> 126 </resources> 127 </build> 128 </project>
Mybatis:存放mybatis配置文件數據庫
Spring:存放spring以及整合文件apache
Resource:數據庫鏈接以及log4配置信息
建立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配置。(該配置文件能夠不建立)
建立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配置文件的屬性。
建立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 <!-- 配置SqlSessionFactory --> 30 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" > 31 <!-- 配置數據源 --> 32 <property name="dataSource" ref="dataSource" ></property> 33 <!-- 配置MyBatis路徑 --> 34 <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" ></property> 35 </bean> 36 <!-- Mapper文件掃描器 --> 37 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" > 38 <property name="basePackage" value="com.test.dao" ></property> 39 </bean> 40 </beans>
該配置文件主要是spring整合mybatis文件。
建立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)配置。
建立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的實現類。
建立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>
修改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-manager</display-name> 8 <welcome-file-list> 9 <welcome-file>index.jsp</welcome-file> 10 </welcome-file-list> 11 <!-- 加載spring容器 --> 12 <context-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value>classpath:spring/applicationContext-*.xml</param-value> 15 </context-param> 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 18 </listener> 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 <!-- springmvc的前端控制器 --> 37 <servlet> 38 <servlet-name>dispatcherServlet</servlet-name> 39 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 40 <!-- contextConfigLocation不是必須的, 若是不配置contextConfigLocation, springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" --> 41 <init-param> 42 <param-name>contextConfigLocation</param-name> 43 <param-value>classpath:spring/springmvc.xml</param-value> 44 </init-param> 45 <load-on-startup>1</load-on-startup> 46 </servlet> 47 <servlet-mapping> 48 <servlet-name>dispatcherServlet</servlet-name> 49 <url-pattern>/</url-pattern> 50 </servlet-mapping> 51 </web-app>
一、導入mybatis逆向工程
建立com.test.dao和com.test.entity包用於存放生成的文件
二、修改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 <!-- 是否去除自動生成的註釋 true:是 : false:否 --> 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 <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer,爲 true時把JDBC DECIMAL 和 18 NUMERIC 類型解析爲java.math.BigDecimal --> 19 <javaTypeResolver> 20 <property name="forceBigDecimals" value="false" /> 21 </javaTypeResolver> 22 23 <!-- targetProject:生成PO類的位置 --> 24 <javaModelGenerator targetPackage="com.test.entity" 25 targetProject=".\src"> 26 <!-- enableSubPackages:是否讓schema做爲包的後綴 --> 27 <property name="enableSubPackages" value="false" /> 28 <!-- 從數據庫返回的值被清理先後的空格 --> 29 <property name="trimStrings" value="true" /> 30 </javaModelGenerator> 31 <!-- targetProject:mapper映射文件生成的位置 --> 32 <sqlMapGenerator targetPackage="com.test.dao" 33 targetProject=".\src"> 34 <!-- enableSubPackages:是否讓schema做爲包的後綴 --> 35 <property name="enableSubPackages" value="false" /> 36 </sqlMapGenerator> 37 <!-- targetPackage:mapper接口生成的位置 --> 38 <javaClientGenerator type="XMLMAPPER" 39 targetPackage="com.test.dao" 40 targetProject=".\src"> 41 <!-- enableSubPackages:是否讓schema做爲包的後綴 --> 42 <property name="enableSubPackages" value="false" /> 43 </javaClientGenerator> 44 <!-- 指定數據庫表 --> 45 <table schema="" tableName="user"></table> 46 </context> 47 </generatorConfiguration>
三、運行GeneratorSqlmap生成實體文件以及mapper文件
四、將com.test.dao以及com.tes.entity到本工程中
一、建立com.test.service包用於存放業務邏輯層接口類
建立UserService接口文件
1 package com.test.service; 2 3 import com.test.entity.User; 4 5 /** 6 * <p>Title:UserService</p> 7 * <p>Description:</p> 8 * <p>Company:www.hack-gov.com</p> 9 * @author 0nise 10 * @date 2017年1月1日 上午12:47:42 11 * @Email woo0nise@gmail.com 12 * @version 1.0 13 */ 14 public interface UserService { 15 /** 16 * 新增用戶 17 * @author 0nise 18 * @date 2017年1月1日 上午12:48:12 19 * @Email woo0nise@gmail.com 20 * @param user 21 */ 22 public int add(User user); 23 }
二、建立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 * <p>Title:UserServiceImpl</p> 11 * <p>Description:</p> 12 * <p>Company:www.hack-gov.com</p> 13 * @author 0nise 14 * @date 2017年1月1日 上午12:51:34 15 * @Email woo0nise@gmail.com 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.一、建立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 * <p>Title:UserController</p> 11 * <p>Description:</p> 12 * <p>Company:www.hack-gov.com</p> 13 * @author 0nise 14 * @date 2017年1月1日 上午12:55:57 15 * @Email woo0nise@gmail.com 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 }