Springboot学习笔记7:整合Mybatis

Posted Vincent9847

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot学习笔记7:整合Mybatis相关的知识,希望对你有一定的参考价值。

一、什么是Mybatis?

MyBatis是一款优秀的基于java的持久层框架,它内部封装了jdbc,使开发者只需要关注sql语句本身,而不需要花费精力去处理加载驱动、创建连接、创建statement等繁杂的过程。

二、配置Mybatis

1.导入依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

2.配置数据库信息

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource

3.测试连接

@SpringBootTest
class SpringbootDataJdbcApplicationTests {

    //DI注入数据源
    @Autowired
    DataSource dataSource;

    @Test
    public void contextLoads() throws SQLException {
        //看一下默认数据源
        System.out.println(dataSource.getClass());
        //获得连接
        Connection connection =   dataSource.getConnection();
        System.out.println(connection);

        DruidDataSource druidDataSource = (DruidDataSource) dataSource;

        //关闭连接
        connection.close();
    }
}

三、实际开发编写测试

1.编写pojo类 Employee

@Data
@AllArgsConstructor    //有参构造方法
@NoArgsConstructor    //无参构造方法
public class Employee {

    private Integer id;
    private String lastName;
    private String email;
    //1 male, 0 female
    private Integer gender;
    private Integer department;
    private Date birth;

    private Department eDepartment; // 冗余设计

}

2.编写 EmployeeDao 接口

@Mapper
@Repository    //将类标识为bean
public interface EmployeeMapper {
    // 获取所有员工信息
    List<Employee> getEmployees();
    // 新增一个员工
    int save(Employee employee);
    // 通过id获得员工信息
    Employee get(Integer id);
    // 通过id删除员工
    int delete(Integer id);
}

3.编写 EmployeeMapper.xml  配置文件


<?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.wwz.dao.EmployeeDao">

    <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 类

@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("xiaoyi");
        employee.setEmail("111@qq.com");
        employee.setGender(1);
        employee.setDepartment(101);
        employee.setBirth(new Date());
        return employeeMapper.save(employee);
    }

    // 通过id获得员工信息
    @GetMapping("/get/{id}")
    public Employee get(@PathVariable("id") Integer id){
        return employeeMapper.get(id);
    }

    // 通过id删除员工
    @GetMapping("/delete/{id}")
    public int delete(@PathVariable("id") Integer id){
        return employeeMapper.delete(id);
    }

}

 

以上是关于Springboot学习笔记7:整合Mybatis的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot 学习笔记 - 整合MyBatis

SpringBoot学习笔记整合Mybatis

springboot学习笔记-2 一些常用的配置以及整合mybatis

SpringBoot 学习笔记 -- [spring Boot集成阿里Druid数据源,整合Mybatis搭建一个案例试试]

springboot学习笔记-5 springboot整合shiro

springboot学习笔记-5 springboot整合shiro