SpringBoot应用程序运行正常,但是休眠& MySql相关属性不起作用

时间:2017-04-17 11:47:16

标签: mysql hibernate maven intellij-idea spring-boot

    @Configuration
    @EnableJpaRepositories(basePackages = 
    "com.gmt.user",entityManagerFactoryRef = 
    "userEntityManager",transactionManagerRef = "userTransactionManager")

    public class UserConfig {


@Bean
@Primary
public LocalContainerEntityManagerFactoryBean userEntityManager(){
    LocalContainerEntityManagerFactoryBean em= new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(userDatasource());
    em.setPackagesToScan(new String[]{"com.gmt.user"});

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);

    HashMap<String,Object> properties = new HashMap<String, Object>();
    properties.put("hibernate.hbm2ddl.auto","craete");
    properties.put("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect");
    properties.put("hibernate.show_sql","true");
    em.setJpaPropertyMap(properties);
    return em;
}

@Bean
@Primary
public DataSource userDatasource(){
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/schema_name");
    dataSource.setUsername("root");
    dataSource.setPassword("password12");
    return dataSource;

}

@Bean
@Primary
public PlatformTransactionManager userTransactionManager(){
    JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();

    jpaTransactionManager.setEntityManagerFactory(userEntityManager().getObject());

    return jpaTransactionManager;

}

}

这是我的SpringBoot应用程序

    @SpringBootApplication
    @ComponentScan(basePackages = "com.gmt")
    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, 
    HibernateJpaAutoConfiguration.class})
    public class Client {
    public static void main(String[] args){
    SpringApplication.run(Client.class,args);

    System.out.println(" ***** Inside Spring Boot Application ***** ");


   }
   }

这是我的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>

<groupId>Simple_Spring</groupId>
<artifactId>Spring01</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
    </dependency>

</dependencies>


<build>

    <plugins>

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

我已将模型类和相应的存储库放在同一个文件夹中。应用程序正常运行,没有任何错误,但没有连接到数据库,也没有在架构中创建表。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

您在此处拼错了hibernate.hbm2ddl.auto属性值:

properties.put("hibernate.hbm2ddl.auto","craete");

必须为create,但您已将其设置为craete

答案 1 :(得分:-1)

如果你使用 spring-boot-starter-data-jpa ,你的POM中的

不需要使用 <groupId>org.hibernate</groupId> 的其他工件。 “ spring-boot-starter-data-jpa ”会将Hibernate作为默认的JPA提供程序引入,这可能会导致依赖性冲突,因此我看到了类似的问题。 所以你说没有显示错误?你调试了吗?

同样在春季启动时你可以将你的属性设置为:

spring.datasource.* 

spring boot JPA将自动准备好

要更改hibernate属性,我们将使用前缀

 `spring.jpa.properties.*`

使用hibernate属性名称 在给定数据源URL的基础上,spring boot可以自动识别数据源驱动程序类。所以我们不需要配置驱动程序类。

编辑1 ________

#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/concretepage
spring.datasource.username=root
spring.datasource.password=
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE