码迷,mamicode.com
首页 > 其他好文 > 详细

mybaits-plus总结

时间:2020-02-04 15:35:56      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:frame   方法   new   boolean   执行sql   get   core   wrap   res   

 

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import cn.hutool.core.date.DateUtil;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ebc.numen.Application;
import com.ebc.numen.entity.SysUser;
import com.ebc.numen.service.SysUserService;
/**
 * 单表操作例子
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class TestSingle {
    @Resource
    private SysUserService sysUserService;
    /**
     * 添加
     * <pre>
     * 1.不需要手工设置id
     * 2.不需要手工设置version、create_datetime、update_datetime(因为已自动填充)
     * 3.delete不填,数据库允许空
     * </pre>
     */
    @Test
    public void insert() {
        for (int i = 0; i < 30; i++) {
            SysUser user = new SysUser();
            user.setOrgId(1L);
            user.setAge(10);
            user.setName("遥远"+i);
            user.setScope(BigDecimal.valueOf(1.23));
            boolean isSucc = sysUserService.insert(user);
            log.info("{},主键id:{}",isSucc,user.getId());
        }
    }
    /**
     * 修改~不获取对象
     * <pre>
     * 1.没设置version,导致数据库version值不变,乐观锁没生效。
     * 如果知道当前version值,也可以手工设置.
     * 2.update_datetime自动填充
     * </pre>
     */
    @Test
    public void update1() {
        SysUser user = new SysUser();
        user.setId(1024613506376708097L);
        user.setName("www");
        //user.setVersion(7);
        boolean isSucc = sysUserService.updateById(user);
        log.info("{}",isSucc);
    }
    /**
     * 修改~获取对象
     * <pre>
     * 乐观锁生效。而且不需要自行+1,数据库当前值即可。
     * </pre>
     */
    @Test
    public void update2() {
        SysUser user = sysUserService.selectById(1024613506376708097L);
        user.setName("eee");
        
        boolean isSucc = sysUserService.updateById(user);
        if (isSucc) {
            log.info("修改成功");
        } else {
            log.info("修改失败,由于乐观锁,可能已被别人修改过了。如果想在修改,请重新获取该条记录后修改。");
        }
    }
    /**
     * <pre>
     * 如果将某个字段置空,用updateAllColumnById方法
     * 如果部分字段修改,用updateById方法
     * </pre>
     */
    @Test
    public void update3() {
        SysUser user = sysUserService.selectById(1024613506376708097L);
        user.setName(null);
        
        boolean isSucc = sysUserService.updateAllColumnById(user);
        if (isSucc) {
            log.info("修改成功");
        } else {
            log.info("修改失败,由于乐观锁,可能已被别人修改过了。如果想在修改,请重新获取该条记录后修改。");
        }
    }
    /**
     * 物理删除
     * <pre>
     * DELETE FROM t_sys_user WHERE id=?
     * </pre> 
     */
    @Test
    public void delete() {
        boolean isSucc = sysUserService.deleteById(1024613254420688898L);
        log.info("{}",isSucc);
    }
    /**
     * 逻辑删除
     * <pre>
     * 需在SysUser类中的deleted上边的注解放开
     * 即:<code>@TableLogic</code>
     * 
     * 执行后结果:
     * UPDATE t_sys_user SET deleted=1 WHERE id=? 
     * </pre>
     */
    @Test
    public void deleteLogic() {
        boolean isSucc = sysUserService.deleteById(1024839982808219649L);
        log.info("{}",isSucc);
    }
    /**
     * 指定条件删除
     * <pre>
     * 执行sql:
     * DELETE FROM t_sys_user WHERE (age = ? AND create_datetime >= ? AND name LIKE ?) 
     * 10(Integer), 2018-08-02 09:37:06.0(Timestamp), 遥远%(String)
     * </pre>
     */
    @Test
    public void deleteByWrapper() {
        String dateStr = "2018-08-02 09:37:06";
        Date date = DateUtil.parse(dateStr);
        EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
        ew.eq("age", 10);
        ew.ge("create_datetime", date);//字符串日期也可。
        ew.like("name", "遥远", SqlLike.RIGHT);
        boolean isSucc = sysUserService.delete(ew);//ew为null,则全部删除
        log.info("{}",isSucc);
    }
    /**
     * 分页查询
     * <pre>
     * Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (age = ? AND create_datetime >= ? AND update_datetime NOT BETWEEN ? AND ? AND name LIKE ?) LIMIT 0,10
     * Parameters: 10(Integer), 2018-08-02 09:37:06(String), 2018-08-01 19:11:10(String), 2018-08-02 09:37:06(String), 遥远%(String)
     * </pre>
     */
    @Test
    public void selectPage() {
        Page<SysUser> page = new Page<SysUser>(1,10);
        EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
        ew.eq("age", 10);
        ew.ge("create_datetime", "2018-08-02 09:37:06");//Date类型 也可以。
        ew.notBetween("update_datetime", "2018-08-01 19:11:10","2018-08-02 09:37:06");
        ew.like("name", "遥远", SqlLike.RIGHT);
        Page<SysUser> p = sysUserService.selectPage(page,ew);
        log.info("{}",p);
        log.info("{}",p.getRecords());
    }
    /**
     * <pre>
     * 查询多个id的对象集合
     * 执行sql:
     * Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE id IN ( ? , ? ) 
     * Parameters: 1024839982728527874(Long), 1024831423877894145(Long)
     * </pre>
     */
    @Test
    public void selectBatchIds() {
        List<Long> ids = Arrays.asList(1024839982728527874L, 1024831423877894145L);
        List<SysUser> list = sysUserService.selectBatchIds(ids);
        list.stream().forEach(s -> System.out.println(s));
    }
    /**
     * 获取符合条件的第1条
     * <pre>
     * Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (name LIKE ?) 
     * Parameters: 遥远%(String)
     * </pre>
     */
    @Test
    public void selectOne() {
        EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
        ew.like("name", "遥远", SqlLike.RIGHT);
        SysUser user = sysUserService.selectOne(ew);
        log.info("{}",user);
    }
    /**
     * 获取符合条件的所有记录
     * <pre>
     * Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (name LIKE ?) 
     * Parameters: 遥远%(String)
     * </pre>
     */
    @Test
    public void selectList() {
        EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
        ew.like("name", "遥远", SqlLike.RIGHT);
        List<SysUser> list = sysUserService.selectList(ew);
        list.stream().forEach(s -> System.out.println(s));
    }
    /**
     * 操作符:in
     * <pre>
     * Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (name IN (?,?,?) AND org_id NOT IN (?,?))
     * Parameters: 遥远0(String), 遥远2(String), 遥远3(String), 2(String), 3(String)
     * </pre>
     */
    @Test
    public void operate_in() {
        EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
        ew.in("name", "遥远0,遥远2,遥远3");//字符串
        ew.notIn("org_id", "2,3");//整数
        List<SysUser> list = sysUserService.selectList(ew);
        list.stream().forEach(s -> System.out.println(s));
    }
    /**
     * 操作符:between
     * <pre>
     * Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (create_datetime BETWEEN ? AND ?)
     * Parameters: 2018-08-01 19:11:10(String), 2018-08-02 09:37:06(String)
     * </pre>
     */
    @Test
    public void operate_between() {
        EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
        ew.between("create_datetime", "2018-08-01 19:11:10","2018-08-02 09:37:06");//字符串或者Date均可
        List<SysUser> list = sysUserService.selectList(ew);
        list.stream().forEach(s -> System.out.println(s));
    }
    /**
     * 将查询结果封装到Map<String, Object>
     * <pre>
     * Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (create_datetime BETWEEN ? AND ?)
     * Parameters: 2018-08-01 19:11:10(String), 2018-08-02 09:37:06(String)
     * 
     * </pre>
     */
    @Test
    public void selectMaps() {
        EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
        ew.between("create_datetime", "2018-08-01 19:11:10","2018-08-02 09:37:06");//字符串或者Date均可
        List<Map<String, Object>> list = sysUserService.selectMaps(ew);
        //打印如下:
        //{createDatetime=2018-08-01 19:11:10.0, updateDatetime=2018-08-01 19:11:10.0, scope=1.23, name=22, id=1024613506376708097, version=0, orgId=1, age=10}
        //{createDatetime=2018-08-02 09:37:06.0, updateDatetime=2018-08-02 09:37:06.0, scope=1.23, name=11, id=1024831423877894145, version=0, orgId=1, age=10}
        list.stream().forEach(s -> System.out.println(s));
    }
    /**
     * 只返回第一个字段值
     * <pre>
     * Preparing: SELECT id AS id FROM t_sys_user WHERE (create_datetime BETWEEN ? AND ?) 
     * Parameters: 2018-08-01 19:11:10(String), 2018-08-02 09:37:06(String)
     * </pre>
     */
    @Test
    public void selectObjs() {
        EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
        ew.between("create_datetime", "2018-08-01 19:11:10","2018-08-02 09:37:06");//字符串或者Date均可
        List<Object> list = sysUserService.selectObjs(ew);
        //打印如下:
        //1024613506376708097
        //1024831423877894145
        list.stream().forEach(s -> System.out.println(s));
    }
}

 

mybaits-plus总结

标签:frame   方法   new   boolean   执行sql   get   core   wrap   res   

原文地址:https://www.cnblogs.com/yaoyuan2/p/12259500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!