@[TOC](Mybatis plus无介绍快使用,枚举变量的使用附源码(七))
问题背景
因为mybatis plus非常的流行,虽然平常mybatis generator也够用了,但多会一个,看别人的代码就轻松一点
注意事项:
- 官方网站:https://baomidou.com/
- 官方文档:https://baomidou.com/pages/24112f/
- 可以自己创建工程,也可以下载源码进行参考
-
MyBatis-Plus在实现插入数据时,会默认基于雪花算法的策略生成id,实体类entity属性都使用对象,使用Long,不能使用long,不然雪花算法会失效
Mybatis-plus无介绍快使用,CRUD增删改查基本使用附源码(一)
Mybatis-plus无介绍快使用,自定义sql语句CRUD增删改查附源码(二)
Mybatis-plus无介绍快使用,自带封装service层的使用附源码(三)
Mybatis-plus无介绍快使用,注解的使用(四)
Mybatis-plus无介绍快使用,Wrapper条件构造器的使用附源码(五)
Mybatis-plus无介绍快使用,分页插件和乐观锁插件的使用附源码(六)
Mybatis-plus无介绍快使用,枚举变量的使用附源码(七)
Mybatis-plus无介绍快使用,多数据源的使用(八)
Mybatis-plus无介绍快使用,MybatisX自动生成代码插件的使用(九)
Mybatis-plus无介绍快使用,可继承通用的基础实体类(十)
通用枚举
表中的有些字段值是固定的,例如性别(男或女),此时我们可以使用MyBatis-Plus的通用枚举来实现
- 数据库表添加字段
sex
- 创建通用枚举类型
@Getter
public enum SexEnum {
MALE(1, "男"),
FEMALE(2, "女");
@EnumValue //将注解所标识的属性的值存储到数据库中
private int sex;
private String sexName;
SexEnum(Integer sex, String sexName) {
this.sex = sex;
this.sexName = sexName;
}
}
- User实体类中添加属性sex
public class User {
private Long id;
@TableField("username")
private String name;
private Integer age;
private String email;
private SexEnum sex;
}
- 配置扫描通用枚举
#MyBatis-Plus相关配置
mybatis-plus:
#指定mapper文件所在的地址
mapper-locations: classpath:mapper/*.xml
configuration:
#配置日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
banner: off
db-config:
#配置mp的主键策略为自增
id-type: auto
# 设置实体类所对应的表的统一前缀
table-prefix: t_
#配置类型别名所对应的包
type-aliases-package: com.atguigu.mybatisplus.pojo
# 扫描通用枚举的包
type-enums-package: com.atguigu.mybatisplus.enums
- 执行测试方法
@Test
public void test(){
User user = new User();
user.setName("admin");
user.setAge(33);
user.setSex(SexEnum.MALE);
int result = userMapper.insert(user);
System.out.println("result:"+result);
}
2 项目目录
作为程序员第 155 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha ...