jpaRepository

时间:2017-11-06 19:00:15

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

我一直在阅读spring-boot教程,大多数mvc模式都是基于

实现的
  

1)Dao界面

     

2)Dao接口实现

     

3)持久性的服务界面

     

4)服务实施

我认为这是一种很好的做法,我正在努力。现在我尝试使用 JpaRepository ,这有助于轻松高效地实施。我项目的配置是

配置类

@Configuration
@PropertySource(value = { "classpath:application.properties" })
@EnableJpaRepositories(value={"com.dept.dao"})
public class ApplicationContextConfig {

    @Autowired
    private Environment environment;

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        //DataSource connections
    }

    private Properties getHibernateProperties() {
        //Hibernate properties
    }   

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(getDataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.dept.model" });
        sessionFactory.setHibernateProperties(getHibernateProperties());
        return sessionFactory;
    }

    @Autowired
    @Bean(name = "transactionManager")
    public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
        return transactionManager;
    }

     @Primary
     @Bean(name="entityManagerFactory")
     public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException {
         final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();

         entityManagerFactoryBean.setDataSource(getDataSource());
         // mention packae scan your classes annotated as @Entity
         entityManagerFactoryBean.setPackagesToScan(new String[] { "com.dept.model" });
         entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);

         final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
         vendorAdapter.setShowSql(true);
         entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
         entityManagerFactoryBean.setJpaProperties(getHibernateProperties());

         entityManagerFactoryBean.afterPropertiesSet();
         entityManagerFactoryBean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());

         return entityManagerFactoryBean;
     }

     @Primary
     @Bean(name = "transactionManager")
     public PlatformTransactionManager transactionManager() {
         final JpaTransactionManager transactionManager = new JpaTransactionManager();
         try {
             transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
             transactionManager.setDataSource(getDataSource());
             transactionManager.setJpaDialect(new HibernateJpaDialect());
             transactionManager.setJpaProperties(getHibernateProperties());
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         }
         return transactionManager;
     }

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

     @Bean
     public HibernateExceptionTranslator hibernateExceptionTranslator() {
         return new HibernateExceptionTranslator();
     }

     @Bean
     public JpaVendorAdapter japAdapter(){
         return new HibernateJpaVendorAdapter();
     }
}

示例我需要在数据库中 保存 此对象。所以我实现了方法

Doa界面

public interface DepartmentDao extends JpaRepository<Department, Integer>{

}

我遇到了如何实现Dao实现,我这样使用

Dao实施

@Repository
public class DepartmentDaoImpl implements DepartmentDao {

    @Autowired
    private SessionFactory sessionFactory;

    // All other overridden methods

    @Override
    public <S extends Department> S save(S entity) {        
        return (S) sessionFactory.getCurrentSession().save(entity);
    }
}

服务界面

public interface DepartmentService {
    // Other all necessary methods
    public <S extends Department> S save(S entity);
}

服务实施

@Service
public class DepartmentServiceImpl implements DepartmentService{
    @Autowired
    DepartmentDao departmentDao;

    @Override
    @Transactional
    public <S extends Department> S save(S entity) {        
        return departmentDao.save(entity);
    }
}

控制器类

@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveDepartment(@Valid @ModelAttribute("departmentForm") Department department) {

    departmentService.save(department);   //departmen already autowired
    return new ModelAndView("redirect:/department/list");
}

我尝试将此对象保存在数据库中,但没有发生任何事务。也没有错误。由于我刚开始使用弹簧引导,我对自己如何使用jparepository感到困惑。我通过使用在线参考开发了这个,花了更多的时间。我尽我最大努力,但我做不到。请帮我解决这个问题。提前致谢

0 个答案:

没有答案