springboot mybatis-plus 集成 【springboot系列5】-爱代码爱编程
SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见。
程序员每天的CV 与 板砖,也要知其所以然,本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCloud 微服务系列项目开发
1 项目准备
创建SpringBoot基础项目
SpringBoot项目集成mybatis
SpringBoot 集成 Druid 数据源【SpringBoot系列3】
SpringBoot MyBatis 实现分页查询数据【SpringBoot系列4】
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;
}
}
项目源码在这里 :https://gitee.com/android.long/spring-boot-study/tree/master/biglead-api-05-mybatis-plus
有兴趣可以关注一下公众号:biglead