springboot整合tkmybatis方式以及使用過程
經(jīng)常用mybatis的的都知道,使用mybatis orm框架存在一個(gè)非常不友善的問題就是,就是每操作一個(gè)單表就需要自己手寫一個(gè)xml文件,雖然說可以用工具生成xml和實(shí)體類可以解決這個(gè)問題,但是二次開發(fā)的時(shí)候?qū)δ硞€(gè)表字段進(jìn)行修改的時(shí)候,生成xml文件就不現(xiàn)實(shí)啦。
最近發(fā)現(xiàn)tkmybatis就非常好的解決了這個(gè)問題。在這里和大家分享一下。
框架配置
這里需要引用到的包
<!--mybatis操作數(shù)據(jù)庫有關(guān)-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--連接mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--使用阿里巴巴的druid數(shù)據(jù)源,有利于監(jiān)控sql的執(zhí)行情況-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--通用mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!--添加lombok支持,可以省掉寫get和set方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
<optional>true</optional>
</dependency>
<!--使用fastjson來操作json數(shù)據(jù)-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<!--spring boot web方面的支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>數(shù)據(jù)源配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver表結(jié)構(gòu)語句
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `user_phone` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, INDEX `user_name_index`(`user_name`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, 'evan', '26'); INSERT INTO `user` VALUES (2, 'evan11', '26');
類配置方法
實(shí)體類方法
注意這里是引用了lombok框架,可以省略寫get,set之類的方法,這里需要idea里面添加lombok插件,如果有報(bào)錯(cuò)的話。(如果有強(qiáng)迫癥的話)其實(shí)還是代碼還是可以跑起來的。
@Data
@Table(name="user")
public class UserModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY,generator = "JDBC")
private Integer id;
@Column
private String userName;
@Column
private String userPhone;
}
其中@Table即數(shù)據(jù)表表名,@Column即列名,@Id作為主鍵,需要注意,@Id注解不可有多個(gè),@Transient即冗余字段,不與數(shù)據(jù)庫任何字段對(duì)應(yīng)。
注意多數(shù)據(jù)源的情況
則@Table注解中可以寫成“{數(shù)據(jù)庫名}.{架構(gòu)名}.{表名}”,如:@Table(name=“db.dbo.tableName”)

Service類
這里主要是實(shí)現(xiàn)了上邊BaseMapper中繼承的5個(gè)Mapper的方法。
tk.mybatis.mapper.common.BaseMapper中有較多方法,均需要繼承實(shí)現(xiàn):
/** * 保存一個(gè)實(shí)體,null屬性也會(huì)保存 * * @param record * @return */ int insert(T record); /** * 保存一個(gè)實(shí)體,null屬性不會(huì)保存 * * @param record * @return */ int insertSelective(T record); /** * 根據(jù)實(shí)體屬性作為條件進(jìn)行刪除,查詢條件使用等號(hào) */ int delete(T record); /** * 根據(jù)主鍵更新屬性不為null的值 */ int updateByPrimaryKeySelective(T record); /** * 根據(jù)實(shí)體中的屬性值進(jìn)行查詢,查詢條件使用等號(hào) */ List<T> select(T record); /** * 查詢?nèi)拷Y(jié)果,select(null)方法能達(dá)到同樣的效果 */ List<T> selectAll(); /** * 根據(jù)實(shí)體中的屬性進(jìn)行查詢,只能有一個(gè)返回值,有多個(gè)結(jié)果是拋出異常,查詢條件使用等號(hào) */ T selectOne(T record); /** * 根據(jù)實(shí)體中的屬性查詢總數(shù),查詢條件使用等號(hào) */ int selectCount(T record);
MySqlMapper中的方法如下:
/** * 批量插入,支持批量插入的數(shù)據(jù)庫可以使用,例如MySQL,H2等,另外該接口限制實(shí)體包含`id`屬性并且必須為自增列 */ public int insertList(List<T> recordList); /** * 插入數(shù)據(jù),限制為實(shí)體包含`id`屬性并且必須為自增列,實(shí)體配置的主鍵策略無效 */ public int insertUseGeneratedKeys(T record);
IdsMapper中的方法如下:
/** * 根據(jù)主鍵@Id進(jìn)行查詢,多個(gè)Id以逗號(hào),分割 * @param id * @return */ List<T> selectByIds(String ids); /** * 根據(jù)主鍵@Id進(jìn)行刪除,多個(gè)Id以逗號(hào),分割 * @param id * @return */ int deleteByIds(String ids);
ConditionMapper中的方法如下:
/** * 根據(jù)Condition條件進(jìn)行查詢 */ public List<T> selectByCondition(Object condition); /** * 根據(jù)Condition條件進(jìn)行查詢 */ public int selectCountByCondition(Object condition); /** * 根據(jù)Condition條件刪除數(shù)據(jù),返回刪除的條數(shù) */ public int deleteByCondition(Object condition); /** * 根據(jù)Condition條件更新實(shí)體`record`包含的全部屬性,null值會(huì)被更新,返回更新的條數(shù) */ public int updateByCondition(T record, Object condition); /** * 根據(jù)Condition條件更新實(shí)體`record`包含的全部屬性,null值會(huì)被更新,返回更新的條數(shù) */ public int updateByConditionSelective(T record, Object condition);
ExampleMapper中的方法如下:
/** * 根據(jù)Example條件進(jìn)行查詢 */ public List<T> selectByExample(Object example); /** * 根據(jù)Example條件進(jìn)行查詢,若有多條數(shù)據(jù)則拋出異常 */ public T selectOneByExample(Object example); /** * 根據(jù)Example條件進(jìn)行查詢總數(shù) */ public int selectCountByExample(Object example); /** * 根據(jù)Example條件刪除數(shù)據(jù),返回刪除的條數(shù) */ public int deleteByExample(Object example); /** * 根據(jù)Example條件更新實(shí)體`record`包含的全部屬性,null值會(huì)被更新,返回更新的條數(shù) */ public int updateByExample(T record, Object example); /** * 根據(jù)Example條件更新實(shí)體`record`包含的不是null的屬性值,返回更新的條數(shù) */ public int updateByExampleSelective(T record, Object example);
使用方法
tk.mybatis.mapper.common.BaseMapper, IdsMapper, MySqlMapper內(nèi)方法使用說明:
從接口中我們可以看到傳入的方法基本均為T record,即實(shí)體類,查詢時(shí)會(huì)根據(jù)實(shí)體類中的屬性值進(jìn)行where語句構(gòu)建,查詢條件為等號(hào),這里沒什么特殊的。
ExampleMapper內(nèi)方法使用說明
Example example = new Example(UserModel.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("id","1");
criteria.orEqualTo("userName","evan11");
Criteria是Example中的一個(gè)內(nèi)部類,在最終sql構(gòu)建時(shí)以括號(hào)呈現(xiàn),Criteria里帶了較多構(gòu)建查詢條件的方法:

ExampleMapper內(nèi)方法使用說明
所有方法均需要傳入tk.mybatis.mapper.entity.Example,
Example example = new Example(UserRole.class);//實(shí)例化 Example.Criteria criteria = example.createCriteria();
Criteria是Example中的一個(gè)內(nèi)部類,在最終sql構(gòu)建時(shí)以括號(hào)呈現(xiàn),Criteria里帶了較多構(gòu)建查詢條件的方法,如
andEqualTo(String property,Object value) orEqualTo(String property,Object value) andGreaterThan(String property, Object value) orGreaterThan(String property, Object value)
舉例說明
Example example = new Example(UserModel.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("id","1");
criteria.orEqualTo("userName","evan11");
List<UserModel> userModels = userDao.selectByExample(example);
最總輸出的sql語句是
Preparing: SELECT id,user_name,user_phone FROM user WHERE ( ( id = ? and user_name = ? ) ) Parameters: 1(String), evan11(String)
其中andCondition(String condition)方法支持手寫條件,傳入的字符串為最終的查詢條件,如:length(f_user_id)<5
以及l(fā)ikeTo()的方法是不帶百分號(hào)%的,需要自己對(duì)傳入?yún)?shù)進(jìn)行構(gòu)建(加左like或者右like等)。
其余方法自行見源碼,不再贅述。
ConditionMapper內(nèi)方法使用說明
所有方法均需要傳入tk.mybatis.mapper.entity.Condition,Condition實(shí)際上繼承自tk.mybatis.mapper.entity.Example,
源碼中有三個(gè)方法:
public Condition(Class<?> entityClass) {
super(entityClass);
}
public Condition(Class<?> entityClass, boolean exists) {
super(entityClass, exists);
}
public Condition(Class<?> entityClass, boolean exists, boolean notNull) {
super(entityClass, exists, notNull);
}
其使用方法與Example類似
Condition condition = new Condition(UserRole.class);
Criteria criteria = condition.createCriteria();
criteria.andEqualTo("id","1");
criteria.orEqualTo("userName","evan11");
List<UserModel> userModels = userDao.selectByExample(example);
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringSceurity實(shí)現(xiàn)短信驗(yàn)證碼登陸
這篇文章主要介紹了SpringSceurity實(shí)現(xiàn)短信驗(yàn)證碼登陸,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Java 8新的時(shí)間日期庫的20個(gè)使用示例
這篇文章主要介紹了Java 8新的時(shí)間日期庫的20個(gè)使用示例,需要的朋友可以參考下2015-04-04
Java之不通過構(gòu)造函數(shù)創(chuàng)建一個(gè)對(duì)象問題
這篇文章主要介紹了Java之不通過構(gòu)造函數(shù)創(chuàng)建一個(gè)對(duì)象問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Mybatis-plus與Mybatis依賴沖突問題解決方法
,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧這篇文章主要介紹了Mybatis-plus與Mybatis依賴沖突問題解決方法2021-04-04
基于Spring實(shí)現(xiàn)搜索目錄下指定名稱文件
這篇文章主要為大家詳細(xì)介紹了如何基于Spring實(shí)現(xiàn)搜索目錄下指定名稱文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-08-08
Java 基于tcp協(xié)議實(shí)現(xiàn)文件上傳
這篇文章主要介紹了Java 基于tcp協(xié)議實(shí)現(xiàn)文件上傳,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-11-11
Java中關(guān)于控制臺(tái)讀取數(shù)字或字符串的方法
下面小編就為大家?guī)硪黄狫ava中關(guān)于控制臺(tái)讀取數(shù)字或字符串的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
JVM用寄存器實(shí)現(xiàn)程序計(jì)數(shù)
這篇文章主要介紹了JVM用寄存器實(shí)現(xiàn)程序計(jì)數(shù),JVM中的程序計(jì)數(shù)寄存器(Program Counter Register),Register的命名源于CPU的寄存器,寄存器存儲(chǔ)指令相關(guān)的現(xiàn)場(chǎng)信息。CPU只有把數(shù)據(jù)裝載到寄存器才能夠運(yùn)行2023-02-02
java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程
后端向前端推送消息就需要長連接,首先想到的就是websocket,下面這篇文章主要給大家介紹了關(guān)于java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程,需要的朋友可以參考下2022-10-10

