You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # spring-boot-demo-mybatis
  2. 依赖 [spring-boot-demo-helloworld](../spring-boot-demo-parent)
  3. 集成了通用Mapper、分页插件PageHelper以及阿里的数据源Druid
  4. ### pom.xml
  5. ```xml
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  9. <modelVersion>4.0.0</modelVersion>
  10. <artifactId>spring-boot-demo-mybatis</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. <packaging>war</packaging>
  13. <name>spring-boot-demo-mybatis</name>
  14. <description>Demo project for Spring Boot</description>
  15. <parent>
  16. <groupId>com.xkcoding</groupId>
  17. <artifactId>spring-boot-demo-parent</artifactId>
  18. <version>0.0.1-SNAPSHOT</version>
  19. <relativePath>../spring-boot-demo-parent/pom.xml</relativePath>
  20. </parent>
  21. <properties>
  22. <mybatis.starter.version>1.3.1</mybatis.starter.version>
  23. <druid.starter.version>1.1.5</druid.starter.version>
  24. <mapper.version>1.1.1</mapper.version>
  25. <pagehelper.version>1.1.0</pagehelper.version>
  26. </properties>
  27. <dependencies>
  28. <!--mybatis-->
  29. <dependency>
  30. <groupId>org.mybatis.spring.boot</groupId>
  31. <artifactId>mybatis-spring-boot-starter</artifactId>
  32. <version>${mybatis.starter.version}</version>
  33. </dependency>
  34. <!--druid-->
  35. <dependency>
  36. <groupId>com.alibaba</groupId>
  37. <artifactId>druid-spring-boot-starter</artifactId>
  38. <version>${druid.starter.version}</version>
  39. </dependency>
  40. <!--通用 Mapper-->
  41. <dependency>
  42. <groupId>tk.mybatis</groupId>
  43. <artifactId>mapper-spring-boot-starter</artifactId>
  44. <version>${mapper.version}</version>
  45. </dependency>
  46. <!--PageHelper-->
  47. <dependency>
  48. <groupId>com.github.pagehelper</groupId>
  49. <artifactId>pagehelper-spring-boot-starter</artifactId>
  50. <version>${pagehelper.version}</version>
  51. </dependency>
  52. </dependencies>
  53. <build>
  54. <finalName>spring-boot-demo-mybatis</finalName>
  55. </build>
  56. </project>
  57. ```
  58. ### application.yml
  59. ```yml
  60. server:
  61. port: 8080
  62. context-path: /demo
  63. spring:
  64. # json 转化移除 null 字段
  65. # jackson:
  66. # default-property-inclusion: non_null
  67. datasource:
  68. # 启动时自动运行的 SQL 文件
  69. schema: classpath:init-sql/schema.sql
  70. continue-on-error: true
  71. druid:
  72. url: jdbc:mysql://localhost:3306/spring-boot-demo?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
  73. username: root
  74. password: root
  75. driver-class-name: com.mysql.jdbc.Driver
  76. # 连接池配置
  77. # 初始化大小,最小,最大
  78. initialSize: 5
  79. minIdle: 5
  80. maxActive: 20
  81. # 配置获取连接等待超时的时间
  82. maxWait: 60000
  83. # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  84. timeBetweenEvictionRunsMillis: 60000
  85. # 配置一个连接在池中最小生存的时间,单位是毫秒
  86. minEvictableIdleTimeMillis: 300000
  87. validationQuery: SELECT 1 FROM DUAL
  88. testWhileIdle: true
  89. testOnBorrow: false
  90. testOnReturn: false
  91. # 打开PSCache,并且指定每个连接上PSCache的大小
  92. poolPreparedStatements: true
  93. maxPoolPreparedStatementPerConnectionSize: 20
  94. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  95. filters: stat,wall,log4j
  96. # 监控配置
  97. web-stat-filter:
  98. enabled: true
  99. url-pattern: /*
  100. exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
  101. stat-view-servlet:
  102. enabled: true
  103. url-pattern: /sys/druid/*
  104. reset-enable: fasle
  105. login-username: xkcoding
  106. login-password: 123456
  107. filter:
  108. stat:
  109. log-slow-sql: true
  110. slow-sql-millis: 5000
  111. merge-sql: true
  112. # mybatis 配置
  113. mybatis:
  114. type-aliases-package: com.xkcoding.springbootdemomybatis.model
  115. mapper-locations: classpath:mapper/*.xml
  116. # 配置项:开启下划线到驼峰的自动转换. 作用:将数据库字段根据驼峰规则自动注入到对象属性
  117. configuration:
  118. map-underscore-to-camel-case: true
  119. # 通用 Mapper 配置
  120. mapper:
  121. not-empty: false
  122. identity: MYSQL
  123. mappers: com.xkcoding.springbootdemomybatis.util.MyMapper
  124. # PageHelper 配置
  125. pagehelper:
  126. helper-dialect: mysql
  127. reasonable: true
  128. support-methods-arguments: true
  129. params: count=countSql
  130. ```
  131. ### SpringBootDemoMybatisApplication.java
  132. ```java
  133. @SpringBootApplication
  134. @MapperScan(basePackages = {"com.xkcoding.springbootdemomybatis.mapper"})
  135. public class SpringBootDemoMybatisApplication {
  136. public static void main(String[] args) {
  137. SpringApplication.run(SpringBootDemoMybatisApplication.class, args);
  138. }
  139. }
  140. ```
  141. ### schema.sql
  142. ```sql
  143. SET FOREIGN_KEY_CHECKS=0;
  144. -- ----------------------------
  145. -- Table structure for `boot_user`
  146. -- ----------------------------
  147. DROP TABLE IF EXISTS `mybatis_user`;
  148. CREATE TABLE `mybatis_user` (
  149. `id` int(11) NOT NULL AUTO_INCREMENT,
  150. `name` varchar(32) DEFAULT NULL,
  151. `tel` varchar(11) DEFAULT NULL,
  152. `create_time` datetime DEFAULT NULL,
  153. PRIMARY KEY (`id`)
  154. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  155. -- ----------------------------
  156. -- Records of boot_user
  157. -- ----------------------------
  158. INSERT INTO `mybatis_user` VALUES (1, 'klay', '13799008800', '2017-11-13 16:04:39');
  159. INSERT INTO `mybatis_user` VALUES (2, 'Tome', '18988991234', '2017-11-13 16:13:28');
  160. ```
  161. ### 其余代码
  162. 详情请参见本demo。

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。

Contributors (1)