SpringBoot 整合 Mybatis:提升你的Java项目开发效率
第一章:数据自动管理
-
引入 JDBC 的依赖和 SpringBoot 的应用场景:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
-
创建 application.yaml 进行配置:
spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/boot_demo driver-class-name: com.mysql.jdbc.Driver type: com.zaxxer.hikari.HikariDataSource
- 在默认情况下,数据链接可以使用 DataSource 池进行自动配置
- Hikari 可用,SpringBoot 将使用它
- Commons DBCP2 可用,我们将使用它
- 也可以指定数据源配置,通过 type 来选取使用哪种数据源
spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/boot_demo driver-class-name: com.mysql.jdbc.Driver type: com.zaxxer.hikari.HikariDataSource # type: org.apache.commons.dbcp2.BasicDataSource
-
设置 druid 数据源:
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> </dependency>
- 修改 application.yaml 文件中:
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
- 在 application.yaml 文件中加入:
spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/boot_demo driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
- 创建数据源注册类:
@Configuration public class DruidConfig { @ConstructorProperties(prefix="spring.datasource") @Bean public DataSource dataSource(){ return new DruidDataSource; } }
- 配置 druid 运行期监控
@Configuration public class DruidConfig { @ConstructorProperties(prefix="spring.datasource") @Bean public DataSource dataSource(){ return new DruidDataSource; } @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); Map<String,String> initParams = new HashMap<>(); initParams.put("loginUsername","root"); initParams.put("loginPassword","root"); initParams.put("allow","");//默认就是允许所有访问 initParams.put("deny","192.168.15.21"); bean.setInitParameters(initParams); return bean; } //2、配置一个web监控的filter @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean bean; bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); Map<String,String> initParams = new HashMap<>(); initParams.put("exclusions","*.js,*.css,/druid/*"); bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }
- 打开监控页面:http://localhost:8080/druid
- 修改 application.yaml 文件中:
第二章:SpringBoot 整合 JDBCTemplate
-
创建表:
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for tx_user -- ---------------------------- DROP TABLE IF EXISTS `tx_user`; CREATE TABLE `tx_user` ( `username` varchar(10) DEFAULT NULL, `userId` int(10) NOT NULL, `password` varchar(10) DEFAULT NULL, PRIMARY KEY (`userId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
创建 Controller:
public class TestController { @Autowired JdbcTemplate jdbcTemplate; @ResponseBody @RequestMapping("/query") public List<Map<String, Object>> query(){ List<Map<String, Object>> maps = jdbcTemplate.queryForList("SELECT * FROM tx_user"); return maps; }
- 启动 SpringBoot 访问:http://localhost:8080/query
- SpringBoot 中提供了 jdbcTemplateAutoConfiguration 的自动配置
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
- jdbcTemplateAutoConfiguration 源码: