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

我是Hibernate的新手.我目前正在使用Spring启动框架并尝试通过hibernate创建数据库表.我知道之前问过同样的问题,但我似乎无法弄清楚如何根据我的环境修复错误.的hibernate.cfg.xmlhibernate-configurationsess...

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

我知道之前问过同样的问题,但我似乎无法弄清楚如何根据我的环境修复错误.

的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>

主要班级

     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");

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;
  }
}

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;
 }
}

DAO班

public interface AdultParticipantDAO {

public void save(AdultParticipant p);

public List<AdultParticipant> list();

}

的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>

控制台中的错误

2017-03-13 11:48:40.512 WARN 9532 — [main] org.hibernate.orm.connections.pooling:HHH10001002:使用H
ibernate内置连接池(不供生产使用!)
线程“main”中的异常org.hibernate.service.spi.ServiceException:无法创建请求的服务[org.hibernate
.engine.jdbc.env.spi.JdbcEnvironment]
    在org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
    在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)

解决方法:

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

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

"connection.username" for database user
"connection.password" for database user password

>创建mysql数据库.见this.
>对于SSL警告,请修改connection.url以包含使用ssl false.例如,jdbc:mysql:// localhost:3306 /< enter-your-database>?autoReconnect = true& useSSL = false
>将您的mysql方言修改为org.hibernate.dialect.MySQLDialect
>使用< property name =“hbmdl.auto”> create-drop< / property>而不是< property name =“hbmdl.auto”> update< / property>但是请勿在生产中使用此选项.您应该自己创建模式而不是通过休眠来创建模式.

本文标题为:java – 无法创建请求的服务[org.hibernate .engine.jdbc.env.spi.JdbcEnvironment] -MySQL

基础教程推荐