mybatis-plus基础使用
mybatis-plus基础使用
引入依赖
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.5'
// https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter
implementation("com.baomidou:mybatis-plus-boot-starter:3.5.14")
基础配置
# mybatis-plus
# 自动给这个包下的实体类生成别名 例如 User -> user
# 可以在 MyBatis 的 XML 映射文件中直接使用这些别名,而不需要全限定类名 等同于 @Alias("user")
mybatis-plus.type-aliases-package: com.example.springboottest.entity
# mybatis xml文件地址
mybatis-plus.mapper-locations: classpath:mapper/*.xml
# mybatis 开启驼峰转下划线
mybatis-plus.configuration.map-underscore-to-camel-case: true
# 开启 MyBatis-Plus SQL 打印日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
实体类常用注解
@TableName("user") // 表名
public class User {
@TableId(type = IdType.AUTO) // 自增ID
private Integer id;
@TableField(value = "username") // 数据库中字段名为username
private String name;
private String phoneNumber;
// 乐观锁字段 设置之后每次更新自动+1
@Version
private Integer version;
// 逻辑删除字段 默认为0
//@TableLogic
private Integer deleted;
// 创建时间字段 默认填充当前时间
@TableField(fill = FieldFill.INSERT,update = "now()")
private Date createTime;
// 更新时间字段 默认填充当前时间
@TableField(fill = FieldFill.INSERT_UPDATE,update = "now()")
private Date updateTime;
}
创建Mapper并实现BaseMapper<T>
-
泛型T为实体类的类型 例如User
-
BaseMapper默认实现一些常用的增删改查方法,具体使用时可以点进去看看

创建Service并继承Iservice接口
-
service接口继承Iservice<实体类>接口,service实现继承ServiceImpl<Mapper类名, 实体类名> 继承的service中也提供了一些常用方法,一些批量接口添加了@Transactional注解
public interface UserService extends IService<User> { } @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { } -
业务一些简单增删改查直接使用提供的默认实现就行,类似于JPA那种
自动填充
- 在需要自动填充的实体类字段上添加@TableField(fill = FieldFill.INSERT)注解
@TableField(fill = FieldFill.INSERT)
private Date createTime;
// FieldFill的取值
public enum FieldFill {
/**
* 默认不处理
*/
DEFAULT,
/**
* 插入时填充字段
*/
INSERT,
/**
* 更新时填充字段
*/
UPDATE,
/**
* 插入和更新时填充字段
*/
INSERT_UPDATE
}
- 继承MetaObjectHandler接口,实现insertFill与updateFill方法
@Component
public class UserMapperHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
// 插入时进行填充
this.strictInsertFill(metaObject,"createTime", Date.class,new Date());
this.strictInsertFill(metaObject,"updateTime", Date.class,new Date());
}
@Override
public void updateFill(MetaObject metaObject) {
// 数据更新时进行填充
this.strictUpdateFill(metaObject,"updateTime", Date.class,new Date());
}
}
使用Wrapper(条件构建器)构建一些复杂的查询条件
基础使用
-
详细可以查看 https://blog.csdn.net/qq_57309855/article/details/139853281
-
eq,ge,gt,like in,notIn,isNull,IsNotNull这些比较简单
User user = initUser();
LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>();
// 构建一些复杂的查询条件
// SELECT username AS name,password FROM user WHERE (username = ? AND password LIKE ? AND id >= ? AND address IS NOT NULL)
query.eq(User::getName, "zhangsan")
.like(User::getPassword,"%23456%")
.ge(User::getId, 0)
.select(User::getName, User::getPassword)
.isNotNull(User::getAddress);
// 更新
LambdaUpdateWrapper<User> update = new LambdaUpdateWrapper<>();
update.set(User::getName, "张三")
.set(User::getPassword,"aassasas")
.set(User::getEmail, "hhhh@test.com")
.eq(User::getId, 1);
- AND OR 构建一些复杂条件 and()or ()or()….
- inSql,eqSql,leSql.....这些带sql的用于构建子查询

浙公网安备 33010602011771号