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