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);
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/16815.html
標籤:其他
上一篇:vue側邊導航欄
