網(wǎng)站首頁 編程語言 正文
最近有在用到mybatis的通用mapper的查詢,既然接觸了索性記錄總結(jié)一下,以便幫到后來人。
一. 首先,放上mybatis 通用mapper的接口代碼Mapper.class:
package tk.mybatis.mapper.common;
import tk.mybatis.mapper.annotation.RegisterMapper;
@RegisterMapper
public interface Mapper<T> extends BaseMapper<T>, ExampleMapper<T>, RowBoundsMapper<T>, Marker {
}
對于我們項目來說,很多對數(shù)據(jù)庫表的自定義mapper映射都是繼承自通用Mapper的,要想繼承,就必須指定泛型。
拿我項目來舉例子:假設(shè)我有一實體類 Student.java,與我數(shù)據(jù)庫里的Student表屬性、字段一一對應(yīng),那么我想使用通用Mapper的各種方法,就要這么實現(xiàn):
在mapper文件夾下新建StudentMapper.java文件,代碼如下:
package com.project.business.mapper;
import com.project.common.model.business.Student;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.common.Mapper;
/**
* @Author cyl
* @Date 2021/10/15 09:55
* @Version 1.0
**/
@Component
public interface StudentMapper extends Mapper<Student> {
}
二. 在自己的service目錄下定義好service以及serviceIml實現(xiàn),在實現(xiàn)類中通過注解@Autowired注入StudentMapper對象作為屬性;然后在類中的方法里便可以使用通用mapper的各類方法了。
@Transactional
@Service
public class StudentServiceImpl implements StudentService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private StudentMapper studentMapper;
@Override
public StudentVO findInfo(String s_id) throws BusinessException
{}
2.1 selectByPrimaryKey()主鍵查詢
使用主鍵查詢時,要注意幾個地方:
- 主鍵只有一個,多個不起作用;
- 待查詢的主鍵,在***對應(yīng)實體類屬性上必須標有 @Id***,該方法才能識別它為主鍵且按照主鍵去查詢,否則只會將所有字段作為聯(lián)合主鍵來查詢!
具體使用:
Student student = studentMapper.selectByPrimaryKey(s_id);
if(student == null) {
throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR, s_id + " 該學(xué)生不存在");
}
2.2 非主鍵字段使用selectByExample()查詢
當我想查詢的字段不是主鍵時,可以調(diào)用selectByExample()方法:
Example o = new Example(Student.class);
Example.Criteria criteria = o.createCriteria();
o.setOrderByClause("s_score desc"); // 查詢條件按成績降序排序
criteria.andLike("s_name","%"+s_name+"%"); // 按名字模糊查詢
criteria.andNotEqualTo("deleted",1); // 查詢未被刪除的學(xué)生(deleted 1為已被刪除)
List<Student> students= studentMapper.selectByExample(o); // 普通查詢
logger.info("query result students size: " + students.size());
if(students.size() > 0) {
logger.info(" result s_id: " + students.get(0).getS_id());
}
對于Example的解釋:
Mybatis的逆向工程中會生成實例及實例對應(yīng)的Example,Example用于添加條件,等同于SQL語句WHERE關(guān)鍵字后的篩選條件部分。
以上僅記錄主鍵查詢和普通查詢實例,更多詳細增刪改查等方法使用看
另外一篇文章專門記錄通用mapper常用函數(shù)以及Example常用篩選條件:
參考博客:
- Mybatis通用Mapper使用詳解:https://www.jianshu.com/p/5854bfd0f0e6
原文鏈接:https://blog.csdn.net/qq_36256590/article/details/121436404
相關(guān)推薦
- 2023-01-28 Flutter?Widget移動UI框架使用Material和密匙Key實戰(zhàn)_Android
- 2022-04-16 實例講解python讀取各種文件的方法_python
- 2022-07-04 Android自定義view利用PathEffect實現(xiàn)動態(tài)效果_Android
- 2022-11-12 NetCore?配置Swagger的詳細代碼_實用技巧
- 2022-08-03 C++類與對象深入之引用與內(nèi)聯(lián)函數(shù)與auto關(guān)鍵字及for循環(huán)詳解_C 語言
- 2022-05-09 Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表,無序鏈表詳解_python
- 2022-07-10 Popconfirm氣泡確認框無法觸發(fā)confirm函數(shù)
- 2023-09-18 document.getElementById()獲取一直為null
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支