TKmybatis的一些常用类

增、删、改

1.InsertSelectiveMapper

接口:InsertSelectiveMapper
方法:int insertSelective(T record);
说明:保存一个实体,null的属性不会保存,会使用数据库默认值

2.UpdateByPrimaryKeySelectiveMapper

接口:UpdateByPrimaryKeySelectiveMapper
方法:int updateByPrimaryKeySelective(T record);
说明:根据主键更新属性不为null的值

3.DeleteByPrimaryKeyMapper

接口:DeleteByPrimaryKeyMapper
方法:int deleteByPrimaryKey(Object key);
说明:根据主键字段进行删除,方法参数必须包含完整的主键属性

查询

1.SelectMapper

接口:SelectMapper
方法:List select(T record);
说明:根据实体中的属性值进行查询,查询条件使用等号

2.SelectByPrimaryKeyMapper

接口:SelectByPrimaryKeyMapper
方法:T selectByPrimaryKey(Object key);
说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号

3.SelectCountMapper

接口:SelectCountMapper
方法:int selectCount(T record);
说明:根据实体中的属性查询总数,查询条件使用等号

4.SelectByExampleMapper

接口:SelectByExampleMapper
方法:List selectByExample(Object example);
说明:根据Example条件进行查询
重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列

public List<TestUser> selectExample() {
Example example = new Example(TestUser.class);
//排序方法setOrderByClause("字段名 ASC")DESC降序
example.setOrderByClause("name ASC");
example.createCriteria()
//添加xxx字段等于value条件
.andEqualTo("password","123456")
//模糊查询xxx字段like value条件
.andLike("name","%四%")
//可以自由拼接SQL
//.andCondition("ID = '5f7139ef295d42a3b964c082e0dd838f' ")
//或者可以这么写
.andCondition("ID =","5f7139ef295d42a3b964c082e0dd838f");
        return testUserMapper.selectByExample(example);
    }

mybatis的逆向工程中会生成实例及实例对应的example,example用于添加条件,相当where后面的部分
Example example = new Example();
Criteria criteria = example.createCriteria();

例子(谷仓项目批量同意邀请)

List<Long> invitationIdLsit = Arrays.asList(invitationIds);
//条件查询要更新的邀请数据
Example example1 = new Example(MissionInvitation.class);
example1.createCriteria().andIn("id",invitationIdLsit).andEqualTo("statusNo",0);

分页,用pagehelper

    /**
     * 分页查询
     * @param page  当前页
     * @param size  每页显示的条数
     * @return
     */
    @Override
    public PageInfo<Brand> pageSearch(Integer page, Integer size) {
        // 分页实现
        // 后面的查询必须是紧跟集合查询
        PageHelper.startPage(page, size);
        // 查询集合
        List<Brand> brands = brandMapper.selectAll();
        return new PageInfo<Brand>(brands);
    }
原文地址:https://www.cnblogs.com/zhuxiang1029/p/14963083.html