因为只有c3p0数据库没有starter,所以本文描述的是c3p0的starter,如果你想替换成其他的连接池,改下配置即可。
官方的开发starter标准推荐有3个模块:
- 核心功能库(本文案例是c3p0连接池,所以引入c3p0的依赖即可)
- 自动配置模块(此模块是专门处理自动配置的)
- starter模块(主要处理自动配置模块与核心模块以及其它让你的核心模块能正常运行起来的其它所有依赖,没有任何代码)
然后提一下artifactId的命名问题,Spring 官方 Starter通常命名为spring-boot-starter-{name} 如 spring-boot-starter-web, Spring官方建议非官方Starter命名应遵循{name}-spring-boot-starter的格式。
首先创建一个自动配置模块,命名为c3p0-spring-boot-starter-autoconfigure,添加以下依赖:
<?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>custom-spring-start-parent</artifactId>
<groupId>com.edu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>c3p0-spring-boot-starter-autoconfigure</artifactId>
<dependencies>
// 添加此依赖,那些条件注解才可以使用,最好设置为 optional 为 true
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<optional>true</optional>
</dependency>
// c3p0 连接池依赖
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.3</version>
</dependency>
// 自动生成实体类 get set 注解,可要可不要
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
在创建一个autoconfigure包(包名没要求),在包下面创建C3p0的自动配置类(官方推荐自动配置类以AutoConfiguration结尾),文件名为C3p0AutoConfiguration。
// 首先告诉spring boot 这是一个配置类
@Configuration
// 这个注解的意思是,当这个Spring boot 项目中有 DataSource 这个类时,此配置类才生效
@ConditionalOnClass(DataSource.class)
public class C3p0AutoConfiguration {
@Bean
// 这个注解的意思是当spring容器中没有名字叫做dataSource类型为DataSource时才注册
@ConditionalOnMissingBean
public DataSource dataSource() throws Exception{
// 创建一个 c3p0 的连接池
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/demo");
dataSource.setUser("root");
dataSource.setPassword("root");
return dataSource;
}
}
并且要在resource文件夹下创建一个META-INF文件夹,在文件夹下创建一个spring.factories文件,里面写上一行代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
autoconfigure.C3p0AutoConfiguration //你的自动配置类的全称
到此自动配置类已经可以了,但是现在又有一个新的问题,用户的数据库配置不可能自己写死啊,所以还得写个配置文件,读取配置文件里的内容进行配置。在autoconfigure包下创建一个c3p0的读取配置类。
@Data
@ConfigurationProperties(prefix = "c3p0")
public class C3p0Properties {
private String driver;
private String url;
private String username;
private String password;
}
并且自动配置类也得改变
@Configuration
@ConditionalOnClass(DataSource.class)
@EnableConfigurationProperties(C3p0Properties.class)
public class C3p0AutoConfiguration {
@Autowired
private C3p0Properties c3p0Properties;
@Bean
@ConditionalOnMissingBean
public DataSource dataSource() throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(c3p0Properties.getDriver());
dataSource.setJdbcUrl(c3p0Properties.getUrl());
dataSource.setUser(c3p0Properties.getUsername());
dataSource.setPassword(c3p0Properties.getPassword());
return dataSource;
}
}
现在既能自定义一个c3p0的连接池,又能读取配置文件信息,所以自动配置类结束。
创建一个c3p0-spring-boot-starter模块,这个模块只管理依赖。
<?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>custom-spring-start-parent</artifactId>
<groupId>com.edu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>c3p0-spring-boot-starter</artifactId>
<dependencies>
//自动配置模块所需依赖
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.3</version>
</dependency>
// 刚刚写的自动配置模块
<dependency>
<groupId>com.edu</groupId>
<artifactId>c3p0-spring-boot-starter-autoconfigure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
现在我们测试刚刚写的自动配置类能不能用上。创建个模块c3p0-spring-boot-sample,在创建个com包,在com包里面进行分层。
Info
@Data
public class Info {
private String name;
private Integer age;
private Date time;
}
InfoDao
public interface InfoDao {
List<Info> getAll(int pageNum, int pageSize);
}
InfoService
public interface InfoService {
List<Info> getAll(int pageNum, int pageSize);
}
InfoServiceImpl
@Service
public class InfoServiceImpl implements InfoService {
@Autowired
private InfoDao infoDao;
@Override
public List<Info> getAll(int pageNum, int pageSize) {
return infoDao.getAll(pageNum, pageSize);
}
}
InfoController
@Controller
public class InfoController {
@Autowired
private InfoService infoService;
@RequestMapping("/index")
@ResponseBody
public List<Info> index(){
return infoService.getAll(0, 2);
}
}
CustomStarterApp
@SpringBootApplication
@MapperScan("com.dao")
public class CustomStarterApp {
public static void main(String[] args) {
SpringApplication.run(CustomStarterApp.class, args);
}
}
InfoDao.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.dao.InfoDao">
<select id="getAll" resultType="info">
select * from info
</select>
</mapper>
application.yml
mybatis:
#对应实体类路径
type-aliases-package: com.entity
#对应mapper映射文件路径
mapper-locations: classpath:mapper/*.xml
# mybatis日志
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
c3p0:
driverClassname: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?serverTimezone=GMT
username: root
password: root
pagehelper:
helper-dialect: mysql
support-methods-arguments: true
# 日期格式转换
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
依赖
<?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>custom-spring-start-parent</artifactId>
<groupId>com.edu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>c3p0-spring-boot-sample</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.edu</groupId>
<artifactId>c3p0-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
结果:
到此c3p0连接池自动配置就已经配置完成了,如果你不想用c3p0,你可以把自动配置类理的dataSource类换成你自己想要的那个连接池即可。