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 常用注解
1.3 通用 Mapper 接口
1.3.1 继承体系
1.3.2 继承核心接口
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/10 * @description 继承通用 mapper 核心接口 */@Repositorypublic interface StudentMapper extends Mapper<Student> {}
1.3.3 操作接口
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 完全一样,只是名字改成了 ConditionInsertListMapper<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();
☞ 接口方法
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 {@Autowiredprivate StudentMapper studentMapper;@Testpublic 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 {@Autowiredprivate StudentMapper studentMapper;@Testpublic 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 {@Autowiredprivate StudentMapper studentMapper;@Testpublic void TestInsert() {// 新增数据Student student = new Student();student.setName("张良");student.setAge(800);// 新增操作studentMapper.insertSelective(student);// 打印,新增成功之后可以从实体类对象中获取 idSystem.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 {@Autowiredprivate StudentMapper studentMapper;@Testpublic 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 {@Autowiredprivate StudentMapper studentMapper;@Testpublic 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);}}
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。