【问题标题】:How to Configure spring mvc 4 with hibernate and Jpa using java config如何使用 java config 使用 hibernate 和 Jpa 配置 spring mvc 4
【发布时间】:2016-09-23 09:06:21
【问题描述】:

我正在尝试使用 java 配置连接 spring-mvc 4 并使用 jpa 进行休眠,但它不起作用,请告诉我错误或指导我使用新的配置设置

第一个配置类是-

package com.codeoxy.web.config.servlet3;

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

 import com.codeoxy.web.config.SpringWebConfig;

 public class MyWebInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

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

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

  @Override
  protected Class<?>[] getRootConfigClasses() {
      return null;
  }

  }

春天的第二节课在后面

 package com.codeoxy.web.config;

 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

  @EnableWebMvc
  @Configuration
 @ComponentScan({ "com.codeoxy.web" })
 public class SpringWebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/jsp/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
  }

  }

而jpa配置文件是

  package com.codeoxy.web.config;

  import java.util.Properties;

  import javax.persistence.EntityManagerFactory;
  import javax.sql.DataSource;

 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig{

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  em.setDataSource(dataSource());
  em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });

  JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  em.setJpaVendorAdapter(vendorAdapter);
  em.setJpaProperties(additionalProperties());

  return em;
  }

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

  @Bean
  public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
  JpaTransactionManager transactionManager = new JpaTransactionManager();
  transactionManager.setEntityManagerFactory(emf);

  return transactionManager;
  }

  @Bean
  public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
  return new PersistenceExceptionTranslationPostProcessor();
  }

  Properties additionalProperties() {
  Properties properties = new Properties();
  properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
  properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
  return properties;
   }
    }

谁能帮帮我,谢谢。

【问题讨论】:

    标签: hibernate spring-mvc spring-data-jpa


    【解决方案1】:

    您的 setPackagesToScan 错误。请更新正确的包装。而且你没有提供错误日志,没有它很难回答。

    em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });

    【讨论】:

    • 谢谢。但是再次出现错误,您能告诉我有关此类配置的完整数据吗?
    猜你喜欢
    • 2014-04-06
    • 1970-01-01
    • 2015-12-04
    • 2018-06-02
    • 2012-07-01
    • 2016-02-29
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    相关资源
    最近更新 更多