引入依赖

  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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.0.RELEASE</version>
  9. </parent>
  10. <groupId>com.example.start</groupId>
  11. <artifactId>spring-demo</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>spring-demo</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.aspectj</groupId>
  29. <artifactId>aspectjweaver</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>mysql</groupId>
  33. <artifactId>mysql-connector-java</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.projectlombok</groupId>
  37. <artifactId>lombok</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.mybatis.spring.boot</groupId>
  41. <artifactId>mybatis-spring-boot-starter</artifactId>
  42. <version>2.1.1</version>
  43. </dependency>
  44. <!--<dependency>-->
  45. <!--<groupId>com.alibaba</groupId>-->
  46. <!--<artifactId>druid</artifactId>-->
  47. <!--<version>1.1.6</version>-->
  48. <!--</dependency>-->
  49. <dependency>
  50. <groupId>com.alibaba</groupId>
  51. <artifactId>druid-spring-boot-starter</artifactId>
  52. <version>1.1.17</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-test</artifactId>
  57. </dependency>
  58. <dependency>
  59. <groupId>com.alibaba</groupId>
  60. <artifactId>dubbo</artifactId>
  61. <version>2.6.5</version>
  62. </dependency>
  63. </dependencies>
  64. <build>
  65. <plugins>
  66. <plugin>
  67. <groupId>org.springframework.boot</groupId>
  68. <artifactId>spring-boot-maven-plugin</artifactId>
  69. </plugin>
  70. <plugin>
  71. <groupId>org.mybatis.generator</groupId>
  72. <artifactId>mybatis-generator-maven-plugin</artifactId>
  73. <version>1.3.7</version>
  74. <configuration>
  75. <configurationFile>src/main/resources/mybatis/generator/generatorConfig.xml</configurationFile>
  76. </configuration>
  77. </plugin>
  78. </plugins>
  79. </build>
  80. </project>

application.properties

  1. spring.application.name=spring-demo
  2. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring_demo?useUnicode=true&characterEncoding=utf-8
  3. spring.datasource.username=root
  4. spring.datasource.password=Xiele
  5. mybatis.mapper-locations=classpath:mybatis/generator/sqlmap/*.xml
  6. mybatis.type-aliases-package=com.example.start.springdemo.mybatis

启动与验证

直接启动后执行查询, 未使用Controller。

  1. @MapperScan("com.example.start.springdemo.mybatis.generator")
  2. @SpringBootApplication
  3. public class MyBatisBootStrap {
  4. private SpringUserDao springUserDao;
  5. @Autowired
  6. public MyBatisBootStrap(SpringUserDao springUserDao) {
  7. this.springUserDao = springUserDao;
  8. }
  9. @Bean
  10. CommandLineRunner sampleCommandLineRunner() {
  11. return args -> {
  12. SpringUserDO user = springUserDao.selectByPrimaryKey(1L);
  13. System.out.println("mapper query -> " + user);
  14. };
  15. }
  16. public static void main(String[] args) {
  17. SpringApplication.run(MyBatisBootStrap.class, args);
  18. }
  19. }