1.0 MyBatis-Plus之枚举
CREATE TABLE `t_user` ( `uid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `user_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名', `age` int(11) NULL DEFAULT NULL COMMENT '年龄', `email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱', `is_delect` int(255) NULL DEFAULT 0 COMMENT '是否删除', `sex` int(255) NULL DEFAULT NULL COMMENT '性别', PRIMARY KEY (`uid`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 16 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
添加性别
表中的有些字段值是固定的,例如性别(男或女),此时我们可以使用MyBatis-Plus的通用枚举来实现
创建枚举 类
package com.example.enums; import com.baomidou.mybatisplus.annotation.EnumValue; import lombok.Getter; @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; } }
package com.example.pojo; import com.baomidou.mybatisplus.annotation.*; import com.example.enums.SexEnum; import lombok.*; /** * */ //get/set 有参无参 @Data=get+set@| Setter+@Getter @Data @AllArgsConstructor @NoArgsConstructor public class User { //我们使用的是数据库自增 @TableId(value="uid",type= IdType.AUTO) private Long id; @TableField("user_name") private String name; private Integer age; private String email; @TableLogic private int isDelect; private SexEnum sexEnum; }
配置扫描通用枚举
spring: # 配置数据源信息 datasource: #配置数据源类型 type: com.zaxxer.hikari.HikariDataSource #配置连接数据的信息 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&userSSL=false username: root password: root mybatis-plus: configuration: #加入mybatis 日志查看执行语句sql语句 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: table-prefix: t_ #设置统一的主键生成策略 id-type: auto #配置实体类类型别名对应的包(也就是实体类和mapper.xml的返回的对象对应) type-aliases-package: com.example.pojo # 扫描通用枚举的包 type-enums-package: com.example.enums
创建测试类:
package com.example; import com.example.enums.SexEnum; import com.example.mapper.UserMapper; import com.example.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class MyBatisPlusEnumTest { @Autowired private UserMapper userMapper; @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); } }
==> Preparing: INSERT INTO t_user ( user_name, age, is_delect, sex ) VALUES ( ?, ?, ?, ? ) ==> Parameters: admin(String), 33(Integer), 0(Integer), 1(Integer) <== Updates: 1
枚举的学习就到这里结束;大家也会说上班可能用不到;
但是正常规范的代码,例如状态码,数据字典等相关都需要类似接口来实现得到;
补充下:有人提问我这边回复下内容
下面的2个方法即可获取;
@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); System.out.println("user的值==》"+user); System.out.println("user中性别的值==》"+user.getSex().getSexName()); }