likes
comments
collection
share

SpringBoot MyBatis-Plus 集成 【SpringBoot系列5】

作者站长头像
站长
· 阅读数 14

SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见。 程序员每天的CV 与 板砖,也要知其所以然,本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCloud 微服务系列项目开发

1 项目准备

官网地址:baomidou.com/

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

在 SpringBoot 项目的 pom.xml 中添加 Mybatis-plus 依赖

<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3.1</version>
</dependency>

2 Mybatis-plus 的基本使用

在项目中的 UserMapper,java 中 继承 BaseMapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.biglead.demo.pojo.UserInfo;
import org.mybatis.spring.annotation.MapperScan;
import java.util.List;

@MapperScan
public interface UserMapper extends BaseMapper<UserInfo> {
    List<UserInfo> selectList();
    
    /**
     * 分页查询用户
     * @return
     */
    List<UserInfo> selectPage();
}

BaseMapper 是 Mybatis-plus 中提供的一个基类,其中封装了基础的增删改查的功能 :

public interface BaseMapper<T> extends Mapper<T> {
    //插入
    int insert(T entity);
    
	//通过主键删除
    int deleteById(Serializable id);
	//通过Map字段对应值删除
    int deleteByMap(@Param("cm") Map<String, Object> columnMap);
	//通过条件Wrapper删除
    int delete(@Param("ew") Wrapper<T> wrapper);
	//批量删除
    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    //更新 通过ID匹配
    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);
	//查询通过Map
    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);
}

然后使用 MyBatis-Plus 可以简便的完成数据的增删改查


@SpringBootTest
class UserServiceTests {

    @Resource
    UserMapper userMapper;

    @Test
    void testInsertData() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName("测试用户");
        userInfo.setUserAge(33);
        //插入数据
        userMapper.insert(userInfo);
    }

}

还可以使 UserServiceImpl 继承 ServiceImpl 来实现便捷的增删改查功能

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserInfo> implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public UserInfo newUser(UserInfo info) {
        //保存用户
        this.save(info);
        return info;
    }
    @Override
    public UserInfo updateUser(UserInfo info) {
        //根据ID来更新用户信息
        this.updateById(info);
        return info;
    }
    @Override
    public UserInfo delete(UserInfo info) {
        //根据ID来删除用户信息
        this.delete(info);
        return info;
    }
  }

特别说明 :

1. 需要在启动类上配置 @MapperScan 扫描包路径
@SpringBootApplication
@MapperScan(basePackages = "com.biglead.demo.mapper")
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

2 application.yml 兼容配置

将 mybatis 的包扫描路径删除 ,使用mybatis-plus 的,比如我这里的

mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
#mybatis
mybatis-plus:
  mapper-locations: classpath*:**/sqlmap/*.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: com.biglead.demo.pojo
  configuration:
    map-underscore-to-camel-case: true
    #缓存
    cache-enabled: false
    call-setters-on-nulls: true
   #配置日志  log-impl:日志实现
    log-impl : org.apache.ibatis.logging.stdout.StdOutImpl

项目源码在这里 :gitee.com/android.lon… 有兴趣可以关注一下公众号:biglead

本文正在参加「金石计划」