您好,登錄后才能下訂單哦!
本篇內容主要講解“SpringBoot怎么整合tkMapper”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“SpringBoot怎么整合tkMapper”吧!
1 搭建一個SpringBoot的項目,怎么搭建網上教程一大堆,這里就不描述了直接進入正題,首先先看一下整體的項目結構
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> <!-- h3嵌入式數據--> <dependency> <groupId>com.h3database</groupId> <artifactId>h3</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文件夾,用來存放數據文件,其中schema-h3.sql為數據庫 Schema 腳本,data-h3.sql為Data腳本
schema-h3.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-h3.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.h3.Driver schema: classpath:db/schema-h3.sql data: classpath:db/data-h3.sql url: jdbc:h3:mem:root username: root password: root
5 在生成的項目路徑下新建一個 tk 包,在 tk 包下新建bean包(存放實體類),dao 包(存放 Mapper 接口類),config 包(存放配置類)
tk 包下新建通用 Mapper 接口 MyMapper
繼承Mapper和MySqlMapper,以后我們的業務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包下新建業務接口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 測試代碼進行測試
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文件依賴沖突
到此,相信大家對“SpringBoot怎么整合tkMapper”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。