本篇内容主要讲解“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。