1 Untuk membina projek SpringBoot, terdapat banyak tutorial dalam talian tentang cara membinanya, saya tidak akan menerangkannya di sini dan terus ke intinya , mari kita lihat keseluruhan struktur projek
2 fail pom memperkenalkan kebergantungan (nota konflik kebergantungan)
<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 Buat folder db baharu dalam direktori sumber , digunakan untuk menyimpan fail data, dengan schema-h3.sql ialah skrip Skema pangkalan data, data-h3.sql ialah skrip 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 Buat yang baharu di bawah pakej tk laluan projek yang dijana, buat pakej kacang baharu (untuk menyimpan kelas entiti), pakej dao (untuk menyimpan kelas antara muka Mapper) dan pakej konfigurasi (untuk menyimpan kelas konfigurasi) di bawah pakej tk
Buat Mapper umum baharu di bawah pakej tk Antara muka MyMapper
mewarisi Mapper dan MySqlMapper Dalam masa hadapan, dao perniagaan kami boleh terus menyepadukan MyMapper
Antara muka ini tidak boleh diimbas, jika tidak, ia akan melaporkan Ralat
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> { }
Buat kelas Pengguna baharu di bawah pakej kacang Lombok digunakan untuk meninggalkan kod di sini, dan anotasi @Data mengandungi get/ set dan kaedah lain
. 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; }
Buat antara muka perniagaan baharu UserDao di bawah pakej dao
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> { }
Kelas konfigurasi baharu 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 Tambah @MapperScan dalam kelas permulaan
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 Uji kod untuk ujian
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); } }
Akhirnya lihat output seperti yang ditunjukkan di bawah dan ia berjaya
8 Perangkap yang dihadapi
rreeeAtas ialah kandungan terperinci Bagaimana untuk mengintegrasikan tkMapper dengan SpringBoot. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!