SpingBoot 錯誤 java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone...


SpringBoot 與緩存

一、概念

JSR-107、Spring緩存抽象、整合Redis

jsr107 太復雜,用Spring緩存抽象,既有自己的緩存管理器,也可以引用jsr

幾個重要概念&緩存注解

Cache 緩存接口,定義緩存操作。實現有:RedisCache、EhCacheCache
CacheManager 緩存管理器,管理各種緩存(Cache)組件
@Cacheable 主要針對方法配置,能夠根據方法的請求參數對其結果進行緩存
@CacheEvict 情況緩存
@CachePut 保證方法被調用,又希望結果被緩存
@EnabelCaching 開啟基於注解的緩存
keyGenerator 緩存數據是key生成策略
serialize 緩存數據時value序列化策略

二、Springboot-cache Demo

1.新建一個springboot 項目

項目結構

pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.cmdzz</groupId>
    <artifactId>springboot-01-cache</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-01-cache</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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.1.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/spring-cache
spring.datasource.username=root
spring.datasource.password=123456
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver

SpringBootCacheApplication.java
package cn.cmdzz;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 搭建基本環境
 * 1.導入數據庫文件 創建出department和employer表
 * 2.創建javebean封裝數據
 * 3.整合MyBatis操作數據庫
 *      1)配置數據源信息
 *      2)使用注解版的MyBatis
 *          *@MapperScan指定需要掃描的mapper接口所在的包
 *
 *
 */
@MapperScan("cn.cmdzz.mapper")
@SpringBootApplication
public class Springboot01CacheApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot01CacheApplication.class, args);
    }

}

bean

Department.java

package cn.cmdzz.bean;

/**
 * @author cmdzhizhu  @date 2020/5/15 16:19
 */
public class Department {

    private Integer id;
    private String departmentName;


 //get/set等省略

Employee.java

package cn.cmdzz.bean;

/**
 * @author cmdzhizhu  @date 2020/5/14 20:59
 */
//get set 快捷鍵 alt+insert
public class Employee {
    private Integer id;
    private String lastName;
    private String email;
    private Integer gender;
    private Integer dId;


//get set 等省略

mapper

EmployeeMapper.java

package cn.cmdzz.mapper;

import cn.cmdzz.bean.Employee;
import org.apache.ibatis.annotations.*;

/**
 * @author cmdzhizhu  @date 2020/5/15 16:30
 */
@Mapper
public interface EmployeeMapper {

    @Select("SELECT * FROM employee WHERE id = #{id}")
    public Employee getEmpById(Integer id);

    @Update("UPDATE employee SET lastName = #{lastName},email = #{email},gender=#{gender},d_id=#{dId} WHERE id=#{id}")
    public void updateEmp(Employee employee);

    @Delete("DELETE FROM employee WHERE id=#{id}")
    public void deleteEmpById(Integer id);

    @Insert("INSERT INTO employee(lastName,email,gender,d_id) VALUES(#{lastName},#{email},#{gender},#{dId}")
    public void insertEmployee(Employee employee);
}

Test

Springboot01CacheApplicationTests.java

package cn.cmdzz;

import cn.cmdzz.bean.Employee;
import cn.cmdzz.mapper.EmployeeMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot01CacheApplicationTests {

    @Autowired
    EmployeeMapper employeeMapper;

    @Test
    void contextLoads() {
        Employee employee = employeeMapper.getEmpById(1);
        System.out.println(employee);
    }

}

錯誤&修復

錯誤描述

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.

原因及解決方法

原因是使用了Mysql Connector/J 6.x以上的版本,然后就報了時區的錯誤,解決方法:
    方法1.
    在配置url的時候不能簡單寫成 :
    jdbc:mysql://localhost:3306/test
    而是要寫成 :
    jdbc:mysql://localhost:3306/test?serverTimezone=UTC
    方法2.更改mysql connector版本

這里修改為:

spring.datasource.url=jdbc:mysql://localhost:3306/spring-cache?serverTimezone=UTC

測試成功


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM