Springboot+Jpa+mysql

目的:通过maven建立项目,springboot 配置datasource  和Jpa的相关配置来连接mysql.


1,软件版本release 

    Sprng_tools_suite:   4release;

    spring-boot :   2.1.6version;

    maven  :   3.6.release;

    mysql:      5.5.X release;


2,建立流程

      ----dos窗口下:net  start mysql ,----意思是开启mysql 的服务。

      ----项目建立---Sprng_tools_suite新建一个Spring-starter-project的项目,将所需要的dependence:spring-data-jpa mysql加入进maven的pom.xml文件。 

      ----配置application.properties---文件。文件的属性首先需要我们的配置的datasource,也就是连接数据库的3个重要配置:

      ---配置@Entity 注解下持久化对象。因为是自动配置,如果我们建立了如下类的话,就不需要配置换着方法去配置映射了。

      然后写一个持久化的接口继承JpaRepository<Use,Long >

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
package com.example.demo.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_use")
public class Use {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private int age;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Use [id=" + id + ", name=" + name + ", age=" + age + "]";
    }

    
}

      直接运行有注解@SpringBootApplication注解的类的main 方法。

      运行完后console  下面会出现一些列的异常报告:

        

Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: 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.
Caused by: com.mysql.cj.exceptions.InuvalidConnectionAttributeException: 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.
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]...
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set....

    看完第一眼是:好像挺多的异常。 

      ----  

3,针对异常信息进行处理。

    思路1:最后一行的异常信息告诉我们,我们缺少hibernate.dialect 方言的设置。而且提示这个设置就是必须的。

        查看官方文档对于方言的名称是:spring.jpa.database-platform=?

        这里解释下,为什么上面要介绍release版本的问题。      

        首先数据库的方言有的会根据版本有对应的dialect方言,因为是mysql5.5版本的,网上查找的dialect 方言版本应该是:org.hibernate.dialect.MySQL55Dialect ,

        将spring.jpa.database-platform=org.hibernate.dialect.MySQL55Dialect

        配置到properties文件中。

        查看的官方文档要求我们还要配置:

          spring.jpa.show-sql=true

      加入后,如果幸运的话,就不再出问题,当然问题也可能出现:server time zone的异常 解决的方法

    解决mysql 时区的方法:进入mysql

            dos 窗口:select  now();

                 set global time_zone = '+8:00';

        重启我们的main 方法 ,发现完美无异常了。

      plus:这个只是简单的配置.如果需要的话,可给datasource 配置连接数。通过池化技术,来解决数据库crud的效率问题。

        另外如果需要test的话,就写个test 的方法去测试就Ok了。


猜你喜欢

转载自www.cnblogs.com/futureT/p/11300799.html