详细笔记的第一遍:学习ssm的整合-CRUD的第2天(2021-11-22)1
现在自动生成的一些mapper.xml文件有些需要进行修改。
1、目前查询员工表的信息的时候,他只会有员工的一些字段,而没有关联的部门信息。
所以我们需要自己建一个方法,来进行联合查询。
在EmployeeMapper.java文件里面再添加两个方法:
public interface EmployeeMapper {
List<Employee> selectByExampleWithDept(EmployeeExample example);
Employee selectByPrimaryKeyWithDept(Integer empId);
}
2、修改Employee.java
添加属性Department
再添加get/set方法。
3、 在EmployeeMapper.xml里面写两个方法对应的查询sql
selectByExampleWithDept
和
selectByPrimaryKeyWithDept
所以,目前我们提供了
查询员工信息的同时不带部门信息和
查询员工信息的同时带上部门信息
EmployeeMapper.xml文件中多加的内容:
1、自定义了resultMap ,除了Employee bean里面的基本类型的属性,还使用association 添加了引用类型属性Department
<resultMap id="WithDeptResultMap" type="com.rtl.crud.bean.Employee">
<id column="emp_id" jdbcType="INTEGER" property="empId" />
<result column="emp_name" jdbcType="VARCHAR" property="empName" />
<result column="gender" jdbcType="CHAR" property="gender" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="d_id" jdbcType="INTEGER" property="dId" />
<association property="department" javaType="com.rtl.crud.bean.Department">
<id column="id" property="id"></id>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap>
2、sql别名。之前只有基本Employee,现在多加了两个部门里面的属性。
<sql id="WithDept_Column_List">
e.emp_id, e.emp_name, e.gender, e.email, e.d_id, d.id, d.dept_name
</sql>
3、多了两个最重要的select标签。
selectByExampleWithDept和selectByPrimaryKeyWithDept
<select id="selectByExampleWithDept" parameterType="com.rtl.crud.bean.EmployeeExample" resultMap="WithDeptResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="WithDept_Column_List" />
FROM tbl_emp e
LEFT JOIN tbl_dept d
ON e.emp_id = d.id
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKeyWithDept" parameterType="java.lang.Integer" resultMap="WithDeptResultMap">
select
<include refid="WithDept_Column_List" />
FROM tbl_emp e
LEFT JOIN tbl_dept d
ON e.emp_id = d.id
where emp_id = #{empId,jdbcType=INTEGER}
</select>
11、搭建Spring的单元测试环境
我们推荐,在Spring项目里面,就可以使用Spring的单元测试,这样可以自动注入我们需要的组件。
1、在pom文件里面导入spring单元测试的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
目前项目的pom文件
<?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>com.rtl</groupId>
<artifactId>ssm-crud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
</dependencies>
</project>
2、new 一个类 MapperTest
并且在这个类上面写上注解:@ContextConfiguration
写上spring的配置文件的存放路径
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
注意:之前引入的juint的范围都写得是test
现在改一下:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
再使用另外一个注解@RunWith
@RunWith(SpringJUnit4ClassRunner.class)
接下来直接写@Autowired就可以自动注入了
而不需要再写:
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
DepartmentMapper departmentMapper = ioc.getBean(DepartmentMapper.class);
这样的代码才能获取对象。
直接使用注解@Autowired即可。
好了,基本的搭建Spring的测试框架:
整个测试类MapperTest:
package com.rtl.crud.test;
import com.rtl.crud.dao.DepartmentMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"} )
public class MapperTest {
@Autowired
DepartmentMapper departmentMapper;
//测试DepartmentMapper
@Test
public void testCRUD(){
System.out.println(departmentMapper);
}
}
这里经历了一个困难:
就是生成的mapper.xml文件,会出现重复id的情况。
检测原因是mybatis的使用版本过低。
但是把mybatis的版本提高,又报另一个错误。
Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.setDefaultEnumTypeHandler(Ljava/lang/Class;)V
最终的pom里面的mybatis版本:
mybatis-spring 1.3.1
mybatis-generator-core 1.3.7
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
测试类:
可以通过搭建spring的单元测试环境,而不需要使用new ClassPathXmlApplicationContext来获取对象。
测试成功!!!
最终的pom文件:
<?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>com.rtl</groupId>
<artifactId>ssm-crud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
</dependencies>
</project>
开始测试:插入数据到部门表
注意:
因为我们部门表里面的id字段是自增属性
所以我们在插入数据的时候,id这个字段写为Null
1、先给Department类里面加上无参和有参构造器。
2、再给测试类里面写插入数据的代码。
所以,往表tbl_dept里面插入数据成功。
测试插入员工表的数据
1、加上构造器
2、测试类:
测试插入数据到员工表成功!!!
批量插入多个员工信息。
11