为了账号安全,请及时绑定邮箱和手机立即绑定

MyBatis-Plus入门

难度高级
时长 4小时 0分
学习人数
综合评分9.50
91人评价 查看评价
9.6 内容实用
9.3 简洁易懂
9.6 逻辑清晰
  • 1.@TableName("TableName") 用来指定实体类对应得数据库表
    2.@TableId 用来指明该字段为主键
    3.@TableField("FieldName")用来指定字段名

    查看全部
    0 采集 收起 来源:常用注解

    2021-06-04

  • 配置文件:

    https://img4.mukewang.com/5d2065ee00010d2409870447.jpg

    查看全部
  • @tableId 指定主键
    查看全部
  • 数据库自增id怎么写
    查看全部
    0 采集 收起 来源:普通查询

    采集自:IT不倒翁 2021-04-28

  • CRUD - 更新

    查看全部
    0 采集 收起 来源:更新方法

    采集自:梦编猿 2021-04-22

  • 2018年最受欢迎中国开源软件排名

    查看全部
    0 采集 收起 来源:MP简介

    2021-04-11

  • 数据库自增id怎么写
    查看全部
    0 采集 收起 来源:普通查询

    2021-04-11

  • myBatis Mapper更新(少量字段)

    查看全部
    0 采集 收起 来源:更新方法

    2021-04-06

  • myBatis plus分页

    查看全部
    0 采集 收起 来源:分页查询

    2021-04-06

  • 普通查询:使用方式为实现BaseMapper<T>接口对象调用该方法。

    1. T selectById(Serializable id):使用场景为通过主键查询,只要该主键类型实现了Serialzable接口即可。

    2、List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList):使用场景为通过主键的集合去批量查询,前提主键的类型实现了Serializable接口。传入array

    3、List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String,Object> columnMap):使用场景为传入一个Map集合,key为表字段,value为表字段值。注意:Map的key为数据表的字段名,不是实体类属性名。

    查看全部
    0 采集 收起 来源:普通查询

    2021-04-06

  • mybatis plus注解

    @TableId 设置主键

    @TableName("mp_user") 匹配表名

    @TableField("name") 匹配字段名

    非数据库表中字段

    @TableField(exist=false)

    查看全部
    0 采集 收起 来源:常用注解

    2021-04-06

  • /**

     * @date 2020年2月23日  

     * @author 翁东州

     * @所有方法的代码点击查看全文,整理了一整天累死了

     */

    package first;


    import java.time.LocalDateTime;

    import java.util.Arrays;

    import java.util.HashMap;

    import java.util.List;

    import java.util.Map;


    import org.junit.Assert;

    import org.junit.Test;

    import org.junit.runner.RunWith;

    import org.springframework.beans.factory.annotation.Autowired;

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

    import org.springframework.test.context.junit4.SpringRunner;


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

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

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

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

    import com.baomidou.mybatisplus.core.metadata.IPage;

    import com.baomidou.mybatisplus.core.toolkit.StringUtils;

    import com.baomidou.mybatisplus.core.toolkit.Wrappers;

    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

    import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;

    import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;

    import com.mp.dao.UserMapper;

    import com.mp.entity.User;

    import com.mp.service.UserService;




    /**

     * @date 2020年2月23日  

     * @author 翁东州

     * @方法中文名称:

     */

    @RunWith(SpringRunner.class)

    @SpringBootTest

    public class SimpleTest {

    @Autowired

    private UserMapper userMapper;

    //展示所有

    @Test

    public void select() {

    List<User> list = userMapper.selectList(null);

    Assert.assertEquals(5, list.size());

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

    System.out.println();

    }

    //传统添加

    public void insert() {

    User user = new User();

    user.setName("名字");

    user.setAge(31);

    user.setManagerId(123L);

    user.setCreateTime(LocalDateTime.now());

    int rows = userMapper.insert(user);

    System.out.println("影响记录数"+rows);


    }

    //单体展示

    public void selectById() {

    User user = userMapper.selectById(123L);

    System.out.println(user);

    }

    //多条展示

    public void selectByIds() {

    List<Long> idList = Arrays.asList(123L,234L,345L);

    List<User> users = userMapper.selectBatchIds(idList);

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

    }

    //map形式查询

    public void selectByMap() {

    Map<String, Object> columnMap = new HashMap<>();

    columnMap.put("name", "王天风");

    columnMap.put("age", 25);

    List<User> users = userMapper.selectByMap(columnMap);

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

    }

    //wrapper 名字包含雨且年龄小于40

    public void selectByWrapper() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.eq("name", "雨").lt("age", 40);

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //wrapper 名字包含雨,且年龄在20到40之间,email不为空

    public void selectByWrapper2() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.eq("name", "雨").between("age", 20, 40).isNotNull("email");

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //wrapper 名字包含王,年龄大于等于25,按年龄降序排序,而后再按id升序排序

    public void selectByWrapper3() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.likeRight("name", "王").or().ge("age", 25).orderByDesc("age").orderByAsc("id");

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //创建日期为2019年2月14日且直属上级名字为王姓

    public void selectByWrapper4() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}", "2019-02-14 or true or true")

    .inSql("manager_id", "select id from user where name like '王%'");

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //名字为王姓,并且(年龄小于40或邮箱不为空)

    public void selectByWrapper5() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.likeRight("name", "王").and(wq -> wq.lt("age", 40).or().isNotNull("email"));

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //名字为王姓,或者(年龄小于40并且年龄大于20并且邮箱不为空)

    public void selectByWrapper6() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.likeRight("name", "王").or(wq -> wq.between("age", 20, 40).isNotNull("email"));

    //queryWrapper.likeRight("name", "王").or(wq -> wq.lt("age", 40).gt("age", 20).isNotNull("email"));

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //名字为王姓,并且(年龄小于40或邮箱不为空)

    public void selectByWrapper7() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.nested(wq -> wq.lt("age", 40).or().isNotNull("email")).likeRight("name", "王");

    //queryWrapper.likeRight("name", "王").and(wq -> wq.lt("age", 40).or().isNotNull("email"));

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //年龄为30,31,34,35

    public void selectByWrapper8() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.in("age", Arrays.asList(30,31,34,35));

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //只返回满足条件的其中一条语句即可

    public void selectByWrapper9() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.in("age", Arrays.asList(30,31,34,35)).last("limit 1");

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //名字中包含雨并且年龄小于40,只展示id和名字

    public void selectByWrapperSupper() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.select("id","name").like("name", "雨").lt("age", 40);

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //名字中包含雨并且年龄小于40,展示不包括创建字段和上级id字段的其他字段

    public void selectByWrapperSupper2() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.like("name", "雨").lt("age", 40)

    .select(User.class, info -> !info.getColumn().equals("create_time")

    &&!info.getColumn().equals("manager_id"));

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //测试condition

    @Test

    public void testCondition() { 

    String name = "王";

    String email = "";

    condition1(name,email);

    }

    //名字中包含王,当email为空时不查询

    public void condition1(String name,String email) { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    //传统方法

    //if (StringUtils.isNotColumnName(name)) {

    // queryWrapper.like("name", name);

    //}

    //if (StringUtils.isNotColumnName(email)) {

    // queryWrapper.like("email", email);

    //}

    queryWrapper.like(StringUtils.isNotEmpty(name), "name",name)

    .like(StringUtils.isNotEmpty(email), "email",email);

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //查找姓名为刘红雨且年龄32岁的数据

    public void selectByWrapperEntity() { 

    User whereUser = new User();

    whereUser.setName("刘红雨");

    whereUser.setAge(32);

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>(whereUser);

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //allEq

    public void selectByWrapperAllEq() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    Map<String, Object> params = new HashMap<String, Object>();

    //姓名为王天风,年龄25

    //params.put("name", "王天风");

    //params.put("age", 25);

    //queryWrapper.allEq(params);

    //姓名为王天风,年龄为空则忽略

    params.put("name", "王天风");

    params.put("age", null);

    //queryWrapper.allEq(params,false);

    //剔除name查询条件

    queryWrapper.allEq((k,v)->!k.equals("name"), params);

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //Map格式,查询姓名包含雨,年龄小于40的数据

    public void selectByWrapperMaps() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.like("name", "雨").lt("age", 40);

    List<Map<String, Object>> userList = userMapper.selectMaps(queryWrapper);

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

    }

    //Map格式,按照直属上级id分组,查询每组的平均年龄,最大年龄,最小年龄,只去年龄总和小于500的组

    public void selectByWrapperMaps2() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.select("avg(age) avg_age","min(age) min_age","max(age) max_age")

    .groupBy("manager_id").having("sum(age)<{0}",500);

    List<Map<String, Object>> userList = userMapper.selectMaps(queryWrapper);

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

    }

    //obj格式,查询姓名包含雨,年龄小于40的数据

    public void selectByWrapperobjs() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.select("id","name").like("name", "雨").lt("age", 40);

    List<Object> userList = userMapper.selectObjs(queryWrapper);

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

    }

    //查询姓名包含雨,年龄小于40的数据条数的总条数

    public void selectByWrapperCount() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.like("name", "雨").lt("age", 40);

    Integer count = userMapper.selectCount(queryWrapper);

    System.out.println("总记录数"+ count);

    }

    //查询姓名包含雨,年龄小于40的数据条数的一条数据

    public void selectByWrapperOne() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.like("name", "雨").lt("age", 40);

    User user = userMapper.selectOne(queryWrapper);

    System.out.println(user);

    }

    //lambda,三种起手

    public void selectLambda() { 

    //LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();

    //LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<User>();

    LambdaQueryWrapper<User> lambdaQuery = Wrappers.<User>lambdaQuery();

    lambdaQuery.like(User::getName, "雨").lt(User::getAge, 40);

    List<User> userList = userMapper.selectList(lambdaQuery);

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

    }

    //lambda,名字为王姓,并且(年龄小于40或邮箱不为空)

    public void selectLambda2() { 

    LambdaQueryWrapper<User> lambdaQuery = Wrappers.<User>lambdaQuery();

    lambdaQuery.likeRight(User::getName, "王")

    .and(lqw ->lqw.lt(User::getAge, 40).or().isNotNull(User::getEmail));

    List<User> userList = userMapper.selectList(lambdaQuery);

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

    }

    //lambda,名字为王姓,并且(年龄小于40或邮箱不为空),LambdaQueryChainWrapper一气呵成

    public void selectLambda3() { 

    List<User> userList = new LambdaQueryChainWrapper<User>(userMapper).like(User::getName, "雨").ge(User::getAge, 20).list();

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

    }

    //分页

    public void selectPage() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.ge("age", 26);

    Page<User> page = new Page<User>(1,10);//1为第一页,10为一页10行

    IPage<User> iPage = userMapper.selectPage(page, queryWrapper);

    System.out.println("总页数"+iPage.getPages());

    System.out.println("总记录数"+iPage.getTotal());

    List<User> userList = iPage.getRecords();

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

    }

    //传统根据id更新,根据id更新数据

    public void updateById() { 

    User user = new User();

    user.setId(123L);

    user.setAge(25);

    user.setEmail("wtf2@baomidou.com");

    int rows = userMapper.updateById(user);

    System.out.println("影响记录数"+ rows);

    }

    //传统根据Wrapper更新,姓名为李艺伟且年龄为28的数据,更改其id,年龄和邮箱

    public void updateByWrapper2() { 

    UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();

    updateWrapper.eq("name", "李艺伟").eq("age", 28);

    User user = new User();

    user.setId(123L);

    user.setAge(25);

    user.setEmail("wtf2@baomidou.com");

    int rows = userMapper.update(user,updateWrapper);

    System.out.println("影响记录数"+ rows);

    }

    //Wrapper更新,姓名为李艺伟且年龄为29的数据,更改年龄为30

    public void updateByWrapper3() { 

    UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();

    updateWrapper.eq("name", "李艺伟").eq("age", 29).set("age", 30);

    int rows = userMapper.update(null,updateWrapper);

    System.out.println("影响记录数"+ rows);

    }

    //lambda的Wrapper更新,姓名为李艺伟且年龄为29的数据,更改年龄为30

    public void updateByWrapperLambda() { 

    LambdaUpdateWrapper<User> lambdaUpdate = Wrappers.<User>lambdaUpdate();

    lambdaUpdate.eq(User::getName, "李艺伟").eq(User::getAge, 30).set(User::getAge, 31);

    int rows = userMapper.update(null,lambdaUpdate);

    System.out.println("影响记录数"+ rows);

    }

    //lambda的Wrapper更新,姓名为李艺伟且年龄为29的数据,更改年龄为30,一气呵成

    public void updateByWrapperLambdaChain() { 

    boolean update = new LambdaUpdateChainWrapper<User>(userMapper)

    .eq(User::getName, "李艺伟").eq(User::getAge, 29).set(User::getAge, 31).update();

    System.out.println(update);

    }

    //根据id删除数据

    public void deleteById() {

    int rows = userMapper.deleteById(123L);

    System.out.println("删除条数"+rows);

    }

    //根据map中名称为向后,年龄25删除数据

    public void deleteByMap() {

    Map<String, Object> columnMap = new HashMap<>();

    columnMap.put("name", "向后");

    columnMap.put("age",25);

    int rows = userMapper.deleteByMap(columnMap);

    System.out.println("删除条数"+rows);

    }

    //根据id删除多条数据

    public void deleteByBatchIds() {

    int rows = userMapper.deleteBatchIds(Arrays.asList(123L,234L,345L));

    System.out.println("删除条数"+rows);

    }

    //根据wrapper删除数据

    public void deleteByWrapper() {

    LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.<User>lambdaQuery();

    lambdaQueryWrapper.eq(User::getAge, 27).or().gt(User::getAge, 41);

    int rows = userMapper.delete(lambdaQueryWrapper);

    System.out.println("删除条数"+rows);

    }

    //AR模式添加

    public void insert2() {

    User user = new User();

    user.setName("刘华");

    user.setAge(29);

    user.setEmail("lh@baomidou.com");

    user.setManagerId(123L);

    user.setCreateTime(LocalDateTime.now());

    boolean insert = user.insert();//类继承Model

    System.out.println(insert);

    }

    //AR模式查找

    public void selectById2() {

    User user = new User();

    user.setId(123L);

    User selectById = user.selectById();

    System.out.println(selectById);

    }

    //AR模式更新

    public void updateById2() {

    User user = new User();

    user.setId(123L);

    user.setName("刘华");

    user.setAge(29);

    user.setEmail("lh@baomidou.com");

    user.setManagerId(123L);

    user.setCreateTime(LocalDateTime.now());

    boolean updateById = user.updateById();//类继承Model

    System.out.println(updateById);

    }

    //AR模式添加或更新

    public void insertOrUpdate() {

    User user = new User();

    user.setName("刘华");

    user.setAge(29);

    user.setEmail("lh@baomidou.com");

    user.setManagerId(123L);

    user.setCreateTime(LocalDateTime.now());

    boolean insertOrUpdate = user.insertOrUpdate();//类继承Model

    System.out.println(insertOrUpdate);

    }

    @Autowired

    private UserService userService;

    //使用service,获取一条数据

    public void getOne() {

    User one = userService.getOne(Wrappers.<User>lambdaQuery().gt(User::getAge, 25));//多条报错

    System.out.println(one);

    }

    //使用service,获取一条数据

    public void batch() {

    User user1 = new User();

    user1.setName("徐丽1");

    user1.setAge(28);

    User user2 = new User();

    user2.setId(123L);

    user2.setName("徐丽2");

    user2.setAge(29);

    List<User> users = Arrays.asList(user1,user2);

    boolean saveBatch = userService.saveBatch(users);

    //保存或更新

    //userService.saveOrUpdateBatch(users);

    System.out.println(saveBatch);

    }

    //使用service,lambda查询年龄大于25且名字包含雨的数据,一气呵成

    public void chain1() {

    List<User> users = userService.lambdaQuery().gt(User::getAge, 25).like(User::getName, "雨").list();

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

    }

    //使用service,lambda更新年龄为25的数据为26,一气呵成

    public void chain2() {

    boolean update = userService.lambdaUpdate().eq(User::getAge, 25).set(User::getAge, 26).update();

    System.out.println(update);

    }

    //使用service,lambda删除年龄为25的数据,一气呵成

    public void chain3() {

    boolean remove = userService.lambdaUpdate().gt(User::getAge, 25).remove();

    System.out.println(remove);

    }


    }


    查看全部
    4 采集 收起 来源:通用service

    采集自:慕粉4457612 2021-03-28

  • /**

     * @date 2020年2月23日  

     * @author 翁东州

     * @所有方法的代码点击查看全文,整理了一整天累死了

     */

    package first;


    import java.time.LocalDateTime;

    import java.util.Arrays;

    import java.util.HashMap;

    import java.util.List;

    import java.util.Map;


    import org.junit.Assert;

    import org.junit.Test;

    import org.junit.runner.RunWith;

    import org.springframework.beans.factory.annotation.Autowired;

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

    import org.springframework.test.context.junit4.SpringRunner;


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

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

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

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

    import com.baomidou.mybatisplus.core.metadata.IPage;

    import com.baomidou.mybatisplus.core.toolkit.StringUtils;

    import com.baomidou.mybatisplus.core.toolkit.Wrappers;

    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

    import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;

    import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;

    import com.mp.dao.UserMapper;

    import com.mp.entity.User;

    import com.mp.service.UserService;




    /**

     * @date 2020年2月23日  

     * @author 翁东州

     * @方法中文名称:

     */

    @RunWith(SpringRunner.class)

    @SpringBootTest

    public class SimpleTest {

    @Autowired

    private UserMapper userMapper;

    //展示所有

    @Test

    public void select() {

    List<User> list = userMapper.selectList(null);

    Assert.assertEquals(5, list.size());

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

    System.out.println();

    }

    //传统添加

    public void insert() {

    User user = new User();

    user.setName("名字");

    user.setAge(31);

    user.setManagerId(123L);

    user.setCreateTime(LocalDateTime.now());

    int rows = userMapper.insert(user);

    System.out.println("影响记录数"+rows);


    }

    //单体展示

    public void selectById() {

    User user = userMapper.selectById(123L);

    System.out.println(user);

    }

    //多条展示

    public void selectByIds() {

    List<Long> idList = Arrays.asList(123L,234L,345L);

    List<User> users = userMapper.selectBatchIds(idList);

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

    }

    //map形式查询

    public void selectByMap() {

    Map<String, Object> columnMap = new HashMap<>();

    columnMap.put("name", "王天风");

    columnMap.put("age", 25);

    List<User> users = userMapper.selectByMap(columnMap);

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

    }

    //wrapper 名字包含雨且年龄小于40

    public void selectByWrapper() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.eq("name", "雨").lt("age", 40);

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //wrapper 名字包含雨,且年龄在20到40之间,email不为空

    public void selectByWrapper2() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.eq("name", "雨").between("age", 20, 40).isNotNull("email");

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //wrapper 名字包含王,年龄大于等于25,按年龄降序排序,而后再按id升序排序

    public void selectByWrapper3() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.likeRight("name", "王").or().ge("age", 25).orderByDesc("age").orderByAsc("id");

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //创建日期为2019年2月14日且直属上级名字为王姓

    public void selectByWrapper4() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}", "2019-02-14 or true or true")

    .inSql("manager_id", "select id from user where name like '王%'");

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //名字为王姓,并且(年龄小于40或邮箱不为空)

    public void selectByWrapper5() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.likeRight("name", "王").and(wq -> wq.lt("age", 40).or().isNotNull("email"));

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //名字为王姓,或者(年龄小于40并且年龄大于20并且邮箱不为空)

    public void selectByWrapper6() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.likeRight("name", "王").or(wq -> wq.between("age", 20, 40).isNotNull("email"));

    //queryWrapper.likeRight("name", "王").or(wq -> wq.lt("age", 40).gt("age", 20).isNotNull("email"));

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //名字为王姓,并且(年龄小于40或邮箱不为空)

    public void selectByWrapper7() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.nested(wq -> wq.lt("age", 40).or().isNotNull("email")).likeRight("name", "王");

    //queryWrapper.likeRight("name", "王").and(wq -> wq.lt("age", 40).or().isNotNull("email"));

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //年龄为30,31,34,35

    public void selectByWrapper8() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.in("age", Arrays.asList(30,31,34,35));

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //只返回满足条件的其中一条语句即可

    public void selectByWrapper9() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.in("age", Arrays.asList(30,31,34,35)).last("limit 1");

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //名字中包含雨并且年龄小于40,只展示id和名字

    public void selectByWrapperSupper() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.select("id","name").like("name", "雨").lt("age", 40);

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //名字中包含雨并且年龄小于40,展示不包括创建字段和上级id字段的其他字段

    public void selectByWrapperSupper2() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.like("name", "雨").lt("age", 40)

    .select(User.class, info -> !info.getColumn().equals("create_time")

    &&!info.getColumn().equals("manager_id"));

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //测试condition

    @Test

    public void testCondition() { 

    String name = "王";

    String email = "";

    condition1(name,email);

    }

    //名字中包含王,当email为空时不查询

    public void condition1(String name,String email) { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    //传统方法

    //if (StringUtils.isNotColumnName(name)) {

    // queryWrapper.like("name", name);

    //}

    //if (StringUtils.isNotColumnName(email)) {

    // queryWrapper.like("email", email);

    //}

    queryWrapper.like(StringUtils.isNotEmpty(name), "name",name)

    .like(StringUtils.isNotEmpty(email), "email",email);

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //查找姓名为刘红雨且年龄32岁的数据

    public void selectByWrapperEntity() { 

    User whereUser = new User();

    whereUser.setName("刘红雨");

    whereUser.setAge(32);

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>(whereUser);

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //allEq

    public void selectByWrapperAllEq() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    Map<String, Object> params = new HashMap<String, Object>();

    //姓名为王天风,年龄25

    //params.put("name", "王天风");

    //params.put("age", 25);

    //queryWrapper.allEq(params);

    //姓名为王天风,年龄为空则忽略

    params.put("name", "王天风");

    params.put("age", null);

    //queryWrapper.allEq(params,false);

    //剔除name查询条件

    queryWrapper.allEq((k,v)->!k.equals("name"), params);

    List<User> userList = userMapper.selectList(queryWrapper);

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

    }

    //Map格式,查询姓名包含雨,年龄小于40的数据

    public void selectByWrapperMaps() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.like("name", "雨").lt("age", 40);

    List<Map<String, Object>> userList = userMapper.selectMaps(queryWrapper);

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

    }

    //Map格式,按照直属上级id分组,查询每组的平均年龄,最大年龄,最小年龄,只去年龄总和小于500的组

    public void selectByWrapperMaps2() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.select("avg(age) avg_age","min(age) min_age","max(age) max_age")

    .groupBy("manager_id").having("sum(age)<{0}",500);

    List<Map<String, Object>> userList = userMapper.selectMaps(queryWrapper);

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

    }

    //obj格式,查询姓名包含雨,年龄小于40的数据

    public void selectByWrapperobjs() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.select("id","name").like("name", "雨").lt("age", 40);

    List<Object> userList = userMapper.selectObjs(queryWrapper);

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

    }

    //查询姓名包含雨,年龄小于40的数据条数的总条数

    public void selectByWrapperCount() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.like("name", "雨").lt("age", 40);

    Integer count = userMapper.selectCount(queryWrapper);

    System.out.println("总记录数"+ count);

    }

    //查询姓名包含雨,年龄小于40的数据条数的一条数据

    public void selectByWrapperOne() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.like("name", "雨").lt("age", 40);

    User user = userMapper.selectOne(queryWrapper);

    System.out.println(user);

    }

    //lambda,三种起手

    public void selectLambda() { 

    //LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();

    //LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<User>();

    LambdaQueryWrapper<User> lambdaQuery = Wrappers.<User>lambdaQuery();

    lambdaQuery.like(User::getName, "雨").lt(User::getAge, 40);

    List<User> userList = userMapper.selectList(lambdaQuery);

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

    }

    //lambda,名字为王姓,并且(年龄小于40或邮箱不为空)

    public void selectLambda2() { 

    LambdaQueryWrapper<User> lambdaQuery = Wrappers.<User>lambdaQuery();

    lambdaQuery.likeRight(User::getName, "王")

    .and(lqw ->lqw.lt(User::getAge, 40).or().isNotNull(User::getEmail));

    List<User> userList = userMapper.selectList(lambdaQuery);

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

    }

    //lambda,名字为王姓,并且(年龄小于40或邮箱不为空),LambdaQueryChainWrapper一气呵成

    public void selectLambda3() { 

    List<User> userList = new LambdaQueryChainWrapper<User>(userMapper).like(User::getName, "雨").ge(User::getAge, 20).list();

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

    }

    //分页

    public void selectPage() { 

    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

    queryWrapper.ge("age", 26);

    Page<User> page = new Page<User>(1,10);//1为第一页,10为一页10行

    IPage<User> iPage = userMapper.selectPage(page, queryWrapper);

    System.out.println("总页数"+iPage.getPages());

    System.out.println("总记录数"+iPage.getTotal());

    List<User> userList = iPage.getRecords();

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

    }

    //传统根据id更新,根据id更新数据

    public void updateById() { 

    User user = new User();

    user.setId(123L);

    user.setAge(25);

    user.setEmail("wtf2@baomidou.com");

    int rows = userMapper.updateById(user);

    System.out.println("影响记录数"+ rows);

    }

    //传统根据Wrapper更新,姓名为李艺伟且年龄为28的数据,更改其id,年龄和邮箱

    public void updateByWrapper2() { 

    UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();

    updateWrapper.eq("name", "李艺伟").eq("age", 28);

    User user = new User();

    user.setId(123L);

    user.setAge(25);

    user.setEmail("wtf2@baomidou.com");

    int rows = userMapper.update(user,updateWrapper);

    System.out.println("影响记录数"+ rows);

    }

    //Wrapper更新,姓名为李艺伟且年龄为29的数据,更改年龄为30

    public void updateByWrapper3() { 

    UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();

    updateWrapper.eq("name", "李艺伟").eq("age", 29).set("age", 30);

    int rows = userMapper.update(null,updateWrapper);

    System.out.println("影响记录数"+ rows);

    }

    //lambda的Wrapper更新,姓名为李艺伟且年龄为29的数据,更改年龄为30

    public void updateByWrapperLambda() { 

    LambdaUpdateWrapper<User> lambdaUpdate = Wrappers.<User>lambdaUpdate();

    lambdaUpdate.eq(User::getName, "李艺伟").eq(User::getAge, 30).set(User::getAge, 31);

    int rows = userMapper.update(null,lambdaUpdate);

    System.out.println("影响记录数"+ rows);

    }

    //lambda的Wrapper更新,姓名为李艺伟且年龄为29的数据,更改年龄为30,一气呵成

    public void updateByWrapperLambdaChain() { 

    boolean update = new LambdaUpdateChainWrapper<User>(userMapper)

    .eq(User::getName, "李艺伟").eq(User::getAge, 29).set(User::getAge, 31).update();

    System.out.println(update);

    }

    //根据id删除数据

    public void deleteById() {

    int rows = userMapper.deleteById(123L);

    System.out.println("删除条数"+rows);

    }

    //根据map中名称为向后,年龄25删除数据

    public void deleteByMap() {

    Map<String, Object> columnMap = new HashMap<>();

    columnMap.put("name", "向后");

    columnMap.put("age",25);

    int rows = userMapper.deleteByMap(columnMap);

    System.out.println("删除条数"+rows);

    }

    //根据id删除多条数据

    public void deleteByBatchIds() {

    int rows = userMapper.deleteBatchIds(Arrays.asList(123L,234L,345L));

    System.out.println("删除条数"+rows);

    }

    //根据wrapper删除数据

    public void deleteByWrapper() {

    LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.<User>lambdaQuery();

    lambdaQueryWrapper.eq(User::getAge, 27).or().gt(User::getAge, 41);

    int rows = userMapper.delete(lambdaQueryWrapper);

    System.out.println("删除条数"+rows);

    }

    //AR模式添加

    public void insert2() {

    User user = new User();

    user.setName("刘华");

    user.setAge(29);

    user.setEmail("lh@baomidou.com");

    user.setManagerId(123L);

    user.setCreateTime(LocalDateTime.now());

    boolean insert = user.insert();//类继承Model

    System.out.println(insert);

    }

    //AR模式查找

    public void selectById2() {

    User user = new User();

    user.setId(123L);

    User selectById = user.selectById();

    System.out.println(selectById);

    }

    //AR模式更新

    public void updateById2() {

    User user = new User();

    user.setId(123L);

    user.setName("刘华");

    user.setAge(29);

    user.setEmail("lh@baomidou.com");

    user.setManagerId(123L);

    user.setCreateTime(LocalDateTime.now());

    boolean updateById = user.updateById();//类继承Model

    System.out.println(updateById);

    }

    //AR模式添加或更新

    public void insertOrUpdate() {

    User user = new User();

    user.setName("刘华");

    user.setAge(29);

    user.setEmail("lh@baomidou.com");

    user.setManagerId(123L);

    user.setCreateTime(LocalDateTime.now());

    boolean insertOrUpdate = user.insertOrUpdate();//类继承Model

    System.out.println(insertOrUpdate);

    }

    @Autowired

    private UserService userService;

    //使用service,获取一条数据

    public void getOne() {

    User one = userService.getOne(Wrappers.<User>lambdaQuery().gt(User::getAge, 25));//多条报错

    System.out.println(one);

    }

    //使用service,获取一条数据

    public void batch() {

    User user1 = new User();

    user1.setName("徐丽1");

    user1.setAge(28);

    User user2 = new User();

    user2.setId(123L);

    user2.setName("徐丽2");

    user2.setAge(29);

    List<User> users = Arrays.asList(user1,user2);

    boolean saveBatch = userService.saveBatch(users);

    //保存或更新

    //userService.saveOrUpdateBatch(users);

    System.out.println(saveBatch);

    }

    //使用service,lambda查询年龄大于25且名字包含雨的数据,一气呵成

    public void chain1() {

    List<User> users = userService.lambdaQuery().gt(User::getAge, 25).like(User::getName, "雨").list();

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

    }

    //使用service,lambda更新年龄为25的数据为26,一气呵成

    public void chain2() {

    boolean update = userService.lambdaUpdate().eq(User::getAge, 25).set(User::getAge, 26).update();

    System.out.println(update);

    }

    //使用service,lambda删除年龄为25的数据,一气呵成

    public void chain3() {

    boolean remove = userService.lambdaUpdate().gt(User::getAge, 25).remove();

    System.out.println(remove);

    }


    }


    查看全部
    0 采集 收起 来源:通用service

    采集自:慕粉4457612 2021-03-27

  • 删除记录!!

    查看全部
    0 采集 收起 来源:删除方法

    采集自:jjwangel 2021-03-25

  • 测试

    查看全部
  • 只返回特定的列:queryWrapper.select(列名字符串数组)

    查看全部
  • http://img1.mukewang.com/6014267f0001860e09490153.jpg

    第一种条件构造

    查看全部
  • @TableField(condition=SqlCondition.*)

    用于属性条件构造

    如果是时间参数:

    @TableField(condition="%s&lt;#{%s}")

    查看全部
  • http://img2.mukewang.com/60141f170001a6c309210195.jpg

    第一种查询方式


    查看全部
  • 最开始的第一步:写配置类

    查看全部
    0 采集 收起 来源:分页查询

    2021-01-29

  • 多表联查的分页:看第二行这个

    查看全部
    0 采集 收起 来源:分页查询

    2021-01-29

  • 不查总记录数,加参数false

    查看全部
    0 采集 收起 来源:分页查询

    2021-01-29

  • 方法2222

    查看全部
    0 采集 收起 来源:分页查询

    2021-01-29

  • 111111111111

    查看全部
    0 采集 收起 来源:分页查询

    2021-01-29

  • application.yml配置文件内容

    spring:
      datasource:
       driver-class-name:com.mysql.cj.jdbc.Driver
       url:jdbc:mysql://localhost:3306/mp?useSSL=false&serverTimezone=GMT%2B8
       username:root
       password:root


    查看全部
    0 采集 收起 来源:快速入门小例子

    采集自:taoy 2021-01-26

  • 继承Model<实体类>。加上@EqualsAndHashCode(callSuper=false)

    Mapper继承BaseMapper

    查看全部
    0 采集 收起 来源:AR模式

    2021-01-25

  • service中直接进行查询操作。

    查看全部
    0 采集 收起 来源:通用service

    2021-01-24

  • fieldstrategy是重点,默认忽略空,还可以不忽略,以及忽略空和空字符串

    查看全部
    0 采集 收起 来源:基本配置

    2021-01-24

  • 设置主键的自增策略,可以通过在配置文件中设置全局的策略,也可以在实体类上设置局部策略,局部优于全局。

    查看全部
    0 采集 收起 来源:主键策略

    2021-01-24

首页上一页1234567下一页尾页

举报

0/150
提交
取消
课程须知
1、有Java开发基础,了解Lambda表达式; 2、至少会使用一种关系型数据库; 3、熟悉Maven; 4、熟悉SpringBoot; 5、最好熟悉MyBatis。
老师告诉你能学到什么?
1、了解MP的基本原理及框架特点; 2、掌握MP通用Mapper的使用; 3、掌握MP常用注解的使用; 4、掌握ActiveRecord模式的使用; 5、掌握MP多种主键策略的使用; 6、掌握MP常用配置的使用; 7、掌握MP通用Service的使用; 8、掌握MP在某些应用场景下的具体使用方式。

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!