休眠不自动创建表

时间:2019-06-24 04:41:20

标签: java spring hibernate spring-mvc

我已经为Mysql配置了Spring和Hibernate,但是hibernate没有自动创建表。我已经在eclipse数据源连接中配置了mysql,并尝试了tomcat 8和tomcat 9

我已配置数据库属性来配置休眠状态

请在下面找到代码

Database.properties

mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/bookdb
mysql.user=root
mysql.password=root

# Hibernate properties
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update

#C3P0 properties
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=150

AppConfig

    package com.bushansirgur.config;

    import java.util.Properties;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScans;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;
    import org.springframework.orm.hibernate5.HibernateTransactionManager;
    import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import static org.hibernate.cfg.Environment.*;

    @Configuration
    @PropertySource("classpath:db.properties")
    @EnableTransactionManagement
    @ComponentScans(value = { @ComponentScan("com.bushansirgur.dao"),
          @ComponentScan("com.bushansirgur.service") })
    public class AppConfig {

       @Autowired
       private Environment env;

       @Bean
       public LocalSessionFactoryBean getSessionFactory() {
          LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();

          Properties props = new Properties();
          // Setting JDBC properties
          props.put(DRIVER, env.getProperty("mysql.driver"));
          props.put(URL, env.getProperty("mysql.url"));
          props.put(USER, env.getProperty("mysql.user"));
          props.put(PASS, env.getProperty("mysql.password"));

          // Setting Hibernate properties
          props.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));
          props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));

          // Setting C3P0 properties
          props.put(C3P0_MIN_SIZE, env.getProperty("hibernate.c3p0.min_size"));
          props.put(C3P0_MAX_SIZE, env.getProperty("hibernate.c3p0.max_size"));
          props.put(C3P0_ACQUIRE_INCREMENT, 
                env.getProperty("hibernate.c3p0.acquire_increment"));
          props.put(C3P0_TIMEOUT, env.getProperty("hibernate.c3p0.timeout"));
          props.put(C3P0_MAX_STATEMENTS, env.getProperty("hibernate.c3p0.max_statements"));

          factoryBean.setHibernateProperties(props);
          factoryBean.setPackagesToScan("com.bushansirgur.model");

          return factoryBean;
       }

       @Bean
       public HibernateTransactionManager getTransactionManager() {
          HibernateTransactionManager transactionManager = new HibernateTransactionManager();
          transactionManager.setSessionFactory(getSessionFactory().getObject());
          return transactionManager;
       }
    }

MyWebAppIntitalizer

    package com.bushansirgur.config;

    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

    public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

       @Override
       protected Class<?>[] getRootConfigClasses() {
          return new Class[] { AppConfig.class };
       }

       @Override
       protected Class<?>[] getServletConfigClasses() {
          return new Class[] { WebConfig.class };
       }

       @Override
       protected String[] getServletMappings() {
          return new String[] { "/" };
       }
    }

WebConfig

    package com.bushansirgur.config;

    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;

    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = { "com.bushansirgur.controller" })
    public class WebConfig extends WebMvcConfigurerAdapter {

    }

BookModel.java

package com.bushansirgur.model;

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

@Entity(name = "Book")
public class Book {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;
   private String title;
   private String author;

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public String getAuthor() {
      return author;
   }

   public void setAuthor(String author) {
      this.author = author;
   }

}

1 个答案:

答案 0 :(得分:1)

问题是AppConfig类位于com.bushansirgur.config之下的软件包@ComponentScan中,而且我确实在不同的类上看到了多个@ComponentScan注释,不建议这样做。您只需在带有基础包的Main类上使用它一次即可

 @Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.bushansirgur" })
public class WebConfig extends WebMvcConfigurerAdapter {

}