mybatisplus 条件构造器的使用

参考:  https://www.jianshu.com/p/ceb1df475021     文中的  三、mp的通用crud:

https://www.jianshu.com/p/c5537559ae3a

https://songzixian.com/mybatisplus/1354.html

https://baomidou.com/pages/10c804/#alleq (官网)

 

实战:

 

 

delete_User

~~~~
    @Autowired
    TestServer testServer;
~~~~~

@PostMapping("/aaa")
    public String delete_User3 (@RequestBody User user, HttpServletRequest HttpServletRequest) {
        testServer.aaa();
        testServer.bbb();
        return "完成更新操作";
}

 

tbl_b2c_user

package com.dao.entity;


import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName(value = "tbl_b2c_user")//-------------------指定表名-------------这个不错,还可以加上库名.表名,如果配置文件没有定义库名 那么这里要加上
public class tbl_b2c_user {
    private int id;
    private String name;
}

 

 

tbl_b2c_userDao

package com.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dao.entity.tbl_b2c_user;

public interface tbl_b2c_userDao extends BaseMapper<tbl_b2c_user> {
}

 

 

TestServer

package com.server.Delete_user;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.dao.entity.tbl_b2c_user;
import com.dao.mapper.tbl_b2c_userDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@DS("staging_B2c")
@Service
public class TestServer {
    //    测试使用构造器写sql
    @Autowired
    com.dao.mapper.tbl_b2c_userDao tbl_b2c_userDao;

//    更新操作
    public void aaa(){
    com.dao.entity.tbl_b2c_user tbl_b2c_user=new tbl_b2c_user();
    tbl_b2c_user.setId(2);
    tbl_b2c_user.setName("更新test测试2");
    tbl_b2c_userDao.updateById(tbl_b2c_user);//根据id进行更新,没有传值的属性就不会更新
}
    //in查询操作
    public void bbb(){
        List<Integer> idList = new ArrayList<>();
        idList.add(1);
        idList.add(2);
        idList.add(3);
        List<tbl_b2c_user> employees = tbl_b2c_userDao.selectBatchIds(idList);
        System.out.println(employees);
        employees.forEach(System.out::println);
    }
//模糊查询
public void ccc(){
    QueryWrapper<tbl_b2c_user> queryWrapper = new QueryWrapper<>();
    //模糊查询
    queryWrapper.like("name", "我是强爷");
    List<tbl_b2c_user> users = tbl_b2c_userDao.selectList(queryWrapper);}

 后记: 发现园可以这么写

@Test
    public void testOptimisticLocker(){
        //查询用户的信息
        User user = userMapper.selectById(2L);
        //修改用户的信息
        user.setName("丁七岁");
        user.setEmail("qisui123456@qq.com");
        //执行更新的操作
        userMapper.updateById(user);
    }
https://blog.csdn.net/qq_43612538/article/details/105824506
public void bbb(){
    NewTable NewTable=new NewTable();     这个是实体类, 貌似 如果一个list 里面给了多个这个实体类的对象 可以 传到 insert() 里面 他会一条一条的插入 ,有空试试,看明爷代码有这么写
    NewTable.setName("张三");
    NewTable.setAge(1);
    NewTable.setGender("小班");
    test_Dao.insert(NewTable);          
    }

  

 

 

如果后面忘了mybatis操作数据库那么就按下面顺序看

mybatis 有xml 怎么用

https://www.cnblogs.com/kaibindirver/p/15025758.html

https://www.cnblogs.com/kaibindirver/p/15240918.html  

mybatis 注解使用

https://www.cnblogs.com/kaibindirver/p/15047034.html

 

mp多数据源使用

https://www.cnblogs.com/kaibindirver/p/15121436.html

 

 

 

 

记录一些常用的

 

 

线上项目,凡哥写的

可以用映射的方法 获取要比对的字段 如下面的 OrderEntity::getUserId  意思是 拿OrderEntity 里面的userId字段   ===> .eq("UserId",req.getUserId())

.eq(OrderEntity::getUserId, req.getUserId())

 

allEq  全部

  • 例1: allEq({id:1,name:"老王",age:null})--->id = 1 and name = '老王' and age is null
  • 例2: allEq({id:1,name:"老王",age:null}, false)--->id = 1 and name = '老王'       #加false 就是 查询的值为空的 就过滤掉这个查询条件
  • 例3: allEq(true,{id:1,name:"老王",age:null})    true --->id = 1 and name = '老王' and age is null   false -->不需要查询条件--------这里可以结合  13、判断返回变量是否为空简写 返回为true or false  这个方法来用,场景是是否需要条件搜索  https://www.cnblogs.com/kaibindirver/p/11761456.html

     

这里面要放进一个字典 有2种方法

一种创建 map 

Map m1 = new HashMap();
m1.put("user_id", "1420");

一种字符转 map 

import com.alibaba.fastjson.JSONObject;

String m="{\"user_id\":\"1420\"}";
JSONObject m1= JSONObject.parseObject(m);

eq  等于

eq("name", "老王")--->name = '老王'

 

ne 不等于 <>

ne("name", "老王")--->name <> '老王'

 

gt   大于 >

gt("age", 18)--->age > 18

 

ge  大于等于 >=

ge("age", 18)--->age >= 18

 

lt  小于 <

lt("age", 18)--->age < 18

le  小于等于 <==

le("age", 18)--->age <= 18

 

between

between("age", 18, 30)--->age between 18 and 30

 

notBetween

notBetween("age", 18, 30)--->age not between 18 and 30

 

like

like("name", "王")--->name like '%王%'

notLike

notLike("name", "王")--->name not like '%王%'

likeLeft

likeLeft("name", "王")--->name like '%王'

 

likeRight

likeRight("name", "王")--->name like '王%'

isNull

isNull("name")--->name is null

 

isNotNull

isNotNull("name")--->name is not null

in

in("age",{1,2,3})--->age in (1,2,3)

notIn

notIn("age", 1, 2, 3)--->age not in (1,2,3)

 

inSql

inSql("age", "1,2,3,4,5,6")--->age in (1,2,3,4,5,6)
inSql("id", "select id from table where id < 3")--->id in (select id from table where id < 3)

notInSql

notInSql("age", "1,2,3,4,5,6")--->age not in (1,2,3,4,5,6)

notInSql("id", "select id from table where id < 3")--->id not in (select id from table where id < 3)

 

groupBy

groupBy("id", "name")--->group by id,name

 

orderByAsc

orderByAsc("id", "name")--->order by id ASC,name ASC

 

orderByDesc

orderByDesc("id", "name")--->order by id DESC,name DESC

 

orderBy

orderBy(true, true, "id", "name")--->order by id ASC,name ASC

having

having("sum(age) > 10")--->having sum(age) > 10

having("sum(age) > {0}", 11)--->having sum(age) > 11

or

eq("id",1).or().eq("name","老王")--->id = 1 or name = '老王'


 

OR嵌套

or(i -> i.eq("name", "李白").ne("status", "活着"))--->or (name = '李白' and status <> '活着')

 

and

and(i -> i.eq("name", "李白").ne("status", "活着"))--->and (name = '李白' and status <> '活着')

 

nested

nested(i -> i.eq("name", "李白").ne("status", "活着"))--->(name = '李白' and status <> '活着')

 

apply  拼接 sql  该方法可用于数据库函数

apply("id = 1")--->id = 1

apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08")--->date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")

 

last   无视优化规则直接拼接到 sql 的最后 --只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用

last("limit 1")

 

exists ---这个就是判断 是否执行前面sql语句的   ---select * from tbl_b2c_user where id=5 and exists(select user_id from tbl_b2c_user where id=1)

exists("select id from table where age = 1")--->exists (select id from table where age = 1)

 

notExists

和exists相反

 

select

指定sql语句要返回的字段

 

后记:

.ne("school", "老王")
和上面的效果一样(SignUserData是实体类,里面有字段school,下面这样写代码会帮你检查对错)
.ne(SignUserData::school, "老王")

 

posted @ 2021-11-09 20:20  凯宾斯基  阅读(351)  评论(0编辑  收藏  举报