Mybatis 使用通用 mapper

时间:2022-07-26
本文章向大家介绍Mybatis 使用通用 mapper,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.1 简介

1.1.1 概述

  通用 Mapper 都可以极大的方便开发人员。可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通用方法。极其方便的使用 MyBatis 单表的增删改查。支持单表操作,不支持通用的多表联合查询。

1.1.2 相关依赖

<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper</artifactId>
    <version>4.1.5</version>
</dependency>

1.2 通用 Mapper 详解

1.2.1 修改配置

<!-- 扫描 mapper 所在的包,为 mapper 创建实现类【org 包改为 tk 包】-->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.software.ssm.mapper"></property>
</bean>

1.2.2 常用注解

注解

说明

@Table

作用:建立实体类和数据库表之间的对应关系。默认规则:实体类类名首字母小写作为表名。Employee 类 → employee 表。用法:在 @Table 注解的 name 属性中指定目标数据库表的表名

@Column

作用:建立实体类字段和数据库表字段之间的对应关系。默认规则: 实体类字段:驼峰式命名 数据库表字段:使用 “_” 区分各个单词用法:在 @Column 注解的 name 属性中指定目标字段的字段名

@ld

通用 Mapper 在执行 xxxByPrimaryKey(key) 方法时,有两种情况。情况1:没有使用 @ld 注解明确指定主键字段 情况2:使用 @ld 主键明确标记和数据库表中主键字段对应的实体类字段。

@GeneratedValue

注解作用:让通用Mapper在执行insert操作之后将数据库自动生成的主键值回写到实体类对象中。自增主键:@GeneratedValue(strategy = GenerationType.IDENTITY)

@Transient

用于标记不与数据库表字段对应的实体类字段,即忽略该字段。

1.3 通用 Mapper 接口

1.3.1 继承体系

1.3.2 继承核心接口

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/10
 * @description 继承通用 mapper 核心接口
 */
@Repository
public interface StudentMapper extends Mapper<Student> {}

1.3.3 操作接口

接口

方法

说明

SelectOneMapper<T>

T selectOne(T record)

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

SelectMapper<T>

List<T> select(T record)

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

SelectAllMapper<T>

List<T> selectAll()

查询全部结果,select(null) 方法能达到同样的效果

SelectCountMapper<T>

int selectCount(T record)

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

SelectByPrimaryKeyMapper<T>

T selectByPrimaryKey(Object key)

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

InsertMapper<T>

int insert(T record)

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

InsertSelectiveMapper<T>

int insertSelective(T record)

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

UpdateByPrimaryKeyMapper<T>

int updateByPrimaryKey(T record)

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

UpdateByPrimaryKeySelectiveMapper<T>

int updateByPrimaryKeySelective(T record)

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

DeleteMapper<T>

int delete(T record)

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

DeleteByPrimaryKeyMapper

int deleteByPrimaryKey(Object key)

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

SelectByExampleMapper<T>

List<T> selectByExample(Object example)

据Example条件进行查询,这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列

SelectCountByExampleMapper<T>

int selectCountByExample(Object example)

根据 Example 条件进行查询总数

DeleteByExampleMapper<T>

int deleteByExample(Object example)

根据 Example 条件删除数据

UpdateByExampleMapper<T>

int updateByExample(@Param(“record”) T record)

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

UpdateByExampleSelectiveMapper<T>

int updateByExampleSelective(@Param(“record”) T record)

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

SelectByConditionMapper<T>

List<T> selectByCondition(Object condition)

根据 Condition 条件进行查询 Condition 方法和 Example 方法作用完全一样只是为了避免 Example 带来的歧义,提供的的 Condition 方法

SelectCountByConditionMapper<T>

int selectCountByCondition(Object condition)

根据 Condition 条件进行查询总数

UpdateByConditionMapper<T>

int updateByCondition(@Param(“record”) T record)

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

UpdateByConditionSelectiveMapper<T>

int updateByConditionSelective(@Param(“record”) T record)

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

DeleteByConditionMapper<T>

int deleteByCondition(Object condition)

根据 Condition 条件删除数据

SelectRowBoundsMapper<T>

List<T> selectByRowBounds(T record, RowBounds rowBounds)

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

SelectByExampleRowBoundsMapper<T>

List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds)

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

SelectByConditionRowBoundsMapper<T>

List<T> selectByConditionAndRowBounds(Object condition, RowBounds rowBounds)

根据 example 条件和 RowBounds 进行分页查询,该方法和 selectByExampleAndRowBounds 完全一样,只是名字改成了 Condition

InsertListMapper<T>

int insertList(List<T> recordList)

批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等,另外该接口限制实体包含id属性并且必须为自增列

InsertUseGeneratedKeysMapper<T>

int insertUseGeneratedKeys(T record)

插入数据,限制为实体包含id属性并且必须为自增列,实体配置的主键策略无效

1.3.4 Example 接口

☞ 创建接口

// Condition 和 Example 作用完全一样
Example example = new Example(JavaBean.class);
Example.Criteria criteria = example.createCriteria();

☞ 接口方法

方法

说明

example.setOrderByClause(“字段名 ASC”)

添加升序排列条件,DESC 为降序

example.setDistinct(false)

去除重复,boolean 型,true 为选择不重复的记录

criteria.andXxxIsNull

添加字段 xxx 为 null 的条件

criteria.andXxxIsNotNull

添加字段 xxx 不为 null 的条件

criteria.andXxxEqualTo(value)

添加 xxx 字段等于 value 条件

criteria.andXxxNotEqualTo(value)

添加 xxx 字段不等于 value 条件

criteria.andXxxGreaterThan(value)

添加 xxx 字段大于 value 条件

criteria.andXxxGreaterThanOrEqualTo(value)

添加 xxx 字段大于等于 value 条件

criteria.andXxxLessThan(value)

添加 xxx 字段小于 value 条件

criteria.andXxxLessThanOrEqualTo(value)

添加 xxx 字段小于等于 value 条件

criteria.andXxxIn(List<T>)

添加 xxx 字段值在 List<T> 条件

criteria.andXxxNotIn(List<T>)

添加 xxx 字段值不在 List<T> 条件

criteria.andXxxLike("%" + value + “%”)

添加 xxx 字段值为 value 的模糊查询条件

criteria.andXxxNotLike("%" + value + “%”)

添加 xxx 字段值不为 value 的模糊查询条件

criteria.andXxxBetween(value1,value2)

添加 xxx 字段值在 value1 和 value2 之间条件

criteria.andXxxNotBetween(value1,value2)

添加 xxx 字段值不在 value1 和 value2 之间条件

1.4 示例

1.4.1 select

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/10
 * @description 简单查询
 */
@SpringJUnitConfig(locations = "classpath:application.xml")
public class Demo {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void TestSelect() {
    	// 查询所有
        List<Student> students = studentMapper.selectAll();
        System.out.println(students);

		// 匹配实体类属性查询
        Student student = new Student();
        student.setId(1L);
        List<Student> select = studentMapper.select(student);
        System.out.println(select);
    }
}

1.4.2 update

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/10
 * @description 修改
 */
@SpringJUnitConfig(locations = "classpath:application.xml")
public class Demo {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void TestUpdate() {
        // 查询参数
        Student student = new Student();
        student.setId(1L);
        // 修改前
        Student result_1 = studentMapper.selectOne(student);
        System.out.println(result_1);

        // 修改
        Student update = new Student();
        update.setId(1L);
        update.setName("王五");
        studentMapper.updateByPrimaryKeySelective(update);

        // 修改后
        Student result_2 = studentMapper.selectOne(student);
        System.out.println(result_2);
    }
}

1.4.3 insert

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/10
 * @description 新增
 */
@SpringJUnitConfig(locations = "classpath:application.xml")
public class Demo {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void TestInsert() {
    	// 新增数据
        Student student = new Student();
        student.setName("张良");
        student.setAge(800);

		// 新增操作
        studentMapper.insertSelective(student);

		// 打印,新增成功之后可以从实体类对象中获取 id
        System.out.println(student);
    }
}

1.4.4 delete

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/10
 * @description 删除
 */
@SpringJUnitConfig(locations = "classpath:application.xml")
public class Demo {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void TestDel() {
        // 删除参数
        Student student = new Student();
        student.setId(1L);

        // 删除操作
        studentMapper.delete(student);

        // 查询
        Student result = studentMapper.selectOne(student);
        System.out.println(result);
    }
}

1.4.5 Example

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/10
 * @description Example 实例
 */
@SpringJUnitConfig(locations = "classpath:application.xml")
public class Demo {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void Test() {
        Example example = new Example(Student.class);

        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", 2L)
                .andLike("name", "%李%");

        List<Student> students = studentMapper.selectByExample(example);
        System.out.println(students);
    }
}