SpringBoot整合JDBC
代码实现
- 导入maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>tuling</artifactId>
<groupId>com.xiyou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>chajian</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
- 因为我们在根项目中已经导入了相关的spring-boot的依赖,所以这里我们只导入自己的相关maven依赖
- 配置相关的数据源
server:
port: 8889
spring:
datasource:
username: root
data-password: root
url: jdbc:mysql://127.0.0.1:3307/test
driver-class-name: com.mysql.jdbc.Driver
- 测试
package com.xiyou.chajian.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
/**
* @author 92823
*/
@RestController
public class MyDataSourceController {
@Autowired
private DataSource dataSource;
@GetMapping("/test")
public String test () {
System.out.println(dataSource.getClass());
return "ok";
}
}
- 这里我们发现注入的dataSource已经是注入了相关的配置。
SpringBoot自动整个JDBC的原理:
- 我们在第三篇的时候分析了SpringBoot自动注入的原理,这里大概分析下
上面的类是我们数据源的自动装配:即DataSourceAutoConfiguration的配置类,我们圈出来的就是我们自动集成进去的数据源 - 数据源的自动装配:
(1)Hikari数据源
/**
* Hikari DataSource configuration.
*/
@ConditionalOnClass(HikariDataSource.class)
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource", matchIfMissing = true)
static class Hikari {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public HikariDataSource dataSource(DataSourceProperties properties) {
HikariDataSource dataSource = createDataSource(properties,
HikariDataSource.class);
if (StringUtils.hasText(properties.getName())) {
dataSource.setPoolName(properties.getName());
}
return dataSource;
}
}
(2)Tomcat连接池的数据源
/**
* Tomcat Pool DataSource configuration.
*/
@ConditionalOnClass(org.apache.tomcat.jdbc.pool.DataSource.class)
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "org.apache.tomcat.jdbc.pool.DataSource", matchIfMissing = true)
static class Tomcat {
//配置tomcat 连接池的数据源
@Bean
@ConfigurationProperties(prefix = "spring.datasource.tomcat")
public org.apache.tomcat.jdbc.pool.DataSource dataSource(
DataSourceProperties properties) {
org.apache.tomcat.jdbc.pool.DataSource dataSource = createDataSource(
properties, org.apache.tomcat.jdbc.pool.DataSource.class);
DatabaseDriver databaseDriver = DatabaseDriver
.fromJdbcUrl(properties.determineUrl());
String validationQuery = databaseDriver.getValidationQuery();
if (validationQuery != null) {
dataSource.setTestOnBorrow(true);
dataSource.setValidationQuery(validationQuery);
}
return dataSource;
}
}
(3)Dbcp2连接池的数据源
/**
* DBCP DataSource configuration.
*/
@ConditionalOnClass(org.apache.commons.dbcp2.BasicDataSource.class)
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "org.apache.commons.dbcp2.BasicDataSource", matchIfMissing = true)
static class Dbcp2 {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.dbcp2")
public org.apache.commons.dbcp2.BasicDataSource dataSource(
DataSourceProperties properties) {
return createDataSource(properties,
org.apache.commons.dbcp2.BasicDataSource.class);
}
}
(4)我们可以通过spring.datasource.type来指定具体的数据源
//通过spring.dataSource.tpye 来指定装配的数据源
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type")
static class Generic {
@Bean
public DataSource dataSource(DataSourceProperties properties) {
return properties.initializeDataSourceBuilder().build();
}
}
- 我们这时候可以再来看下jdbcTemplate的配置
@Configuration
@ConditionalOnClass({ DataSource.class, JdbcTemplate.class })
@ConditionalOnSingleCandidate(DataSource.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcTemplateAutoConfiguration {
@Configuration
static class JdbcTemplateConfiguration {
private final DataSource dataSource;
private final JdbcProperties properties;
JdbcTemplateConfiguration(DataSource dataSource, JdbcProperties properties) {
this.dataSource = dataSource;
this.properties = properties;
}
//jdbcTemplate 配置
@Bean
@Primary
@ConditionalOnMissingBean(JdbcOperations.class)
public JdbcTemplate jdbcTemplate() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
JdbcProperties.Template template = this.properties.getTemplate();
jdbcTemplate.setFetchSize(template.getFetchSize());
jdbcTemplate.setMaxRows(template.getMaxRows());
if (template.getQueryTimeout() != null) {
jdbcTemplate
.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
}
return jdbcTemplate;
}
}
@Configuration
@Import(JdbcTemplateConfiguration.class)
static class NamedParameterJdbcTemplateConfiguration {
@Bean
@Primary
@ConditionalOnSingleCandidate(JdbcTemplate.class)
@ConditionalOnMissingBean(NamedParameterJdbcOperations.class)
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(
JdbcTemplate jdbcTemplate) {
return new NamedParameterJdbcTemplate(jdbcTemplate);
}
}
}
这就是为什么我们可以直接注入JdbcTemplate了。
SpringBoot整合Druid以及Druid的监控
(1)加入druid的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--//自定义属性绑定配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- 其中spring-boot-configuration-processor的作用是我们默认使用的是yml文件,但是有时候要用xml或者properties文件,就需要这个依赖
- 注意如果要用properties文件的属性,注入到类的属性中,需要在配置类的开头加上@PropertySource(“classpath:your.properties”),其余用法与加载yml的配置一样
(2)这里我们需要在yml文件中进行配置,我们先直接对其进行配置
- 仔细观察我们发现这样写属性无法映射,因为配置的名字有很多在PO类中是不存在的,PO类如下:
- 我们可以看下注入的结果:
很明显没有映射进去。我们应该怎么映射呢?
(3)配置我们自己的druid的数据源属性,自己映射
server:
port: 8889
spring:
datasource:
druid:
username: root
password: root
jdbcUrl: jdbc:mysql://127.0.0.1:3307/test
driverClassName: com.mysql.jdbc.Driver
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,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
- 新增自己将要映射的PO类:
package com.xiyou.chajian.po;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author 92823
*/
@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class DruidDataSourceProperties {
private String username;
private String password;
private String jdbcUrl;
private String driverClassName;
private Integer initialSize;
private Integer maxActive;
private Integer minIdle;
private long maxWait;
private boolean poolPreparedStatements;
}
这里我们利用@ConfigurationProperties(prefix = “spring.datasource.druid”)自动将yml中配置的属性注入到了该类中。
(4)定制config配置类,这里我们注入了数据源,也配置了一个Servlet负责跳转到druid的监控页面,一个过滤器负责拦截请求,去除静态资源等不过滤的字段
package com.xiyou.chajian.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.xiyou.chajian.po.DruidDataSourceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author 92823
*/
@Configuration
// 自动注入到该类中, 该注解负责开启@ConfigurationProperties
@EnableConfigurationProperties(value = DruidDataSourceProperties.class)
public class DruidDataSourceConfig {
@Autowired
private DruidDataSourceProperties druidDataSourceProperties;
/**
* 注入数据源的相关信息
* @return
*/
@Bean
public DataSource dataSource() {
System.out.println("这时候已经注入了: " + druidDataSourceProperties);
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUsername(druidDataSourceProperties.getUsername());
druidDataSource.setPassword(druidDataSourceProperties.getPassword());
druidDataSource.setUrl(druidDataSourceProperties.getJdbcUrl());
druidDataSource.setDriverClassName(druidDataSourceProperties.getDriverClassName());
druidDataSource.setInitialSize(druidDataSourceProperties.getInitialSize());
druidDataSource.setMinIdle(druidDataSourceProperties.getMinIdle());
druidDataSource.setMaxActive(druidDataSourceProperties.getMaxActive());
druidDataSource.setMaxWait(druidDataSourceProperties.getMaxWait());
druidDataSourceProperties.setPoolPreparedStatements(druidDataSourceProperties.isPoolPreparedStatements());
return druidDataSource;
}
/**
* 配置数据源的监控
* 配置了一个Servlet
*/
@Bean
public ServletRegistrationBean statViewServlet() {
// 该Servlet是人家提供的。
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
Map<String, Object> initParameters = new HashMap<>();
initParameters.put("loginUsername","admin");
initParameters.put("loginPassword","123456");
// 设置用户名和密码
bean.setInitParameters(initParameters);
return bean;
}
/**
* 配置一个过滤器
*/
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
// 设置过滤的路径
filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
// 去除不用过滤的路径
Map<String, Object> initParams = new HashMap<>();
initParams.put("exclusions", "*.js,*.css,/druid/*");
filterRegistrationBean.setInitParameters(initParams);
return filterRegistrationBean;
}
}
- 注意,这里的注解@EnableConfigurationProperties负责开启注解@ConfigurationProperties
(5)观察结果: - 可以看到数据源已经注入
这时候已经注入了: DruidDataSourceProperties(username=root, password=root, jdbcUrl=jdbc:mysql://127.0.0.1:3307/test, driverClassName=com.mysql.jdbc.Driver, initialSize=5, maxActive=20, minIdle=5, maxWait=60000, poolPreparedStatements=true)
- 访问http://localhost:8889/druid/,可以看到对应的监控页面
其中用户名密码就是我们在conf中配置的
Map<String, Object> initParameters = new HashMap<>();
initParameters.put("loginUsername","admin");
initParameters.put("loginPassword","123456");
// 设置用户名和密码
bean.setInitParameters(initParameters);
SpringBoot整合myBatis
代码实现
(1)pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>tuling</artifactId>
<groupId>com.xiyou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.46</version>
</dependency>
</dependencies>
</project>
(2)数据库结构
CREATE TABLE `t_users` (
`user_name` varchar(20) DEFAULT NULL,
`age` int(100) DEFAULT NULL,
`id` int(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
(3)yml文件
server:
port: 8080
spring:
datasource:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/test
driver-class-name: com.mysql.jdbc.Driver
# 配置mybatis
mybatis:
configuration:
# 开启驼峰命名
map-underscore-to-camel-case: true
# 指定配置文件的位置
mapper-locations: classpath:/mappers/*.xml
- 注意:这里我们开启了驼峰命名,就是说我们可以将下划线和驼峰相互转换
- 另外我们开启了xml文件的位置指定,这样我们就可以用xml的形式写sql了
(4)Mapper文件
package com.xiyou.mapepr;
import com.xiyou.po.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @author
*/
@Mapper
public interface UserMapper {
@Select("select * from t_users")
List<User> selectUsers();
public User selectUserById(Integer id);
}
- 我们可以看到这里我们一个用了注解的模式,一个映射到了xml文件中
- 这里我们是在类上面写了@Mapper而没有在配置类上写@MapperScan
(5)user.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xiyou.mapepr.UserMapper">
<select id="selectUserById" parameterType="Integer" resultType="com.xiyou.po.User">
select * from t_users where id = #{id}
</select>
</mapper>
- 该文件是在resources/mappers/user.xml
(6)启动类
package com.xiyou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author
*/
@SpringBootApplication
public class MyBatisApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatisApplication.class, args);
}
}
(7)User
package com.xiyou.po;
import lombok.Data;
/**
* @author
* 实体类对应数据库的t_users信息
*/
@Data
public class User {
private Integer id;
private Integer age;
private String userName;
}
(8)Controller类,用来测试
package com.xiyou.controller;
import com.alibaba.fastjson.JSONObject;
import com.xiyou.mapepr.UserMapper;
import com.xiyou.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author
*/
@RestController
public class TestController {
@Autowired
private UserMapper userMapper;
@GetMapping("/test")
public String test() {
List<User> users = userMapper.selectUsers();
return JSONObject.toJSONString(users);
}
@GetMapping("/selectUserById/{id}")
public String selectUserById(@PathVariable Integer id) {
User user = userMapper.selectUserById(id);
return JSONObject.toJSONString(user);
}
}
源码解析
- 我们还是一样自动注解么,明显看的类是MybatisAutoConfiguration类
- 该类为我们导入了:
(1)SqlSessionFactory
(2)SqlSessionTemplate
(3)mapperScanner
@org.springframework.context.annotation.Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisAutoConfiguration implements InitializingBean {
1:自动装配了 sqlSessionFactory
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
if (StringUtils.hasText(this.properties.getConfigLocation())) {
factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
}
applyConfiguration(factory);
if (this.properties.getConfigurationProperties() != null) {
factory.setConfigurationProperties(this.properties.getConfigurationProperties());
}
if (!ObjectUtils.isEmpty(this.interceptors)) {
factory.setPlugins(this.interceptors);
}
if (this.databaseIdProvider != null) {
factory.setDatabaseIdProvider(this.databaseIdProvider);
}
if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
}
if (this.properties.getTypeAliasesSuperType() != null) {
factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
}
if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
}
if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
factory.setMapperLocations(this.properties.resolveMapperLocations());
}
return factory.getObject();
}
2:配置了sqlSessionTemplate
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
ExecutorType executorType = this.properties.getExecutorType();
if (executorType != null) {
return new SqlSessionTemplate(sqlSessionFactory, executorType);
} else {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
3:自动装配 导入mapperScanner
public static class AutoConfiguredMapperScannerRegistrar
implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware {
private BeanFactory beanFactory;
private ResourceLoader resourceLoader;
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if (!AutoConfigurationPackages.has(this.beanFactory)) {
logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.");
return;
}
logger.debug("Searching for mappers annotated with @Mapper");
List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
if (logger.isDebugEnabled()) {
packages.forEach(pkg -> logger.debug("Using auto-configuration base package '{}'", pkg));
}
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
if (this.resourceLoader != null) {
scanner.setResourceLoader(this.resourceLoader);
}
scanner.setAnnotationClass(Mapper.class);
scanner.registerFilters();
scanner.doScan(StringUtils.toStringArray(packages));
}
}
}
SpringBoot整合Redis
- 这里给大家一个传送门好了,因为我在分析这个集成redis的时候顺便分析了下,SpringBoot自动装配的原理:SpringBoot自动注入的原理讲解,以及redis的集成