修改入口类

需要使用 @MapperScan 注解来指定 Mapper 接口的路径
PS: 注意这里的 @MapperScan 注解是 tk.mybatis.spring.annotation.MapperScan; 包下的

  1. package com.funtl.hello.spring.boot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import tk.mybatis.spring.annotation.MapperScan;
  5. @SpringBootApplication
  6. @MapperScan(basePackages = "com.funtl.hello.spring.boot.mapper")
  7. public class HelloSpringBootApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(HelloSpringBootApplication.class, args);
  10. }
  11. }

创建测试类

如果想测试的数据不写入数据库里就进行回滚操作,加上下面两个注解
@Transactional
@Rollback

  1. package com.funtl.hello.spring.boot;
  2. import com.funtl.hello.spring.boot.entity.TbUser;
  3. import com.funtl.hello.spring.boot.mapper.TbUserMapper;
  4. import com.github.pagehelper.PageHelper;
  5. import com.github.pagehelper.PageInfo;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import org.springframework.test.annotation.Rollback;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import tk.mybatis.mapper.entity.Example;
  14. import java.util.Date;
  15. import java.util.List;
  16. @RunWith(SpringRunner.class)
  17. @SpringBootTest(classes = HelloSpringBootApplication.class)
  18. @Transactional
  19. @Rollback
  20. public class MyBatisTests {
  21. /**
  22. * 注入数据查询接口
  23. */
  24. @Autowired
  25. private TbUserMapper tbUserMapper;
  26. /**
  27. * 测试插入数据
  28. */
  29. @Test
  30. public void testInsert() {
  31. // 构造一条测试数据
  32. TbUser tbUser = new TbUser();
  33. tbUser.setUsername("Lusifer");
  34. tbUser.setPassword("123456");
  35. tbUser.setPhone("15888888888");
  36. tbUser.setEmail("topsale@vip.qq.com");
  37. tbUser.setCreated(new Date());
  38. tbUser.setUpdated(new Date());
  39. // 插入数据
  40. tbUserMapper.insert(tbUser);
  41. }
  42. /**
  43. * 测试删除数据
  44. */
  45. @Test
  46. public void testDelete() {
  47. // 构造条件,等同于 DELETE from tb_user WHERE username = 'Lusifer'
  48. Example example = new Example(TbUser.class);
  49. example.createCriteria().andEqualTo("username", "Lusifer");
  50. // 删除数据
  51. tbUserMapper.deleteByExample(example);
  52. }
  53. /**
  54. * 测试修改数据
  55. */
  56. @Test
  57. public void testUpdate() {
  58. // 构造条件
  59. Example example = new Example(TbUser.class);
  60. example.createCriteria().andEqualTo("username", "Lusifer");
  61. // 构造一条测试数据
  62. TbUser tbUser = new TbUser();
  63. tbUser.setUsername("LusiferNew");
  64. tbUser.setPassword("123456");
  65. tbUser.setPhone("15888888888");
  66. tbUser.setEmail("topsale@vip.qq.com");
  67. tbUser.setCreated(new Date());
  68. tbUser.setUpdated(new Date());
  69. // 修改数据
  70. tbUserMapper.updateByExample(tbUser, example);
  71. }
  72. /**
  73. * 测试查询集合
  74. */
  75. @Test
  76. public void testSelect() {
  77. List<TbUser> tbUsers = tbUserMapper.selectAll();
  78. for (TbUser tbUser : tbUsers) {
  79. System.out.println(tbUser.getUsername());
  80. }
  81. }
  82. /**
  83. * 测试分页查询
  84. */
  85. @Test
  86. public void testPage() {
  87. // PageHelper 使用非常简单,只需要设置页码和每页显示笔数即可
  88. PageHelper.startPage(0, 2);
  89. // 设置分页查询条件
  90. Example example = new Example(TbUser.class);
  91. PageInfo<TbUser> pageInfo = new PageInfo<>(tbUserMapper.selectByExample(example));
  92. // 获取查询结果
  93. List<TbUser> tbUsers = pageInfo.getList();
  94. for (TbUser tbUser : tbUsers) {
  95. System.out.println(tbUser.getUsername());
  96. }
  97. }
  98. }

从输出的日志可以看出数据库连接池是 MyHikariCP
image.png

附:完整的 POM

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.funtl</groupId>
  6. <artifactId>hello-spring-boot</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>hello-spring-boot</name>
  10. <description></description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.2.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-tomcat</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-actuator</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-test</artifactId>
  42. <scope>test</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>com.alibaba</groupId>
  46. <artifactId>druid-spring-boot-starter</artifactId>
  47. <version>1.1.10</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>tk.mybatis</groupId>
  51. <artifactId>mapper-spring-boot-starter</artifactId>
  52. <version>2.0.2</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>com.github.pagehelper</groupId>
  56. <artifactId>pagehelper-spring-boot-starter</artifactId>
  57. <version>1.2.5</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>net.sourceforge.nekohtml</groupId>
  61. <artifactId>nekohtml</artifactId>
  62. <version>1.9.22</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>mysql</groupId>
  66. <artifactId>mysql-connector-java</artifactId>
  67. <scope>runtime</scope>
  68. </dependency>
  69. </dependencies>
  70. <build>
  71. <plugins>
  72. <plugin>
  73. <groupId>org.springframework.boot</groupId>
  74. <artifactId>spring-boot-maven-plugin</artifactId>
  75. <configuration>
  76. <mainClass>com.funtl.hello.spring.boot.HelloSpringBootApplication</mainClass>
  77. </configuration>
  78. </plugin>
  79. <plugin>
  80. <groupId>org.mybatis.generator</groupId>
  81. <artifactId>mybatis-generator-maven-plugin</artifactId>
  82. <version>1.3.5</version>
  83. <configuration>
  84. <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
  85. <overwrite>true</overwrite>
  86. <verbose>true</verbose>
  87. </configuration>
  88. <dependencies>
  89. <dependency>
  90. <groupId>mysql</groupId>
  91. <artifactId>mysql-connector-java</artifactId>
  92. <version>${mysql.version}</version>
  93. </dependency>
  94. <dependency>
  95. <groupId>tk.mybatis</groupId>
  96. <artifactId>mapper</artifactId>
  97. <version>3.4.4</version>
  98. </dependency>
  99. </dependencies>
  100. </plugin>
  101. </plugins>
  102. </build>
  103. </project>