本文主要是介绍SSM整合篇】三. SSM整合+事务+单元测试案例 第一章(共四章),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Spring+SpringMvc+MyBatis整合+事务+单元测试案例 第一章
github源码(day56-ssm-transaction)https://github.com/1196557363/ideaMavenProject
对一些知识点不懂可以参考 SSM整合篇】一. Spring+SpringMvc+MyBatis简单案例
1. pom.xml导依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>ssm-transaction</groupId><artifactId>day56-ssm-transaction</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>day56-ssm-transaction Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><!-- Spring版本号 --><spring.version>5.1.5.RELEASE</spring.version></properties><dependencies><!-- 连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.18</version></dependency><!-- Spring相关包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><!-- AOP相关包 --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.8.0</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.0</version></dependency><!-- MyBatis相关包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.3.0</version></dependency><!-- MySQL相关包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.26</version></dependency><!-- Spring集成MyBatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.2.3</version></dependency><!-- 日志相关包 --><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.5</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.5</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-web</artifactId><version>2.5</version></dependency><!--解决Spring使用slf4j输出日志与log4j冲突的问题--><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.13</version></dependency><!-- 单元测试相关包 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.10</version></dependency><!-- 导入thymeleaf依赖包--><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf</artifactId><version>3.0.11.RELEASE</version></dependency><!-- thymeleaf和Spring整合的依赖--><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring5</artifactId><version>3.0.11.RELEASE</version></dependency></dependencies><build><finalName>day56-ssm-transaction</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>
2 创建数据库 day56-ssm-transaction
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (`dept_id` int(11) NOT NULL,`dept_name` varchar(14) DEFAULT NULL,`dept_addr` varchar(13) DEFAULT NULL,PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `dept` VALUES ('1', '高层部门', '深圳');
INSERT INTO `dept` VALUES ('2', '基层部门', '广州');
INSERT INTO `dept` VALUES ('3', '技术部门', '汕尾');
INSERT INTO `dept` VALUES ('4', '销售部门', '汕头');DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (`emp_id` int(4) NOT NULL,`emp_name` varchar(10) DEFAULT NULL,`job` varchar(11) DEFAULT NULL,`super_id` int(4) DEFAULT NULL,`dept_no` int(11) DEFAULT NULL,PRIMARY KEY (`emp_id`),KEY `FK_DEPT_ID` (`dept_no`),CONSTRAINT `FK_DEPT_ID` FOREIGN KEY (`dept_no`) REFERENCES `dept` (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `emp` VALUES ('1', 'jieKaMi', '董事长', 0, '1');
INSERT INTO `emp` VALUES ('2', 'wpj', '经理', '1', '1');
INSERT INTO `emp` VALUES ('3', 'czh', '副经理', '2', '1');
INSERT INTO `emp` VALUES ('4', 'yzh', '经理助理', '2', '1');
INSERT INTO `emp` VALUES ('5', 'wcw', '分公司经理', '2', '2');
INSERT INTO `emp` VALUES ('6', 'zcy', '主管', '5', '2');
INSERT INTO `emp` VALUES ('7', 'hcm', '项目负责人', '5', '2');
INSERT INTO `emp` VALUES ('8', 'lgx', '审核', '5', '3');
INSERT INTO `emp` VALUES ('9', 'zmy', '财务', '5', '3');
INSERT INTO `emp` VALUES ('10', 'oysc', '后勤', '5', '4');
3 创建jdbc.properties 和 spring-context.xml
3.1 jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/day56-ssm-transaction
jdbc.username=root
jdbc.password=123456
3.2 spring-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 1. 加载jdbc.properties --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 2.创建数据源 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><!--基本属性url,user,password--><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><!-- 配合初始化大小, 最大, 最小 --><property name="initialSize" value="5" /><property name="minIdle" value="5" /><property name="maxActive" value="10" /><!-- 配置从连接池获取连接等待超时的时间 --><property name="maxWait" value="10000" /></bean>
</beans>
3.3 Test
没错 就是测试 测试dataSource是否配置正确
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.test.context.*;
import org.springframework.test.context.junit4.*;import javax.sql.*;/*** ClassName: SSMTest* Description:** @author JieKaMi* @version 1.0* @date: 2020\1\8 0008 17:51* @since JDK 1.8*/@RunWith(value= SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:spring-context.xml")
public class SSMTest {@Autowiredprivate DataSource dataSource;@Testpublic void testDataSource(){System.out.println(dataSource);}
}
3.3.1 结果
如果报错了 或者为null 就要检查一下前面哪里写错了 再往下面走。
可能出错的地方
- 检查jdbc.properties看看有没有写错
- spring-context.xml看看 加载jdbc.properties有没有加载到,DataSource有没有配对。
- @ContextConfiguration(value = “classpath:spring-context.xml”) 看看有没有写错有没有写错
4. Spring和MyBatis整合
4.1 定义实体类
可以不先创建,但为配置完善是在这里创建。
4.1.1 Dept
package com.wpj.bean;
/*** ClassName: Dept* Description:* 提供无参,全参,getter,setter和toString()* @author JieKaMi* @version 1.0* @date: 2020\1\8 0008 19:04* @since JDK 1.8*/
public class Dept {private Integer deptId;private String deptName;private String deptAddr;public Dept() {}public Dept(Integer deptId, String deptName, String deptAddr) {this.deptId = deptId;this.deptName = deptName;this.deptAddr = deptAddr;}public Integer getDeptId() { return deptId; }public void setDeptId(Integer deptId) { this.deptId = deptId; }public String getDeptName() { return deptName; }public void setDeptName(String deptName) { this.deptName = deptName; }public String getDeptAddr() { return deptAddr; }public void setDeptAddr(String deptAddr) { this.deptAddr = deptAddr; }@Overridepublic String toString() {return "Dept{" +"deptId=" + deptId +", deptName='" + deptName + '\'' +", deptAddr='" + deptAddr + '\'' +'}';}
}
4.1.2 Emp
package com.wpj.bean;/*** ClassName: Emp* Description:* 提供无参,全参,getter,setter和toString()* @author JieKaMi* @version 1.0* @date: 2020\1\8 0008 19:07* @since JDK 1.8*/
public class Emp {private Integer empId;private String empName;private String job;private Integer superId;private Integer deptNo;public Emp() {}public Emp(Integer empId, String empName, String job, Integer superId, Integer deptNo) {this.empId = empId;this.empName = empName;this.job = job;this.superId = superId;this.deptNo = deptNo;}public Integer getEmpId() { return empId; }public void setEmpId(Integer empId) { this.empId = empId; }public String getEmpName() { return empName; }public void setEmpName(String empName) { this.empName = empName; }public String getJob() { return job; }public void setJob(String job) { this.job = job; }public Integer getSuperId() { return superId; }public void setSuperId(Integer superId) { this.superId = superId; }public Integer getDeptNo() { return deptNo; }public void setDeptNo(Integer deptNo) { this.deptNo = deptNo; }@Overridepublic String toString() {return "Emp{" +"empId=" + empId +", empName='" + empName + '\'' +", job='" + job + '\'' +", superId=" + superId +", deptNo=" + deptNo +'}';}
}
4.2 定义接口
4.2.1 IDeptDao
package com.wpj.dao;/*** ClassName: IDeptDao* Description:** @author JieKaMi* @version 1.0* @date: 2020\1\8 0008 19:12* @since JDK 1.8*/
public interface IDeptDao {}
4.2.2 IEmpDao
package com.wpj.dao;/*** ClassName: IEmpDao* Description:** @author JieKaMi* @version 1.0* @date: 2020\1\8 0008 19:11* @since JDK 1.8*/
public interface IEmpDao {}
4.3 接口对应映射文件(尽可能跟接口同名)
4.3.1 IDeptDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.wpj.dao.IDeptDao"></mapper>
4.3.2 IEmpDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.wpj.dao.IEmpDao"></mapper>
4.4 在spring-context.xml中配置
<!-- 3. Spring跟MyBatis整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 3.1 设置数据源 --><property name="dataSource" ref="dataSource" /><!-- 3.2 设置别名 --><property name="typeAliasesPackage" value="com.wpj.bean" /><!-- 3.3 --><property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
4.5 Test
import org.apache.ibatis.session.*;
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.test.context.*;
import org.springframework.test.context.junit4.*;import javax.sql.*;/*** ClassName: SSMTest* Description:** @author JieKaMi* @version 1.0* @date: 2020\1\8 0008 17:51* @since JDK 1.8*/@RunWith(value= SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:spring-context.xml")
public class SSMTest {@Autowiredprivate SqlSessionFactory sqlSessionFactory;@Testpublic void testSqlSessionFactory(){System.out.println(sqlSessionFactory);}
}
4.5.1 结果
如果报错了 或者为null 就要检查一下前面哪里写错了 再往下面走。
可能出错的地方
- 看看spring-context.xml中 <!-- 3. Spring跟MyBatis整合 –> bean的id和class有没有写错
- 看看 <!-- 3.1 设置数据源 –> 和 <!-- 3.3 mapper文件的路径映射 –> 有没有错,特别是 value=“classpath:mapper/*.xml”
- 至于<!-- 3.2 设置别名 –>没写sql映射的时候还用不到
未完待续。。。。
- 配置事务【SSM整合篇】三. SSM整合+事务+单元测试案例 第二章(共四章)
这篇关于SSM整合篇】三. SSM整合+事务+单元测试案例 第一章(共四章)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!