之前介绍了 SpringBoot 整合 Mybatis 实现数据库的增删改查操作,分别给出了 xml 和注解两种实现 mapper 接口的方式;虽然注解方式干掉了 xml 文件,但是使用起来并不优雅,本文将介绍 mybats-plus 的常用实例,简化常规的 CRUD 操作。
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
学习 mybatis-plus:https://mp.baomidou.com/guide
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 热部署模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
# spring setting
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: zwqh@0258
@TableName(value="t_user")
public class UserEntity {
@TableId(value="id",type=IdType.AUTO)
private Long id;
private String userName;
private String userSex;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
}
@TableName 指定数据库表名,否则默认查询表会指向 user_entity ;@TableId(value=”id”,type=IdType.AUTO) 指定数据库主键,否则会报错。
继承 BaseMapper,T表示对应实体类
public interface UserDao extends BaseMapper<UserEntity>{
}
在启动类添加 @MapperScan 就不用再 UserDao 上用 @Mapper 注解。
@SpringBootApplication
@MapperScan("cn.zwqh.springboot.dao")
public class SpringBootMybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisPlusApplication.class, args);
}
}
@Configuration
public class MybatisPlusConfig {
/**
* mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor page = new PaginationInterceptor();
page.setDialectType("mysql");
return page;
}
}
UserEntity user=new UserEntity();
user.setUserName("朝雾轻寒");
user.setUserSex("男");
userDao.insert(user);
UserEntity user=new UserEntity();
user.setUserName("朝雾轻晓");
user.setUserSex("男");
user.setId(25L);
userDao.updateById(user);
UserEntity user=new UserEntity();
user.setUserSex("女");
userDao.update(user,new QueryWrapper<UserEntity>().eq("user_name", "朝雾轻寒"));
UserEntity user = userDao.selectById(id);
int count = userDao.selectCount(new QueryWrapper<UserEntity>().eq("user_sex", "男"));
QueryWrapper<UserEntity> queryWrapper=new QueryWrapper<UserEntity>();
UserEntity user=new UserEntity();
user.setUserName("朝雾轻寒");
user.setUserSex("男");
queryWrapper.setEntity(user);
user = userDao.selectOne(queryWrapper);
如果表内有两条或以上的相同数据则会报错,可以用来判断某类数据是否已存在 根据entity条件查询返回第一个字段的值(返回id列表)
QueryWrapper<UserEntity> queryWrapper=new QueryWrapper<UserEntity>();
UserEntity user=new UserEntity();
user.setUserSex("男");
queryWrapper.setEntity(user);
List<Object> objs= userDao.selectObjs(queryWrapper);
Map<String, Object> map=new HashMap<String, Object>();
map.put("user_name", username);
map.put("user_sex",sex);
List<UserEntity> list = userDao.selectByMap(map);
Map<String, Object> map=new HashMap<String, Object>();
map.put("user_sex","男");
List<UserEntity> list = userDao.selectList(new QueryWrapper<UserEntity>().allEq(map));
Map<String, Object> map=new HashMap<String, Object>();
map.put("user_sex","男");
List<Map<String, Object>> list = userDao.selectMaps(new QueryWrapper<UserEntity>().allEq(map));
List<Long> ids=new ArrayList<Long>();
ids.add(1L);
ids.add(2L);
ids.add(3L);
List<UserEntity> list = userDao.selectBatchIds(ids);
主键ID列表(不能为 null 以及 empty)
Page<UserEntity> page=userDao.selectPage(new Page<>(1,5), new QueryWrapper<UserEntity>().eq("user_sex", "男"));
Page<Map<String, Object>> page=userDao.selectMapsPage(new Page<>(1,5), new QueryWrapper<UserEntity>().eq("user_sex", "男"));
需先配置分页插件bean,否则分页无效。如有pagehelper需先去除,以免冲突。 new Page<>(1,5),1表示当前页,5表示页面大小。
userDao.deleteById(1);
userDao.delete(new QueryWrapper<UserEntity>().eq("id", 1));
Map<String, Object> map=new HashMap<String, Object>();
map.put("user_name", "zwqh");
map.put("user_sex","男");
userDao.deleteByMap(map);
List<Long> ids=new ArrayList<Long>();
ids.add(1L);
ids.add(2L);
ids.add(3L);
userDao.deleteBatchIds(ids);
主键ID列表(不能为 null 以及 empty)
本文介绍了 mybatis-plus 相关的 Mapper层 CRUD 接口实现,其还提供了 Service层 CRUD 的相关接口,有兴趣的小伙伴可以去使用下。mybatis-plus 真正地提升了撸码效率。
其他学习要点:
学习地址:https://mp.baomidou.com/guide/
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有