SpringBoot整合tkMapper
项目使用SpringBoot2.0,H2数据库,使用了 Lombok 简化代码;本人是刚入门的菜鸟,下面是本人使用SpringBoot整合tkMapper的一个小demo,记录下来本人在此处踩得坑,有需要修正的地方欢迎各路大神、大佬指导,小弟谦虚受教。
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> <!-- h2嵌入式数据--> <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文件夹,用来存放数据文件,其中schema-h2.sql为数据库 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: 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文件依赖冲突,由于本人也是刚入门菜鸟一个,也不是很清楚哪个与哪个冲突,这个有待读者验证,如果有了解的大神可以评论指导,多谢 以上就上本人整合通用Mappe时遇见的几个小坑,如有错误多多谅解,欢迎来指导
项目GitHub地址:https://github.com/TianPuJun/tkMapper.git