tk-mybatis 的使用方法詳解
tkmybatis是在mybatis框架的基礎上提供了很多工具,讓開發(fā)更加高效,下面來看看這個框架的基本使用,后面會對相關源碼進行分析,感興趣的同學可以看一下,挺不錯的一個工具
實現(xiàn)對員工表的增刪改查的代碼?
java的dao層接口
public interface WorkerMapper extends Mapper<Worker> {
}
實體對象
@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 ...
}
以上就是實現(xiàn)對Worker進行增刪改查的所有代碼,包括選擇性更新、插入、刪除等,所有的方法列表如下

以后對表字段的添加或修改只需要更改實體對象的注解,不需要修改xml映射文件,如將worker_id改成worker_no
@Column(name = "worker_no") private String workerNo;
數(shù)據(jù)源的配置,只需要將org.mybatis.spring.mapper.MapperScannerConfigurer改成tk.mybatis.spring.mapper.MapperScannerConfigurer,然后加一個屬性?
,也可不加,因為框架提供了默認實現(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);
}
}
到此這篇關于tk-mybatis 的使用方法詳解的文章就介紹到這了,更多相關tk-mybatis使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot實現(xiàn)對Http接口進行監(jiān)控的代碼
Spring Boot Actuator是Spring Boot提供的一個模塊,用于監(jiān)控和管理Spring Boot應用程序的運行時信息,本文將介紹一下Spring Boot Actuator以及代碼示例,以及如何進行接口請求監(jiān)控,需要的朋友可以參考下2024-07-07
使用Java解析JSON數(shù)據(jù)并提取特定字段的實現(xiàn)步驟(以提取mailNo為例)
在現(xiàn)代軟件開發(fā)中,處理JSON數(shù)據(jù)是一項非常常見的任務,無論是從API接口獲取數(shù)據(jù),還是將數(shù)據(jù)存儲為JSON格式,解析和提取JSON中的特定字段都是開發(fā)人員需要掌握的基本技能,本文將以一個實際案例為例,詳細介紹如何使用Java解析JSON數(shù)據(jù)并提取其中的mailNo字段2025-01-01
mybatis-plus @DS實現(xiàn)動態(tài)切換數(shù)據(jù)源原理
本文主要介紹了mybatis-plus @DS實現(xiàn)動態(tài)切換數(shù)據(jù)源原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
SpringBoot接口或方法進行失敗重試的實現(xiàn)方式
為了防止網(wǎng)絡抖動,影響我們核心接口或方法的成功率,通常我們會對核心方法進行失敗重試,如果我們自己通過for循環(huán)實現(xiàn),會使代碼顯得比較臃腫,所以本文給大家介紹了SpringBoot接口或方法進行失敗重試的實現(xiàn)方式,需要的朋友可以參考下2024-07-07

