tk-mybatis 的使用方法詳解
tkmybatis是在mybatis框架的基礎(chǔ)上提供了很多工具,讓開(kāi)發(fā)更加高效,下面來(lái)看看這個(gè)框架的基本使用,后面會(huì)對(duì)相關(guān)源碼進(jìn)行分析,感興趣的同學(xué)可以看一下,挺不錯(cuò)的一個(gè)工具
實(shí)現(xiàn)對(duì)員工表的增刪改查的代碼?
java的dao層接口
public interface WorkerMapper extends Mapper<Worker> {
}
實(shí)體對(duì)象
@Table(name = "worker")
public class Worker {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "worker_id")
private String workerId;
private String name;
@Column(name = "org_id")
private Integer orgId;
private String status;
@Column(name = "role_id")
private Integer roleId;
// getters and setters ...
}
以上就是實(shí)現(xiàn)對(duì)Worker進(jìn)行增刪改查的所有代碼,包括選擇性更新、插入、刪除等,所有的方法列表如下

以后對(duì)表字段的添加或修改只需要更改實(shí)體對(duì)象的注解,不需要修改xml映射文件,如將worker_id改成worker_no
@Column(name = "worker_no") private String workerNo;
數(shù)據(jù)源的配置,只需要將org.mybatis.spring.mapper.MapperScannerConfigurer改成tk.mybatis.spring.mapper.MapperScannerConfigurer,然后加一個(gè)屬性?
,也可不加,因?yàn)榭蚣芴峁┝四J(rèn)實(shí)現(xiàn)
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.jjs.zanbi.dao" />
<property name="properties">
<value>
mappers=tk.mybatis.mapper.common.Mapper
</value>
</property>
</bean>
通用Service類
/**
* Created by Kaiwen
*/
@Service
public abstract class CommonServiceImpl<T,PK extends Serializable> implements CommonService<T,PK> {
/**
* 泛型注入
*/
@Autowired
private Mapper<T> mapper;
public T selectByPrimaryKey(PK entityId) {
return mapper.selectByPrimaryKey(entityId);
}
public int deleteByPrimaryKey(PK entityId) {
return mapper.deleteByPrimaryKey(entityId);
}
public int insert(T record) {
return mapper.insert(record);
}
public int insertSelective(T record) {
return mapper.insertSelective(record);
}
public int updateByPrimaryKeySelective(T record) {
return mapper.updateByPrimaryKeySelective(record);
}
public int updateByPrimaryKey(T record) {
return mapper.updateByPrimaryKey(record);
}
public List<T> selectByExample(Example example) {
return mapper.selectByExample(example);
}
}
到此這篇關(guān)于tk-mybatis 的使用方法詳解的文章就介紹到這了,更多相關(guān)tk-mybatis使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)對(duì)Http接口進(jìn)行監(jiān)控的代碼
Spring Boot Actuator是Spring Boot提供的一個(gè)模塊,用于監(jiān)控和管理Spring Boot應(yīng)用程序的運(yùn)行時(shí)信息,本文將介紹一下Spring Boot Actuator以及代碼示例,以及如何進(jìn)行接口請(qǐng)求監(jiān)控,需要的朋友可以參考下2024-07-07
使用Java解析JSON數(shù)據(jù)并提取特定字段的實(shí)現(xiàn)步驟(以提取mailNo為例)
在現(xiàn)代軟件開(kāi)發(fā)中,處理JSON數(shù)據(jù)是一項(xiàng)非常常見(jiàn)的任務(wù),無(wú)論是從API接口獲取數(shù)據(jù),還是將數(shù)據(jù)存儲(chǔ)為JSON格式,解析和提取JSON中的特定字段都是開(kāi)發(fā)人員需要掌握的基本技能,本文將以一個(gè)實(shí)際案例為例,詳細(xì)介紹如何使用Java解析JSON數(shù)據(jù)并提取其中的mailNo字段2025-01-01
mybatis-plus @DS實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源原理
本文主要介紹了mybatis-plus @DS實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
反射機(jī)制:getDeclaredField和getField的區(qū)別說(shuō)明
這篇文章主要介紹了反射機(jī)制:getDeclaredField和getField的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot接口或方法進(jìn)行失敗重試的實(shí)現(xiàn)方式
為了防止網(wǎng)絡(luò)抖動(dòng),影響我們核心接口或方法的成功率,通常我們會(huì)對(duì)核心方法進(jìn)行失敗重試,如果我們自己通過(guò)for循環(huán)實(shí)現(xiàn),會(huì)使代碼顯得比較臃腫,所以本文給大家介紹了SpringBoot接口或方法進(jìn)行失敗重試的實(shí)現(xiàn)方式,需要的朋友可以參考下2024-07-07
SpringAOP如何修改請(qǐng)求參數(shù)列表
這篇文章主要介紹了SpringAOP如何修改請(qǐng)求參數(shù)列表問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

