Mybatyis-Plus学习
Mybatis常用注解
@TableName:
- 表名注解
- 设置实体类所对应的表名
@TableId:
- 主键注解
- 将属性所对应的字段设置为主键
- value属性指定主键字段
- type属性指定主键类型
IdType(enum类型)
值 | 描述 |
---|---|
AUTO | 数据库 ID 自增 |
NONE | 无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT) |
INPUT | insert 前自行 set 主键值 |
ASSIGN_ID | 分配 ID(主键类型为 Number(Long 和 Integer)或 String)(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法) |
ASSIGN_UUID | 分配 UUID,主键类型为 String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认 default 方法) |
@Data
@TableName("t_user")
public class User{
//数据库会将id默认为主键,所以有时需要自定义主键
@TableId(value="id",type=IdType.AUTO)
private int uid;
private String name;
private String gender;
}
@TableField
- 字段注解(非主键)
@TableLogic
- 逻辑删除注解
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | "" | 逻辑未删除值 |
delval | String | 否 | "" | 逻辑删除值 |
条件构造器wrapper
AbstractWrapper
用于生成sql的where条件
-
eq: 判断是否相等
eq("name","xiaoming")
等价于where name = 'xiaoming'
- eq(R column, Object val)
- eq(boolean condition, R column, Object val)
-
allEq: 全部相等(或部分为空)
- allEq(Map<R, V> params)
- allEq(Map<R, V> params, boolean null2IsNull)
- allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
:::success
params:key
为数据库字段名,value
为数据库字段值
**null2IsNull: **为true
时,当map
中value
的值为空时,调用IsNull
方法,为false
时则value
为null
则忽略。
allEq({id:1, name:"xiaoming",gender:null},gender != null)
等价于where id=1 and name='xiaoming'
:::
-
ne: 不等于(<>)
ne("age",20)
等价于age<>20
- ne(R column, Object val)
- ne(boolean condition, R column, Object val)
-
gt: 大于>
gt("age",20)
等价于age>20
- gt(R column, Object val)
- gt(boolean condition, R column, Object val)
-
lt: 小于<
lt("age",20)
等价于age<20
- lt(R column, Object val)
- lt(boolean condition, R column, Object val)
-
ge: 大于等于>=
ge("age",20)
等价于age >= 20
- ge(R column, Object val)
- ge(boolean condition, R column, Object val)
-
le: 小于等于<=
le("age",20)
等价于age <= 20
- le(R column, Object val)
- le(boolean condition, R column, Object val)
-
between: 在 1 AND 2 之间,
between("age",20,30)
--->age between 20 and 30
- between(R column, Object val1, Object val2)
- between(boolean condition, R column, Object val1, Object val2)
-
notBetween: 不在 1 AND 2 之间
- notBetween(R column, Object val1, Object val2)
- notBetween(boolean condition, R column, Object val1, Object val2)
-
like: 模糊匹配,
like("name","李")
--->name like '%李%'
(名字中带李的)- like(R column, Object val)
- like(boolean condition, R column, Object val)
-
notLike:
notLike("name","李")
--->name not like '%李%'
-
likeLeft:
likeLeft("name","李")
--->name not like '%李'
(左模糊) -
likeRight:
likeRight("name","李")
--->name not like'李%'
(右模糊) -
isNull:
isNull("name")
--->name is null
判断name是否为空- isNull(R column)
- isNull(boolean condition, R column)
-
isNotNull:
isNotNull("name")
--->name is not null
-
in:
in("age",{1,2,3})
--->age in (1,2,3)
或 **in("age",1,2,3)
--->age in (1,2,3)
- in(R column, Collection<?> value)
- in(boolean condition, R column, Collection<?> value)
-
notIn:
notIn("age",{1,2,3})
--->age not in (1,2,3)
-
inSql: 对应sql语句中的in操作,允许传递sql语句给方法,例如:
inSql("id", "1,2,3,4,5,6")
--->
id in (1,2,3,4,5,6)
,inSql("id","select id where id >5")
-->id in (select id from user where id >5)
- inSql(R column, String inValue)
- inSql(boolean condition, R column, String inValue)
- notInSql: 类似与inSql
- groupBy:
groupBy("id","name")
--->group by id,name- groupBy(R... columns)
- groupBy(boolean condition, R... columns)
- orderByAsc: 升序排列(SQL语言的默认排序方式是升序ASC)
- orderByAsc(R... columns)
- orderByAsc(boolean condition, R... columns)
- orderByDesc: 降序排列
- orderByDesc(R... columns)
- orderByDesc(boolean condition, R... columns)
- orderBy: 自定义排序方式(
isAsc
:是否升序排列- orderBy(boolean condition, boolean isAsc, R... columns)
- having:
- having(String sqlHaving, Object... params)
- having(boolean condition, String sqlHaving, Object... params)
- func:
- or:
- and:
- nested:
- apply:
- last:
- exists:
- notExists:
文章写自语雀文档,使用此链接页面效果更好https://www.yuque.com/docs/share/88565fbd-0bea-4935-9c6e-4c342c3c504f?#
本文来自博客园,作者:我只有一天的回忆,转载请注明原文链接:https://www.cnblogs.com/cc-coding/articles/16555852.html