2. 设置Druid数据源com.alibaba druid${druid.version} org.mybatis.spring.boot mybatis-spring-boot-starter${mybatis-spring-boot.version} org.springframework.boot spring-boot-starter-jdbcmysql mysql-connector-java${mysql-connector-java.version}
添加SingleDataSourceConfig.java配置文件,整合Druid数据源
@Configuration@MapperScan({"com.hrabbit.admin.modual.system.mapper"})public class SingleDataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource") public DruidProperties druidProperties() { return new DruidProperties();
}
}其中的DruidProperties.java的配置信息如下(这里使用了Lombok,因此省略了get/set方法,不了解Lombok的童鞋请搜索下):
@Component@ConfigurationProperties(prefix = "spring.datasource")@Datapublic class DruidProperties { private String url = "jdbc:mysql://127.0.0.1:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull"; private String username = "root"; private String password = "root"; private String driverClassName = "com.mysql.jdbc.Driver"; private Integer initialSize = 2; private Integer minIdle = 1; private Integer maxActive = 20; private Integer maxWait = 60000; private Integer timeBetweenEvictionRunsMillis = 60000; private Integer minEvictableIdleTimeMillis = 300000; private String validationQuery = "SELECt 'x'"; private Boolean testWhileIdle = true; private Boolean testOnBorrow = false; private Boolean testOnReturn = false; private Boolean poolPreparedStatements = true; private Integer maxPoolPreparedStatementPerConnectionSize = 20; private String filters = "stat"; public void config(DruidDataSource dataSource) {
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
dataSource.setInitialSize(initialSize); //定义初始连接数
dataSource.setMinIdle(minIdle); //最小空闲
dataSource.setMaxActive(maxActive); //定义最大连接数
dataSource.setMaxWait(maxWait); //最长等待时间
// 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // 配置一个连接在池中最小生存的时间,单位是毫秒
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
dataSource.setValidationQuery(validationQuery);
dataSource.setTestWhileIdle(testWhileIdle);
dataSource.setTestonBorrow(testOnBorrow);
dataSource.setTestonReturn(testOnReturn); // 打开PSCache,并且指定每个连接上PSCache的大小
dataSource.setPoolPreparedStatements(poolPreparedStatements);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try {
dataSource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
}
}3.配置application.yml文件中的mapper.xml的存放路径### 启动端口号server:
port: 8080### 设置数据库spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/hrabbit_admin?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
username: root password: root### 配置Mybatismybatis:
### xml存放路径
mapper-locations: classpath:mapperpublic interface SysUserMapper {
SysUser findSysUserById(@Param("id") Long id);
}5. 在SysUserMapper.xml编写sql语句:6. 启动项目,在Postman访问获取用户信息的接口:
image.png
作者:hrabbits
链接:https://www.jianshu.com/p/5a372d096597



