spring中bean配置8.0版本数据库,时区解决问题!

spring中bean配置8.0版本数据库,时区解决问题!

使用属性文件配置请看另外一篇文章
spring中bean配置8.0版本数据库(使用属性文件配置)
话不多说,直接上代码!

如果jdbcurl那一行不加上后面的server…就会报时区错误!
这是mysql8.0版本独有的错误
并且8.0版本的驱动为:

com.mysql.cj.jdbc.Driver

并非以往的:

com.mysql.jdbc.Driver

p标签作用类似于property,不必细看,第四行为导入p
bean文件为:(重点在jdbcurl那一行)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		p:user="root" 
		p:password="123456"
		p:driverClass="com.mysql.cj.jdbc.Driver"
		p:jdbcUrl="jdbc:mysql://localhost:3306/dbname?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT%2B8">
	</bean>
</beans>

测试类:

package springDemo.properties;
import java.sql.SQLException;

import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {

	public static void main(String[] args) throws SQLException {
		// TODO Auto-generated method stub
		ApplicationContext ctx =  new ClassPathXmlApplicationContext("beans-properties.xml");
		
		DataSource dataSource = (DataSource)ctx.getBean("dataSource");
		System.out.println(dataSource.getConnection());
	}
}

谢谢大家!喜欢有用点个关注哦!

发布了47 篇原创文章 · 获赞 51 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_41976613/article/details/97635169
今日推荐