SpringBoot整合tkMapper的方法
SpringBoot整合tkMapper
項目使用SpringBoot2.0,H2數(shù)據(jù)庫,使用了 Lombok 簡化代碼;本人是剛?cè)腴T的菜鳥,下面是本人使用SpringBoot整合tkMapper的一個小demo,記錄下來本人在此處踩得坑,有需要修正的地方歡迎各路大神、大佬指導(dǎo),小弟謙虛受教。
1 搭建一個SpringBoot的項目,怎么搭建網(wǎng)上教程一大堆,這里就不描述了直接進(jìn)入正題,首先先看一下整體的項目結(jié)構(gòu)

2 pom文件引入依賴(注意依賴沖突)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok省略代碼-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- h2嵌入式數(shù)據(jù)-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- tk-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>3 在resources目錄下新建db文件夾,用來存放數(shù)據(jù)文件,其中schema-h2.sql為數(shù)據(jù)庫 Schema 腳本,data-h2.sql為Data腳本
schema-h2.sql
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主鍵ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年齡',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '郵箱',
PRIMARY KEY (id)
);data-h2.sql
DELETE FROM user; INSERT INTO user (id, name, age, email) VALUES (1, 'red', 18, 'test1@tian.com'), (2, 'yll', 20, 'test2@tian.com'), (3, 'putty', 22, 'test3@tian.com'), (4, 'ele', 24, 'test4@tian.com'), (5, 'tom', 26, 'test5@tian.com');
4 application.yml
spring:
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
url: jdbc:h2:mem:root
username: root
password: root5 在生成的項目路徑下新建一個 tk 包,在 tk 包下新建bean包(存放實體類),dao 包(存放 Mapper 接口類),config 包(存放配置類)
tk 包下新建通用 Mapper 接口 MyMapper
繼承Mapper和MySqlMapper,以后我們的業(yè)務(wù)dao直接集成MyMapper即可
此接口不能被掃描到,否則會報錯
MyMapper.java
package com.tian.tkmapper;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
@Repository
public interface MyMapper<T> extends Mapper<T> ,MySqlMapper<T> {
}bean包下新建User類,此處使用了Lombok省略代碼,@Data注解包含get/set等方法
User.java
package com.tian.tkmapper.tk.bean;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}dao包下新建業(yè)務(wù)接口UserDao
UserDao.java
package com.tian.tkmapper.tk.dao;
import com.tian.tkmapper.MyMapper;
import com.tian.tkmapper.tk.bean.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDao extends MyMapper<User> {
}config包下新建配置類MybatisConfigurer
MybatisConfigurer.java
package com.tian.tkmapper.tk.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
public class MybatisConfigurer {
@Resource
private DataSource dataSource;
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("com.tian.tkmapper.tk.bean");
//添加XML目錄
// ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
return bean.getObject();
}
@Configuration
@AutoConfigureAfter(MybatisConfigurer.class)
public static class MyBatisMapperScannerConfigurer {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
mapperScannerConfigurer.setBasePackage("com.tian.tkmapper.tk.dao.*");
//配置通用mappers
Properties properties = new Properties();
properties.setProperty("mappers", "com.tian.tkmapper.MyMapper");
properties.setProperty("notEmpty", "false");
properties.setProperty("IDENTITY", "MYSQL");
mapperScannerConfigurer.setProperties(properties);
return mapperScannerConfigurer;
}
}
}6 在啟動類添加@MapperScan
package com.tian.tkmapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages = "com.tian.tkmapper.tk.dao")//mapper接口的路徑
public class TkmapperApplication {
public static void main(String[] args) {
SpringApplication.run(TkmapperApplication.class, args);
}
}7 測試代碼進(jìn)行測試
package com.tian.tkmapper;
import com.tian.tkmapper.tk.bean.User;
import com.tian.tkmapper.tk.dao.UserDao;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TkmapperApplicationTests {
@Autowired
private UserDao userDao;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userDao.selectAll();
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
}最后看到顯示輸出為如下即成功

8 說一下本人遇到的小坑,劃重點了劃重點了劃重點了,重要的事情說三遍?。?!
a> MyMapper不能被掃描到,解決方案很多,不讓啟動類啟動時掃描到就可以
b> 啟動類中@MapperScan要引tk包下面的 import tk.mybatis.spring.annotation.MapperScan;
c>pom文件依賴沖突,由于本人也是剛?cè)腴T菜鳥一個,也不是很清楚哪個與哪個沖突,這個有待讀者驗證,如果有了解的大神可以評論指導(dǎo),多謝
以上就上本人整合通用Mappe時遇見的幾個小坑,如有錯誤多多諒解,歡迎來指導(dǎo)到此這篇關(guān)于SpringBoot整合tkMapper的方法的文章就介紹到這了,更多相關(guān)SpringBoot整合tkMapper內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot實現(xiàn)web.xml功能示例詳解
這篇文章主要介紹了Spring?Boot實現(xiàn)web.xml功能,通過本文介紹我們了解到,在Spring Boot應(yīng)用中,我們可以通過注解和編程兩種方式實現(xiàn)web.xml的功能,包括如何創(chuàng)建及注冊Servlet、Filter以及Listener等,需要的朋友可以參考下2023-09-09
Java中Cron表達(dá)式的生成解析及計算的工具類完整代碼
這篇文章主要給大家介紹了關(guān)于Java中Cron表達(dá)式的生成解析及計算工具類的相關(guān)資料,Cron表達(dá)式是一個字符串,字符串空格分割,每一個域代表一個含義,一個cron表達(dá)式有至少6個,需要的朋友可以參考下2023-12-12
Java中String的intern()方法詳細(xì)說明
這篇文章主要介紹了Java中String的intern()方法詳細(xì)說明,String::intern()是一個本地方法,他的作用就是如果字符串常量池中已經(jīng)包含了一個等于此String對象的字符串,則返回代表池中的這個字符串額String對象的引用,需要的朋友可以參考下2023-11-11
Springboot?Vue實現(xiàn)單點登陸功能示例詳解
這篇文章主要為大家介紹了Springboot?Vue實現(xiàn)單點登陸功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
解決Springboot get請求是參數(shù)過長的情況
這篇文章主要介紹了解決Springboot get請求是參數(shù)過長的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
mybatis generator只能生成insert和selectAll的操作
這篇文章主要介紹了mybatis generator只能生成insert和selectAll的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

