TK.Mapper

核心API

增
Mapper.insert(record);

保存一个实体,null的属性也会保存,不会使用数据库默认值

Mapper.insertSelective(record);

保存一个实体,null的属性不会保存,会使用数据库默认值

删
Mapper.delete(record);

根据实体属性作为条件进行删除,查询条件使用等号

Mapper.deleteByExample(example)

根据Example条件删除数据

Mapper.deleteByPrimaryKey(key)

根据主键字段进行删除,方法参数必须包含完整的主键属性

改
Mapper.updateByExample(record, example)

根据Example条件更新实体`record`包含的全部属性,null值会被更新

Mapper.updateByExampleSelective(record, example)

根据Example条件更新实体`record`包含的不是null的属性值

Mapper.updateByPrimaryKey(record)

根据主键更新实体全部字段,null值会被更新

Mapper.updateByPrimaryKeySelective(record)

根据主键更新属性不为null的值

查
Mapper.select(record)

根据实体中的属性值进行查询,查询条件使用等号

Mapper.selectAll()

查询全部结果

Mapper.selectByExample(example)

根据Example条件进行查询

Mapper.selectByExampleAndRowBounds(example, rowBounds)

根据example条件和RowBounds进行分页查询

Mapper.selectByPrimaryKey(key)

根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号

Mapper.selectByRowBounds(record, rowBounds)

根据实体属性和RowBounds进行分页查询

Mapper.selectCount(record)

根据实体中的属性查询总数,查询条件使用等号

Mapper.selectCountByExample(example)

根据Example条件进行查询总数

Mapper.selectOne(record)

根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号

Example 查询语句

 //注意:(addEqualTo)这里的userId是映射的实体类。
    @Test
    public void selectAllTest2() {
        Example example = new Example(Category.class);
        example.createCriteria()
                .andEqualTo("categoryID",1)
                .andEqualTo("categoryName","Beverages");

        List<Category> categories = categoryDao.selectByExample(example);
        System.out.println(categories);
        assertEquals(true, categories.size() > 0);
    }

    //注意:(addCondition)这里的user_id是数据库的字段。即where后面的条件。应该写sql语句。
    @Test
    public void selectAllTest3() {
        Example example = new Example(Category.class);
        example.createCriteria()
                .andCondition("category_id=",1)
                .andCondition("category_name=","Beverages");

        List<Category> categories = categoryDao.selectByExample(example);
        System.out.println(categories);
        assertEquals(true, categories.size() > 0);
    }

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐