TkMybatis常用方法

本文详细介绍了Java中对SysUser表进行全量查询、主键查询、按条件更新(部分和全部字段)、按主键删除以及各种条件下的筛选查询,包括精确匹配、模糊查询和时间范围查询。此外,还展示了如何使用Example类进行复杂条件更新和删除操作。
摘要由CSDN通过智能技术生成

1、查询全部

    public List<SysUser> getUserList() {
        return sysUserMapper.selectAll();
    }

3、按主键查询

    public SysUser getUserById(Long id) {
    
        return  sysUserMapper.selectByPrimaryKey(id);
    }

4、按主键修改(只更新有值的字段)

    public void update(SysUser user) {

        sysUserMapper.updateByPrimaryKeySelective(user);
    }

5、按主键修改(更新所有字段)

    public void update(SysUser user) {

        sysUserMapper.updateByPrimaryKey(user);
    }

6、按主键删除

    public void delete(Long id) {
    
        sysUserMapper.deleteByPrimaryKey(id);
    }

7、按条件精确查询

    public List<SysUser> getUserList(String loginName) {
        Example example = new Example(SysUser.class);
        if(StringUtils.isNotBlank(loginName)){
            example.createCriteria().andEqualTo("loginName", loginName);
        }
        return sysUserMapper.selectByExample(example);
    }

8、按条件模糊查询

    public List<SysUser> getUserList(String loginName) {
        Example example = new Example(SysUser.class);
        if(StringUtils.isNotBlank(loginName)){
            example.createCriteria().andLike("loginName", "%" + loginName + "%");
        }
        return sysUserMapper.selectByExample(example);
    }

9、按时间段查询并排序

    public List<SysUser> getUserList(String startDate, String endDate) {
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(startDate)) {
            criteria.andGreaterThanOrEqualTo("createTime", startDate);
        }
        if (StringUtils.isNotBlank(endDate)) {
            criteria.andLessThanOrEqualTo("createTime", endDate);
        }
        example.setOrderByClause("create_time desc");
        return sysUserMapper.selectByExample(example);
    }

10、按条件修改(只更新有值的字段)

    public void update(SysUser user) {
        Example example = new Example(SysUser.class);
        example.createCriteria().andEqualTo("loginName", user.getLoginName());
        sysUserMapper.updateByExampleSelective(user, example);
    }

11、按条件修改(更新所有字段)

    public void update(SysUser user) {
        Example example = new Example(SysUser.class);
        example.createCriteria().andEqualTo("loginName", user.getLoginName());
        sysUserMapper.updateByExample(user, example);
    }

12、按条件删除

    public void delete(String loginName) {
        Example example = new Example(SysUser.class);
        example.createCriteria().andEqualTo("loginName", loginName);
        sysUserMapper.deleteByExample(example);
    }

13、复杂条件查询

    public List<SysUser> getUserList(String loginName, String password) {
        Example example = new Example(SysUser.class);
        Example.Criteria criteria1 = example.createCriteria();
        criteria1.andEqualTo("loginName", loginName);
        criteria1.andEqualTo("password", password);
        Example.Criteria criteria2 = example.createCriteria();
        criteria2.andEqualTo("email", loginName);
        criteria2.andEqualTo("password", password);
        example.or(criteria2);
        return sysUserMapper.selectByExample(example);
    }

SELECT * FROM sys_user WHERE ((login_name = ? AND password = ?) OR (email = ? AND password = ?)) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值