无法创建请求的服务[org.hibernate .engine.jdbc.env.spi.JdbcEnvironment] -MySQL

In-*_*ung 5 java spring hibernate maven

我是Hibernate的新手。我目前正在使用Spring启动框架,并尝试通过休眠创建数据库表。

我知道之前曾问过同样的问题,但是我似乎无法根据我的环境弄清楚如何解决该错误。

hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.mm.mysql.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306</property>
    <property name="connection_userid">user</property>
    <property name="connection_pwd">pass</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection_pool_size">true</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.MySQLDialect</property>

    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">1</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbmdl.auto">update</property>

    <!-- Names the annotated entity class -->
    <mapping class="com.test.springboot.model.AdultParticipant" />

</session-factory>
Run Code Online (Sandbox Code Playgroud)

主班

     public static void main(String[] args) throws Exception {
    SpringApplication.run(WebApplication.class, args);

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

    Session session = factory.openSession();
    Transaction t = session.beginTransaction();

    AdultParticipant ap = new AdultParticipant();
    ap.setFirstName("User"); 
    ap.setLastName("UserLastName");

    session.persist(ap);

    t.commit();

    session.close();
    System.out.println("successfully saved");
Run Code Online (Sandbox Code Playgroud)

POJO课

@Entity
@Table(name = "adultparticipant")
public class AdultParticipant {

@GeneratedValue
@Id
@Column (name = "id")
private int id;

@Column (name = "firstName")
private String firstName;

@Column (name = "lastName")
private String lastName;


public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}


public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}
Run Code Online (Sandbox Code Playgroud)

DAOImpl类

  public class AdultParticipantDAOImpl implements AdultParticipantDAO{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

@Override
public void save(AdultParticipant ap) {
    Session session = this.sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.persist(ap);
    tx.commit();
    session.close();
}

@SuppressWarnings("unchecked")
@Override
public List<AdultParticipant> list() {
    Session session = this.sessionFactory.openSession();
    List<AdultParticipant> adultParticipants = session.createQuery("from AdultParticipant").list();
    session.close();
    return adultParticipants;
 }
}
Run Code Online (Sandbox Code Playgroud)

DAO类

public interface AdultParticipantDAO {

public void save(AdultParticipant p);

public List<AdultParticipant> list();

}
Run Code Online (Sandbox Code Playgroud)

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>hello-springboot</artifactId>
<name>hello-springboot</name>
<description>hello-springboot</description>
<packaging>war</packaging>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>

<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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.40</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

控制台错误

2017-03-13 11:48:40.512  WARN 9532 --- [           main] org.hibernate.orm.connections.pooling    : HHH10001002: Using H
ibernate built-in connection pool (not for production use!)
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate
.engine.jdbc.env.spi.JdbcEnvironment]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
Run Code Online (Sandbox Code Playgroud)

p u*_*ran 8

将 MySql 驱动程序升级到mysql-connector-java - 8.0.17

使用MySQL 5.5 以上版本者

更改他们的驱动程序属性

com.mysql.jdbc.Drivercom.mysql.cj.jdbc.Driver

因为:

加载类 com.mysql.jdbc.Driver。这已被弃用。新的驱动程序类是 com.mysql.cj.jdbc.Driver。驱动程序通过 SPI 自动注册,通常不需要手动加载驱动程序类。INFO - HHH000401: using driver [com.mysql.jdbc.Driver] at URL....

hibernate.properties

hibernate.connection.driver_class = com.mysql.cj.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)

或者如果您正在使用hibernate.cfg.xml更新

<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
Run Code Online (Sandbox Code Playgroud)


Gau*_*avJ 1

您需要mysql在依赖项中添加 JDBC jar。


  1. 将您的驱动程序类名称修复为com.mysql.jdbc.Driver.
  2. 将您的用户名和密码属性修复为

    "connection.username" for database user
    "connection.password" for database user password
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建mysql数据库。看到这个

  4. 对于 SSL 警告,请修改您的 connection.url 以包含 use ssl false。例如,jdbc:mysql://localhost:3306/<enter-your-database>?autoReconnect=true&useSSL=false

  5. 将你的 mysql 方言修改为org.hibernate.dialect.MySQLDialect

  6. 使用<property name="hbmdl.auto">create-drop</property>而不是<property name="hbmdl.auto">update</property>不要在生产中使用此选项。您应该自己创建架构,而不是通过 hibernate 来创建架构。