SpringBoot整合Mybatis-plus
@TableName注解
表名注解,用于标识实体类对应的表,作用于实体类
autoResultMap属性
autoResultMap: 是否自动构建resultMap,非必填,boolean类型,默认是false
ResultMap是Mybatis中用于映射查询结果的一个重要组件.当设置autoResultMap值为true时, Mybatis-plus会自动根据实体类的字段和数据库表的字段进行映射,生成相应的resultMap
keepGlobalPrefix属性
keepGlobalPrefix属性: 用于指定是否保留全局的表前缀
该属性通常和全局表前缀配置配合使用,以实现更灵活的表名配置
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| ### yml配置全局表前缀 mybatis-plus: global-config: db-config: table-prefix: sys_
@Data @TableName(value = "teacher", keepGlobalPrefix = true) public class TeacherEntity {
@TableId(value = "id", type = IdType.AUTO) private Integer id;
private String name;
private int age; }
-- 建表sql CREATE TABLE `sys_teacher` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `name` varchar(255) DEFAULT NULL COMMENT '客户名称', `age` int(11) DEFAULT NULL COMMENT '客户年龄', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;
|
说明
- 全局表前缀: 在全局配置中通过
table-prefix设置全局表前缀为sys_
- 实体类的
@TableName注解: 在实体类中,通过value属性指定表名为teacher,再通过设置keepGlobalPrefix=true,表示要保留表前缀
- 最终表名;通过
全局表前缀配置和@TableName注解属性配合,最终的表名为sys_teacher
@TableId注解
@TableId: 作用于实体类主键字段;有两个属性
value: 主键字段别名,非必填
type: 指定主键类型,非必填,Eunm类型,默认是IdType.NONE
type属性
IdType
AUTO: 数据表ID自增
NONE: 无状态,该类型为未设置主键类型
INPUT: insert 自行 set 主键值
ASSIGN_ID: 分配 id,(主键类型为numer,long或者integer或者string),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法)
ASSIGN_UUID: 分配 UUID,主键类型为string,使用接口IdentifierGenerator的方法nextUUID
ID_WORKER: 分布式全局唯一 ID,长整型类型
UUID: 32位uuid字符串
ID_WORKER_STR: 分布式全局唯一 ID,字符串类型
@TableField注解
源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
@Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) public @interface TableField {
String value() default "";
boolean exist() default true;
String condition() default "";
String update() default "";
FieldStrategy insertStrategy() default FieldStrategy.DEFAULT;
FieldStrategy updateStrategy() default FieldStrategy.DEFAULT;
FieldStrategy whereStrategy() default FieldStrategy.DEFAULT;
FieldFill fill() default FieldFill.DEFAULT;
boolean select() default true;
boolean keepGlobalFormat() default false;
String property() default "";
JdbcType jdbcType() default JdbcType.UNDEFINED;
Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
boolean javaType() default false;
String numericScale() default ""; }
|
condition属性
源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
String condition() default "";
public class SqlCondition {
public static final String EQUAL = "%s=#{%s}";
public static final String NOT_EQUAL = "%s<>#{%s}";
public static final String LIKE = "%s LIKE CONCAT('%%',#{%s},'%%')";
public static final String ORACLE_LIKE = "%s LIKE CONCAT(CONCAT('%%',#{%s}),'%%')";
public static final String LIKE_LEFT = "%s LIKE CONCAT('%%',#{%s})";
public static final String LIKE_RIGHT = "%s LIKE CONCAT(#{%s},'%%')"; }
|
@Version注解
乐观锁 注解,作用于字段上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
@Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) public @interface Version { }
|
使用前置工作
1 2 3 4 5 6 7 8 9 10 11 12 13
| @EnableTransactionManagement @Configuration public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return interceptor; } }
|
@OrderBy注解
MySQL指定排序,优先级低于wrapper条件查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) public @interface OrderBy {
boolean asc() default false;
@Deprecated boolean isDesc() default true;
short sort() default Short.MAX_VALUE;
}
|
全局表前缀配置
1 2 3 4 5
| mybatis-plus: global-config: db-config: table-prefix: sys_
|
调试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Data public class Teacher {
@TableId(value = "id", type = IdType.AUTO) private Integer id;
private String name;
private int age; }
CREATE TABLE `sys_teacher` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `name` varchar(255) DEFAULT NULL COMMENT '客户名称', `age` int(11) DEFAULT NULL COMMENT '客户年龄', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;
|
打印日志
1 2 3
| mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|