JpaRepository中的SERVICE层

我一直在阅读春季启动教程,大部分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实现,我这样使用

道执行

@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。 我通过使用在线参考开发了这个,并花费了更多时间。 我尽我所能,但我无法做到。 请帮我解决这个问题。 提前致谢

链接地址: http://www.djcxy.com/p/87895.html

上一篇: SERVICE layer in jpaRepository

下一篇: Why always have single implementation interfaces in service and dao layers?