MyBatis 是一个流行的 Java 持久层框架,它简化了与关系型数据库的交互。通过将 SQL 语句与 Java 代码进行映射,MyBatis 提供了一种方便、灵活的方式来执行数据库操作。它支持动态SQL、缓存机制和插件扩展,使得开发人员能够更高效地编写和管理数据库访问代码。作为一种轻量级框架,MyBatis 在 Java 开发中被广泛应用于构建可靠的持久化解决方案。
本文将会指导你如何在 Spring Boot 中整合 MyBatis。
一、注解实现:
1.导入mybatis坐标
<!-- mybatis坐标 -->
<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>2.2.2</version>
</dependency>
<!-- mysql -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.28</version>
</dependency> 
2.创建application.yml文件

在其中写入连接数据库信息

3.创建映射数据库表数据的实体类

4.创建数据访问层-接口

5.使用@mapper或@mapperscan注解
1、@Mapper注解:
 作用:在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类
 添加位置:接口类上面
 
@Mapper
 public interface Accountmapper {
 //代码
 }
2、@MapperScan
 作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
 添加位置:是在Springboot启动类上面添加,
 
@SpringBootApplication
 @MapperScan(basePackages = "com.apesourse.mapper")
 public class SpringbootMybatisDemoApplication {
public static void main(String[] args) {
 SpringApplication.run(SpringbootMybatisDemoApplication.class, args);
 }
 }
6.在数据访问层接口写入方法,用注解写入SQL语句
//@Mapper
public interface Accountmapper{
    @Select("select * from account")
    public List<Account> findAll();
} 
7.在测试类中测试
用@Autowired注解注入属性
@SpringBootTest
class MybatisSpringbootApplicationTests {
    @Autowired(required = false)
    Accountmapper accountmapper;
    @Test
    void setAccountmapper(){
        List<Account> all = accountmapper.findAll();
        for(Account account:all){
            System.out.println(account);
        }
    } 
二、Xml实现:
1.和注解一样搭配环境
2.在resources下创建xml文件,并在yml文件中将配置路径下的*.xml文件加载到mybatis中


3在数据访问层接口写入方法
//@Mapper
public interface Accountmapper{
    public List<Account> find();
} 
4.在xml文件中注入类和方法并书写SQL语句
接口中方法名必须与其对应的id一致
<?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.apesourse.mapper.Accountmapper">
  <select id="find" resultType="com.apesourse.pojo.Account">
    select * from account;
  </select>
</mapper> 
5.测试
@SpringBootTest
class MybatisSpringbootApplicationTests {
    @Autowired(required = false)
    Accountmapper accountmapper;
    @Test
    void contextLoads() {
        List<Account> all1 = accountmapper.find();
        for(Account account:all1){
            System.out.println(account);
        }
    }
} 
三、Springboot使用Mybatisplus:BaseMapper与IService
BaseMapper :
MyBatis-Plus 的核心类 BaseMapper 主要是用于提供基本的 CRUD(创建、读取、更新、删除)操作的接口定义。它是 MyBatis-Plus 框架中的一个重要组成部分,可以大大简化基于 MyBatis 的数据访问层代码的编写。
BaseMapper 接口通常定义了一些基本的数据库操作方法,如下:
  
public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);
    int deleteById(Serializable id);
    int deleteByMap(@Param("cm") Map<String, Object> columnMap);
    int delete(@Param("ew") Wrapper<T> queryWrapper);
    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
    int updateById(@Param("et") T entity);
    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
    T selectById(Serializable id);
    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
    T selectOne(@Param("ew") Wrapper<T> queryWrapper);
    Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
    List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
    <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
} 
实现步骤:
1.导入Mybatisplus坐标
<dependencies>
  <!-- mybatis-plus坐标 -->
  <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3</version>
  </dependency>
  <!-- mysql 相关连接-->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
  </dependency>
<!--        springboot相关开始-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
  <!--        springboot相关结束-->
</dependencies> 
2.数据访问层接口继承BaseMapper类
BaseMapper<T>泛型中使用映射数据库表数据的实体类
public interface Accountmapper extends BaseMapper<Account> {
} 
3.写一个配置类注入拦截器
为了简化处理,可以在sql执行的时候加入一个拦截器,并对将要执行的sql进行统一的处理。
@Configuration
public class MyBatisPlusConfig {
    //注入mp拦截器
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //1.实例化拦截器
        MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();
        //2.分页拦截器
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }
} 
4.Test类相关实现
@SpringBootTest
public class Test01 {
    @Autowired(required = false)
    Accountmapper accountmapper;
    //新增
    @Test
    public void show1(){
        Account account=new Account("韩佳瑶ppplus",999999);
        int insert = accountmapper.insert(account);
        System.out.println(insert);
    }
    //多个条件删除多个
    @Test
    public void delmany(){
        int i = accountmapper.deleteBatchIds(Arrays.asList("1","2","3"));
    }
    //分组
    @Test
    public void countshow(){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("amoney",80000000);
        Integer integer = accountmapper.selectCount(queryWrapper);
        System.out.println(integer);
    }
    //按id修改
    @Test
    public void updatebyid(){
        int i = accountmapper.updateById(new Account(6,"zyt",200));
        System.out.println(i);
    }
    //按条件修改
    @Test
    public void updateshow(){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("aid",3);
        Account account=new Account("赵依婷",99999999);
        int update = accountmapper.update(account,queryWrapper);
        System.out.println(update);
    }
    //删除
    @Test
    public void delbyid(){
        int i = accountmapper.deleteById(2);
        System.out.println(i);
    }
    //分页
    @Test
    public void limitshow2(){
        Page<Account> page=new Page<Account>();
        page.setSize(3);//每页记录数
        page.setCurrent(2);//当前页码
        QueryWrapper queryWrapper=new QueryWrapper();
        IPage<Account> accountIPage = accountmapper.selectPage(page,null);
        List<Account> list = accountIPage.getRecords();//分页结果
        System.out.println("总记录数"+accountIPage.getTotal());
        System.out.println("总页数"+accountIPage.getPages());
        for(Account account:list){
            System.out.println(account);
        }
    }
} 
                


















