SpringBoot:整合MyBatis

我是 ABin-阿斌:写一生代码,创一世佳话,筑一揽芳华。 如果小伙伴们觉得我的文章有点 feel ,那就点个赞再走哦。
在这里插入图片描述

SpringBoot其它两大整合:

更多整合可关注首页 SpringBoot分栏:SpringBoot大整合

  1. 整合 JDBC
  2. 整合 Druid 数据源

一、 前言

  当大家进入到这篇文章的时候我相信已经对 MyBatis 以及如何使用 MyBatis 有一个全面的了解了,那么这里我就不在阐述如何使用 MyBatis,直接进入整合主题。

二、相关介绍

  • SpringBoot 整合MyBatis的官方文档地址: http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
  • 相关依赖Maven仓库地址: https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter/2.1.1

三、整合测试

3.1、建立一个新的模块,导入我们所需的一个 MyBatis 依赖

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

3.2、配置数据库连接信息

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

3.3、编写测试用例,查看数据库是否连接成功

@SpringBootTest
class Springboot05MybatisApplicationTests {
    @Autowired
    private DataSource dataSource;

    @Test
    void contextLoads() throws SQLException {
        //查看默认数据源:com.zaxxer.hikari.HikariDataSource(目前速度最快的)
        System.out.println(dataSource.getClass());

        //获取数据库连接
        Connection connection = dataSource.getConnection();
        System.out.println("数据库连接信息:" + connection);

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

3.4、创建实体类,导入 Lombok

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dept {
    /**
     * 部门编号
     */
    private Integer id;
    /**
     * 部门名称
     */
    private String  dname;
    /**
     * 部门地址
     */
    private String loc;

}

3.4、创建 mapper 目录以及对应的 Mapper 接口

/**
 * @Mapper:该注解表示本类是一个 Mybatis 的 Mapper类
 */
@Repository
@Mapper
public interface DeptMapper {
    /**
     *  获取所有部门信息
     */
    List<Dept> getDeptList();

    /**
     *  添加部门信息
     * @Param: [dept]
     * @return: int
     */
    int saveDept(Dept dept);

    /**
     *  通过ID查询部门信息
     * @Param: [id]
     * @return: com.abin.pojo.Dept
     */
    Dept queryDeptById(Integer id);

    /**
     *  通过ID删除部门信息
     * @Param: [id]
     * @return: int
     */
    int deleteDept(Integer id);

    /**
     * 修改部门信息
     * @Param: [dept]
     * @return: int
     */
    int updateDept(Dept dept);
}

3.5、对应的 Mapper 映射文件

<?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.abin.mapper.DeptMapper">

    <select id="getDeptList" resultType="Dept">
        select * from Dept
    </select>

    <insert id="saveDept" parameterType="Dept">
        insert into dept (id,dname,loc) values(#{id},#{dname},#{loc});
    </insert>

    <select id="queryDeptById" resultType="Dept">
        select * from Dept where id = #{id}
    </select>

    <delete id="deleteDept" parameterType="Dept">
        delete from dept where id = #{id}
    </delete>

    <update id="updateDept" parameterType="Dept">
        update dept set dname = #{dname},loc = #{loc} where id = #{id}
    </update>

</mapper>

3.6、maven 配置资源过滤问题

<!--在 pom.xml的<build> 标签中加上即可-->
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
    </resource>
</resources>

3.7、编写部门的 DaptController 进行测试

@RestController
public class DeptController {
    @Autowired
    private DeptMapper deptMapper;

    @GetMapping("/getDeptList")
    public List<Dept> getDeptList() {
        List<Dept> deptList = deptMapper.getDeptList();
        for (Dept dept : deptList) {
            System.out.println(dept);
        }
        return deptList;
    }

    /**
     * 添加部门
     * @return: java.lang.String
     */
    @GetMapping("/save")
    public String saveDept(){
        deptMapper.saveDept(new Dept(60,"技术部","上海"));
        return "添加部门成功!";
    }

    /**
     * 根据 id 删除部门
     * @Param: [id]
     * @return: java.lang.String
     */
    @GetMapping("/clear")
    public String deleteDept(){
        deptMapper.deleteDept(60);
        return "删除部门成功!";
    }

    /**
     * 通过ID查询部门信息
     * @Param: [id]
     * @return: java.lang.String
     */
    @GetMapping("/query/{id}")
    public Dept queryDeptById(@PathVariable("id") Integer id){
        Dept dept = deptMapper.queryDeptById(id);
        return dept;
    }

    /**
     * 修改部门信息
     * @Param: []
     * @return: java.lang.String
     */
    @GetMapping("/updateDept")
    public String updateDept(){
        deptMapper.updateDept(new Dept(60,"运营部","杭州"));
        return "修改部门表数据成功!";
    }
}

总结

  最后直接拿着测试类的 path 去测试就可以,如果在测试的过程中有问题可以在评论下方回复。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值