Mybatis-Plus,WrapperTest

package com.baomidou.mybatisplus.samples.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.baomidou.mybatisplus.samples.wrapper.entity.User;

import com.baomidou.mybatisplus.samples.wrapper.mapper.RoleMapper;

import com.baomidou.mybatisplus.samples.wrapper.mapper.UserMapper;

import org.junit.jupiter.api.Assertions;

import org.junit.jupiter.api.Test;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

import java.util.List;

/**

* @author miemie

* @since 2018-08-10

*/

@SpringBootTest

public class WrapperTest {

@Resource

    private UserMapper userMapper;

@Resource

    private RoleMapper roleMapper;

@Test

    public void tests() {

System.out.println("----- 普通查询 ------");

ListplainUsers =userMapper.selectList(new QueryWrapper().eq("role_id",2L));

ListlambdaUsers =userMapper.selectList(new QueryWrapper().lambda().eq(User::getRoleId,2L));

Assertions.assertEquals(plainUsers.size(),lambdaUsers.size());

print(plainUsers);

System.out.println("----- 带子查询(sql注入) ------");

ListplainUsers2 =userMapper.selectList(new QueryWrapper()

.inSql("role_id","select id from role where id = 2"));

ListlambdaUsers2 =userMapper.selectList(new QueryWrapper().lambda()

.inSql(User::getRoleId,"select id from role where id = 2"));

Assertions.assertEquals(plainUsers2.size(),lambdaUsers2.size());

print(plainUsers2);

System.out.println("----- 带嵌套查询 ------");

ListplainUsers3 =userMapper.selectList(new QueryWrapper()

.nested(i -> i.eq("role_id",2L).or().eq("role_id",3L))

.and(i -> i.ge("age",20)));

ListlambdaUsers3 =userMapper.selectList(new QueryWrapper().lambda()

.nested(i -> i.eq(User::getRoleId,2L).or().eq(User::getRoleId,3L))

.and(i -> i.ge(User::getAge,20)));

Assertions.assertEquals(plainUsers3.size(),lambdaUsers3.size());

print(plainUsers3);

System.out.println("----- 自定义(sql注入) ------");

// 方式一

        ListplainUsers4 =userMapper.selectList(new QueryWrapper()

.apply("role_id = 2"));

/*        List lambdaUsers4 = userMapper.selectList(new QueryWrapper().lambda()

.apply("role_id = 2"));*/

        // 方式二

        ListplainUsers5 =userMapper.selectList(new QueryWrapper()

.apply("role_id = {0}",2));

/*        List lambdaUsers5 = userMapper.selectList(new QueryWrapper().lambda()

.apply("role_id = {0}",2));*/

        print(plainUsers4);

Assertions.assertEquals(plainUsers4.size(),plainUsers5.size());

UpdateWrapperuw =new UpdateWrapper<>();

uw.set("email",null);

uw.eq("id",4);

userMapper.update(new User(),uw);

User u4 =userMapper.selectById(4);

Assertions.assertNull(u4.getEmail());

}

@Test

    public void lambdaQueryWrapper() {

System.out.println("----- 普通查询 ------");

ListplainUsers =userMapper.selectList(new LambdaQueryWrapper().eq(User::getRoleId,2L));

ListlambdaUsers =userMapper.selectList(new QueryWrapper().lambda().eq(User::getRoleId,2L));

Assertions.assertEquals(plainUsers.size(),lambdaUsers.size());

print(plainUsers);

System.out.println("----- 带子查询(sql注入) ------");

ListplainUsers2 =userMapper.selectList(new LambdaQueryWrapper()

.inSql(User::getRoleId,"select id from role where id = 2"));

ListlambdaUsers2 =userMapper.selectList(new QueryWrapper().lambda()

.inSql(User::getRoleId,"select id from role where id = 2"));

Assertions.assertEquals(plainUsers2.size(),lambdaUsers2.size());

print(plainUsers2);

System.out.println("----- 带嵌套查询 ------");

ListplainUsers3 =userMapper.selectList(new LambdaQueryWrapper()

.nested(i -> i.eq(User::getRoleId,2L).or().eq(User::getRoleId,3L))

.and(i -> i.ge(User::getAge,20)));

ListlambdaUsers3 =userMapper.selectList(new QueryWrapper().lambda()

.nested(i -> i.eq(User::getRoleId,2L).or().eq(User::getRoleId,3L))

.and(i -> i.ge(User::getAge,20)));

Assertions.assertEquals(plainUsers3.size(),lambdaUsers3.size());

print(plainUsers3);

System.out.println("----- 自定义(sql注入) ------");

ListplainUsers4 =userMapper.selectList(new QueryWrapper()

.apply("role_id = 2"));

print(plainUsers4);

UpdateWrapperuw =new UpdateWrapper<>();

uw.set("email",null);

uw.eq("id",4);

userMapper.update(new User(),uw);

User u4 =userMapper.selectById(4);

Assertions.assertNull(u4.getEmail());

}

private void print(List list) {

if (!CollectionUtils.isEmpty(list)) {

list.forEach(System.out::println);

}

}

/**

* SELECT id,name,age,email,role_id FROM user

* WHERE ( 1 = 1 ) AND ( ( name = ? AND age = ? ) OR ( name = ? AND age = ? ) )

*/

    @Test

    public void testSql() {

QueryWrapper w =new QueryWrapper<>();

w.and(i -> i.eq("1",1))

.nested(i ->

i.and(j -> j.eq("name","a").eq("age",2))

.or(j -> j.eq("name","b").eq("age",2)));

userMapper.selectList(w);

}

/**

* SELECT id,name FROM user

* WHERE (age BETWEEN ? AND ?) ORDER BY role_id ASC,id ASC

*/

    @Test

    public void testSelect() {

QueryWrapperqw =new QueryWrapper<>();

qw.select("id","name").between("age",20,25)

.orderByAsc("role_id","id");

ListplainUsers =userMapper.selectList(qw);

LambdaQueryWrapperlwq =new LambdaQueryWrapper<>();

lwq.select(User::getId,User::getName).between(User::getAge,20,25)

.orderByAsc(User::getRoleId,User::getId);

ListlambdaUsers =userMapper.selectList(lwq);

print(plainUsers);

Assertions.assertEquals(plainUsers.size(),lambdaUsers.size());

}

}

你可能感兴趣的:(Mybatis-Plus,WrapperTest)