整合测试 1、导入 MyBatis 所需要的依赖
1 2 3 4 5 <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 2.1.4</version > </dependency >
2、配置数据库连接信息
1 2 3 4 5 6 7 8 spring.datasource.username =root spring.datasource.password =root spring.datasource.url =jdbc:mysql://localhost:3306/login?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 spring.datasource.driver-class-name =com.mysql.cj.jdbc.Driver mybatis.type-aliases-package =com.lsh.pojo mybatis.mapper-locations =classpath:mybatis/Mapper/*.xml
3、测试数据库是否连接成功!
4、创建实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.lsh.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data @NoArgsConstructor @AllArgsConstructor public class Department { private Integer id; private String departmentName; }
5、创建mapper目录以及对应的 Mapper 接口
1 2 3 4 5 6 7 8 9 10 11 12 @Mapper @Repository public interface DepartmentMapper { List<Department> getDepartments () ; Department getDepartment (Integer id) ; }
6、对应的Mapper映射文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="com.lsh.mapper.DepartmentMapper" > <select id ="getDepartments" resultType ="Department" > select * from department; </select > <select id ="getDepartment" resultType ="Department" parameterType ="int" > select * from department where id = #{id}; </select > </mapper >
7、maven配置资源过滤问题
1 2 3 4 5 6 7 8 9 <resources > <resource > <directory > src/main/java</directory > <includes > <include > **/*.xml</include > </includes > <filtering > true</filtering > </resource > </resources >
8、编写部门的 DepartmentController 进行测试!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @RestController public class DepartmentController { @Autowired DepartmentMapper departmentMapper; @GetMapping("/getDepartments") public List<Department> getDepartments () { return departmentMapper.getDepartments(); } @GetMapping("/getDepartment/{id}") public Department getDepartment (@PathVariable("id") Integer id) { return departmentMapper.getDepartment(id); } }
启动项目访问进行测试!
我们增加一个员工类再测试下 1、新建一个pojo类 Employee ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Data @AllArgsConstructor @NoArgsConstructor public class Employee { private Integer id; private String lastName; private String email; private Integer gender; private Integer department; private Date birth; private Department eDepartment; }
2、新建一个 EmployeeMapper 接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Mapper @Repository public interface EmployeeMapper { List<Employee> getEmployees () ; int save (Employee employee) ; Employee get (Integer id) ; int delete (Integer id) ; }
3、编写 EmployeeMapper.xml 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="com.lsh.mapper.EmployeeMapper" > <resultMap id ="EmployeeMap" type ="Employee" > <id property ="id" column ="eid" /> <result property ="lastName" column ="last_name" /> <result property ="email" column ="email" /> <result property ="gender" column ="gender" /> <result property ="birth" column ="birth" /> <association property ="eDepartment" javaType ="Department" > <id property ="id" column ="did" /> <result property ="departmentName" column ="dname" /> </association > </resultMap > <select id ="getEmployees" resultMap ="EmployeeMap" > select e.id as eid,last_name,email,gender,birth,d.id as did,d.department_name as dname from department d,employee e where d.id = e.department </select > <insert id ="save" parameterType ="Employee" > insert into employee (last_name,email,gender,department,birth) values (#{lastName},#{email},#{gender},#{department},#{birth}); </insert > <select id ="get" resultType ="Employee" > select * from employee where id = #{id} </select > <delete id ="delete" parameterType ="int" > delete from employee where id = #{id} </delete > </mapper >
4、编写EmployeeController类进行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 @RestController public class EmployeeController { @Autowired EmployeeMapper employeeMapper; @GetMapping("/getEmployees") public List<Employee> getEmployees () { return employeeMapper.getEmployees(); } @GetMapping("/save") public int save () { Employee employee = new Employee(); employee.setLastName("lisihan" ); employee.setEmail("lisihan@qq.com" ); employee.setGender(1 ); employee.setDepartment(101 ); employee.setBirth(new Date()); return employeeMapper.save(employee); } @GetMapping("/get/{id}") public Employee get (@PathVariable("id") Integer id) { return employeeMapper.get(id); } @GetMapping("/delete/{id}") public int delete (@PathVariable("id") Integer id) { return employeeMapper.delete(id); } }