spring boot項目的數據庫連接的一些問題
樓主現在練習spring boot項目,連接數據庫時遇到的一點問題和大家分享
一 、數據庫驅動
在spring boot2.x中,mysql的依賴為
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
mysql-connector-java的版本是6.x或者是8.x
這時數據庫驅動要使用com.mysql.cj.jdbc.Driver,不能使用com.mysql.jdbc.Driver
否則數據庫會報如下錯誤:
* Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
如果手動修改mysql-connector-java的版本,修改為5.x,則com.mysql.cj.jdbc.Driver會報紅,需要使用com.mysql.jdbc.Driver
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
如果在5.x中使用com.mysql.cj.jdbc.Driver,項目報錯java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
有的人在8.x中使用com.mysql.jdbc.Driver也不會報錯,只是會有一個警告,但是樓主還是建議6.x和8.x的版本使用com.mysql.cj.jdbc.Driver
二、數據庫時區相關問題
1. 數據庫時區報錯問題
樓主的項目是spring boot 2.1.4,默認mysql-connector-java的版本為:<version>8.0.15</version>
如果不指定時區 spring.datasource.url=jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=UTF-8
則項目報錯 :
* java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
解決方案 :在后面加上&serverTimezone=UTC,制定時區為世界標准時間,同時就帶來了第二個問題
2. 數據庫時間和本地時間不一致的問題
樓主的本地時間 :
樓主是使用的spring data jpa 來進行數據庫的操作
建表語句為:
CREATE TABLE `product_category` (
`category_id` INT(11) NOT NULL AUTO_INCREMENT,
`category_name` VARCHAR(64) NOT NULL COMMENT '類目名字',
`category_type` INT(11) NOT NULL COMMENT '類目編號',
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間',
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間',
PRIMARY KEY (`category_id`)
) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
樓主的實體類為
@Entity
@ToString
@Data
public class ProductCategory implements Serializable {
/**
* 類目id
*/
@Id@GeneratedValue(strategy = GenerationType.AUTO)
private Integer categoryId;
/**
* 類目名稱
*/
private String categoryName;
/**
* 類目編號
*/
private Integer categoryType;
/**
* 創建時間
*/
private Date createTime;
/**
* 修改時間
*/
private Date updateTime;
}
樓主使用的編輯器為idea,添加了lombok這個插件
創建 dao層
@Repository
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
}
進行數據插入
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryTest {
@Autowired
private ProductCategoryRepository repository;
@Test
public void testProductCategorySave(){
ProductCategory category = new ProductCategory();
category.setCategoryName("女生最愛");
category.setCategoryType(3);
repository.save(category);
}
}
此時查詢數據庫,時間為 :
* 此時數據庫的時間和我的本地時間相差了8小時,樓主瞬間懵比,這個不能差啊,樓主立馬各種百度
* 第一個解決方案是修改serverTimezone,樓主把url修改為jdbc:mysql://192.168.200.134/weixinsell?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
* 其實就是把serverTimezone修改為Asia/Shanghai,but,沒用,數據庫的時間還是要早8小時
*
* 樓主繼續修改serverTimezone為Asia/Hongkong
* 然后程序直接報錯:
* java.sql.SQLException: No timezone mapping entry for 'Asia/Hongkong'
* 樓主接着修改serverTimezone為Hongkong
* 但是依然不成功
* 程序為:
@Test
public void testProductCategoryUpdate(){
ProductCategory category = new ProductCategory();
category.setCategoryId(6);
category.setCategoryName("男生最愛");
category.setCategoryType(3);
repository.save(category);
}
*
* 樓主接着百度,立馬找到了第二種方法
* @SpringBootApplication
public class SellApplication {
@PostConstruct
void started() {
//TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
}
public static void main(String[] args) {
SpringApplication.run(SellApplication.class, args);
}
}
* 但是程序運行依然不成功,在程序中樓主的PostConstruct使用了Asia/Shanghai和GMT+8都沒用
* 再次百度也無非是修改url中的serverTimezone,樓主修改成GMT+8那些都沒用
* 接着百度,第三種方法是set/get方法上面加注解
* @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
* 樓主果斷的試了一下,然而依然沒有成功
解決方案
1. 樓主在數據庫中直接進行數據的查詢
2. 如圖
*
* 數據庫層面的時間直接比當前時間早8小時
* 樓主這時候趕緊的修改數據庫的時區
* 如圖 : 數據庫方面修改成功
*
3. 樓主趕緊的修改程序
* 如圖 : 終於成功了
* but樓主又發現了一個大問題
4. 大家注意這個男生最愛,create_time和update_time是一樣的,樓主明明對create_time這個字段沒有設置函數,怎么他也修改了,樓主立馬去百度
* 百度上面一個哥們讓樓主的pojo類上面添加注解@DynamicUpdate
* 樓主果斷的進行測試,run一下項目
* 大家看一下圖片,果然沒有用,樓主趕緊的接着百度,一個小伙伴也有過類似問題
* 這個小伙伴讓我把這兩個字段在pojo類中刪掉,樓主趕緊的試了一下,修改pojo類為:
@Entity
@ToString
@Data
public class ProductCategory implements Serializable {
/**
* 類目id
*/
@Id@GeneratedValue(strategy = GenerationType.AUTO)
private Integer categoryId;
/**
* 類目名稱
*/
private String categoryName;
/**
* 類目編號
*/
private Integer categoryType;
}
* run一下項目
* 代碼是:
@Test
public void testProductCategoryUpdate(){
ProductCategory category = new ProductCategory();
category.setCategoryId(6);
category.setCategoryName("年輕最愛");
repository.save(category);
}
* 成功了,不容易啊,一下子遇到這么多問題
* 
* 但是pojo類中有這兩個字段,時間還是解決不了,樓主接着查閱資料,待查出來的時候就改帖子,也希望各路大神批評指正