SSM框架整合(注解)-Spring+SpringMVC+MyBatis+MySql

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.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>
pom.xml

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 <!-- 配置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>
applicationContext-dao.xml

该配置文件主要是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>
applicationContext-trans.xml

该配置文件主要配置事务以及事务管理和切面(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>
applicationContext-service.xml

该配置文件主要是用来扫描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-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>
web.xml

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 <!-- 是否去除自动生成的注释 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>
generatorConfig.xml

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  * <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 }

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  * <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、创建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  * <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 }

 

原创作品,侵权必究!
Email:woo0nise@gamail.com
交流QQ群:193306983

转载于:https://www.cnblogs.com/0nise/p/6323420.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值