• 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);
        }
    
  • 相关阅读:
    数据库同步软件介绍以及使用说明(SyncNavigator多元异构数据实时同步工具)
    关于异构数据库的不同表之间数据同步的操作细节---syncnavigator同步工具实操
    ASP.NET Core 配置文件(无处不在的依赖注入)
    ASP.NET Core 开源项目整理
    性能差异 ASP.NET WebForm与ASP.NET MVC
    MySQL 分区知识点(三)
    Docker 资料
    MySQL 基础知识(基本架构、存储引擎差异)
    MySQL InnoDB与MyISAM存储引擎差异
    MySQL 索引知识整理(创建高性能的索引)
  • 原文地址:https://www.cnblogs.com/zhuxiang1029/p/14963083.html
Copyright © 2020-2023  润新知