SpringBoot全家桶

SpringBoot 全家桶 | MyBatisPlus(三)条

2020-09-23  本文已影响0人  码农StayUp

本文源码:Gitee·点这里

这里主要用到QueryWrapperUpdateWrapper两个类,用于生成sql的where条件。

UpdateWrapper

根据 whereEntity 条件,删除记录

删除年龄为10岁的用户

@Test
public void testDelete() {
    int result = userMapper.delete(new UpdateWrapper<User>().eq("age", 10));
    System.out.println(result);
}

执行日志:

JDBC Connection [HikariProxyConnection@161938368 wrapping com.mysql.cj.jdbc.ConnectionImpl@c1fa7d4] will not be managed by Spring
==>  Preparing: DELETE FROM user WHERE (age = ?)
==> Parameters: 10(Integer)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7b7b3edb]
1

更新 whereEntity 条件,更新记录

修改小于20岁用户的邮箱

@Test
public void testUpdate() {
    User user = new User();
    user.setEmail("mm@163.com");
    int result = userMapper.update(user, new UpdateWrapper<User>().le("age", 20));
    System.out.println(result);
}

执行结果:

JDBC Connection [HikariProxyConnection@203401172 wrapping com.mysql.cj.jdbc.ConnectionImpl@7a93b263] will not be managed by Spring
==>  Preparing: UPDATE user SET email=? WHERE (age <= ?)
==> Parameters: mm@163.com(String), 20(Integer)
<==    Updates: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1dbb650b]
3

QueryWrapper

根据 whereEntity 条件,查询一条记录

查询id为2的用户

@Test
public void testSelectOne() {
    User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", 2L));
    System.out.println(user);
}

执行日志:

JDBC Connection [HikariProxyConnection@1689723487 wrapping com.mysql.cj.jdbc.ConnectionImpl@33db72bd] will not be managed by Spring
==>  Preparing: SELECT id,name,age,email FROM user WHERE (id = ?)
==> Parameters: 2(Long)
<==    Columns: id, name, age, email
<==        Row: 2, Jack, 20, mm@163.com
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@15cea7b0]
User{id=2, name='Jack', age=20, email='mm@163.com'}

根据 whereEntity 条件,查询记录数量

查询年龄在20-30之前用户数量

@Test
public void testSelectCount() {
    Integer count = userMapper.selectCount(new QueryWrapper<User>().between("age", 20, 30));
    System.out.println(count);
}

执行日志:

JDBC Connection [HikariProxyConnection@859193874 wrapping com.mysql.cj.jdbc.ConnectionImpl@2216effc] will not be managed by Spring
==>  Preparing: SELECT COUNT( 1 ) FROM user WHERE (age BETWEEN ? AND ?)
==> Parameters: 20(Integer), 30(Integer)
<==    Columns: COUNT( 1 )
<==        Row: 3
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6b474074]
3

更多条件构造器(来自官网

#allEq

allEq(Map<R, V> params)
allEq(Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, Map<R, V> params, boolean null2IsNull)

个别参数说明:

params : key为数据库字段名,value为字段值
null2IsNull : 为true则在mapvaluenull时调用 isNull 方法,为false时则忽略valuenull

allEq(BiPredicate<R, V> filter, Map<R, V> params)
allEq(BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) 

个别参数说明:

filter : 过滤函数,是否允许字段传入比对条件中
paramsnull2IsNull : 同上

#eq

eq(R column, Object val)
eq(boolean condition, R column, Object val)

#ne

ne(R column, Object val)
ne(boolean condition, R column, Object val)

#gt

gt(R column, Object val)
gt(boolean condition, R column, Object val)

#ge

ge(R column, Object val)
ge(boolean condition, R column, Object val)

#lt

lt(R column, Object val)
lt(boolean condition, R column, Object val)

#le

le(R column, Object val)
le(boolean condition, R column, Object val)

#between

between(R column, Object val1, Object val2)
between(boolean condition, R column, Object val1, Object val2)

#notBetween

notBetween(R column, Object val1, Object val2)
notBetween(boolean condition, R column, Object val1, Object val2)

#like

like(R column, Object val)
like(boolean condition, R column, Object val)

#notLike

notLike(R column, Object val)
notLike(boolean condition, R column, Object val)

#likeLeft

likeLeft(R column, Object val)
likeLeft(boolean condition, R column, Object val)

#likeRight

likeRight(R column, Object val)
likeRight(boolean condition, R column, Object val)

#isNull

isNull(R column)
isNull(boolean condition, R column)

#isNotNull

isNotNull(R column)
isNotNull(boolean condition, R column)

#in

in(R column, Collection<?> value)
in(boolean condition, R column, Collection<?> value)
in(R column, Object... values)
in(boolean condition, R column, Object... values)

#notIn

notIn(R column, Collection<?> value)
notIn(boolean condition, R column, Collection<?> value)
notIn(R column, Object... values)
notIn(boolean condition, R column, Object... values)

#inSql

inSql(R column, String inValue)
inSql(boolean condition, R column, String inValue)

#notInSql

notInSql(R column, String inValue)
notInSql(boolean condition, R column, String inValue)

#groupBy

groupBy(R... columns)
groupBy(boolean condition, R... columns)

#orderByAsc

orderByAsc(R... columns)
orderByAsc(boolean condition, R... columns)

#orderByDesc

orderByDesc(R... columns)
orderByDesc(boolean condition, R... columns)

#orderBy

排序:ORDER BY 字段, ...

#having

having(String sqlHaving, Object... params)
having(boolean condition, String sqlHaving, Object... params)

#func

func(Consumer<Children> consumer)
func(boolean condition, Consumer<Children> consumer)

#or

or()
or(boolean condition)

注意事项:

主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)

or(Consumer<Param> consumer)
or(boolean condition, Consumer<Param> consumer)

#and

and(Consumer<Param> consumer)
and(boolean condition, Consumer<Param> consumer)

#nested

nested(Consumer<Param> consumer)
nested(boolean condition, Consumer<Param> consumer)

#apply

pply(String applySql, Object... params)
apply(boolean condition, String applySql, Object... params)

注意事项:

该方法可用于数据库函数 动态入参的params对应前面applySql内部的{index}部分.这样是不会有sql注入风险的,反之会有!

#last

last(String lastSql)
last(boolean condition, String lastSql)

注意事项:

只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用

#exists

exists(String existsSql)
exists(boolean condition, String existsSql)

#notExists

notExists(String notExistsSql)
notExists(boolean condition, String notExistsSql)
上一篇下一篇

猜你喜欢

热点阅读