
1.spring-boot-starter-xxxx:SpringBoot中自带的starter
2.xxx-spring-boot-starter:第三方和SpringBoot集成提供的starter
druid-spring-boot-starter(包含:druid数据源的原始依赖+自配配置功能的依赖)
druid数据源的原始依赖
<bean id="dataSource" class="">
<property name="" value="">
</bean>
druid-spring-boot-starter
不需要自己手动的创建datasource,自配配置功能的依赖自动的把数据源的对象创建出来,并放到spring容器中.

首先先要添加相应的依赖
1.1 添加Druid的依赖.
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.4</version>
</dependency>
1.2 添加mysql连接驱动依赖.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
1.3 添加jdbc依赖.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
(1)其中出现bug:Failed to introspect Class [com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
解决方案:
将druid升级
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10 </version>
</dependency>
(2)mysql连接异常:The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zo错误
解决方案:

结果如下:

方式1代码:
application.properties
##修改端口号
server.port=8082
##应用上下文
#server.servlet.context-path=/crm
#spring.main.banner-mode=off
db.username=root
db.password=123456
db.url=jdbc:mysql://localhost:3306/opinion?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
db.driverClassName=com.mysql.jdbc.Driver
spring.devtools.restart.triggerFile=trigger.file
DataSourceController
package com.helloworld.datasource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.sql.DataSource;
import java.sql.SQLException;
@Controller
public class DataSourceController {
@Autowired
private DataSource dataSource;
@RequestMapping("/druidDataSource")
@ResponseBody
public String dataSource() throws Exception {
System.out.println(dataSource.getConnection());
System.out.println(dataSource);
return "hello dataSource";
}
}
AppConfig
package com.helloworld.datasource;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
@SpringBootApplication
public class AppConfig {
@Bean
@ConfigurationProperties(prefix = "db")
public DataSource dataSource(){
DruidDataSource ds=new DruidDataSource();
return ds;
}
public static void main(String[] args) {
SpringApplication.run(AppConfig.class,args);
}
}
方法2代码:
application.properties
###修改端口号
#server.port=8082
###应用上下文
##server.servlet.context-path=/crm
##spring.main.banner-mode=off
#db.username=root
#db.password=123456
#db.url=jdbc:mysql://localhost:3306/opinion?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
#db.driverClassName=com.mysql.jdbc.Driver
#spring.devtools.restart.triggerFile=trigger.file
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.url=jdbc:mysql://localhost:3306/opinion?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.initial-size=5
DataSourceController
package com.helloworld.datasource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.sql.DataSource;
import java.sql.SQLException;
@Controller
public class DataSourceController {
@Autowired
private DataSource dataSource;
@RequestMapping("/druidDataSource")
@ResponseBody
public String dataSource() throws Exception {
System.out.println(dataSource.getConnection());
System.out.println(dataSource);
return "hello dataSource";
}
}
AppConfig
package com.helloworld.datasource;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
@SpringBootApplication
public static void main(String[] args) {
SpringApplication.run(AppConfig.class,args);
}
}


来源:CSDN
作者:zhouzhou_98
链接:https://blog.csdn.net/zhouzhou_98/article/details/104156477