spring aop示例
这篇文章描述了一个使用内存中HSQL数据库的简单Spring JpaRepository示例。 该代码示例可从GitHub的Spring-JpaRepository目录中获得。 它基于带有注释的Spring-MVC-示例和此处提供的信息 。
JPA资料库
在此示例中,我们实现了一个虚拟bean:
@Entity
@AutoProperty
public class SomeItem {@Id@GeneratedValue(strategy=GenerationType.AUTO)private long Id;private String someText;/* ...Setters & Getters */}
和相应的JpaRepository :
@Transactional
public interface SomeItemRepositoryextends JpaRepository<SomeItem, Long> {}
服务与控制器
接下来,我们实现一个将注入我们的存储库的服务。 我们还用伪数据填充存储库:
@Service
@Repository
public class SomeItemService {@Autowiredprivate SomeItemRepository someItemRepository;@PostConstruct@Transactionalpublic void populate() {SomeItem si = new SomeItem();si.setSomeText("aaa");someItemRepository.saveAndFlush(si);si = new SomeItem();si.setSomeText("bbb");someItemRepository.saveAndFlush(si);si = new SomeItem();si.setSomeText("ccc");someItemRepository.saveAndFlush(si);}@Transactional(readOnly=true)public List<SomeItem> getAll() {return someItemRepository.findAll();}@SuppressWarnings("AssignmentToMethodParameter")@Transactionalpublic SomeItem saveAndFlush(SomeItem si) {if ( si != null ) {si = someItemRepository.saveAndFlush(si);}return si;}@Transactionalpublic void delete(long id) {someItemRepository.delete(id);}}
和一个控制器:
@Controller
public class MyController {@Autowiredprivate SomeItemService someItemService;@RequestMapping(value = "/")public ModelAndView index() {ModelAndView result = new ModelAndView("index");result.addObject("items", this.someItemService.getAll());return result;}@RequestMapping(value = "/delete/{id}")public String delete(@PathVariable(value="id") String id) {this.someItemService.delete(Long.parseLong(id));return "redirect:/";}@RequestMapping(value = "/create")@SuppressWarnings("AssignmentToMethodParameter")public String add() {SomeItem si = new SomeItem();si.setSomeText("Time is: " + System.currentTimeMillis());this.someItemService.saveAndFlush(si);return "redirect:/";}}
JPA配置
除了基于HSQL数据库的内存实例创建实体管理器之外,我们还通过以下方式启用JPA存储库:
@EnableJpaRepositories批注:
@Configuration
@EnableJpaRepositories(basePackages={"com.jverstry"})
@EnableTransactionManagement
public class JpaConfig implements DisposableBean {private EmbeddedDatabase ed;@Bean(name="hsqlInMemory")public EmbeddedDatabase hsqlInMemory() {if ( this.ed == null ) {EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();this.ed = builder.setType(EmbeddedDatabaseType.HSQL).build();}return this.ed;}@Beanpublic LocalContainerEntityManagerFactoryBean entityManagerFactory(){LocalContainerEntityManagerFactoryBean lcemfb= new LocalContainerEntityManagerFactoryBean();lcemfb.setDataSource(this.hsqlInMemory());lcemfb.setPackagesToScan(new String[] {"com.jverstry"});lcemfb.setPersistenceUnitName("MyPU");HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter();lcemfb.setJpaVendorAdapter(va);Properties ps = new Properties();ps.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");ps.put("hibernate.hbm2ddl.auto", "create");lcemfb.setJpaProperties(ps);lcemfb.afterPropertiesSet();return lcemfb;}@Beanpublic PlatformTransactionManager transactionManager(){JpaTransactionManager tm = new JpaTransactionManager();tm.setEntityManagerFactory(this.entityManagerFactory().getObject() );return tm;}@Beanpublic PersistenceExceptionTranslationPostProcessor exceptionTranslation(){return new PersistenceExceptionTranslationPostProcessor();}@Overridepublic void destroy() {if ( this.ed != null ) {this.ed.shutdown();}}}
JSP页面
我们创建一个简单的页面来列出具有删除链接的现有项目,以及创建新项目的可能性:
运行示例
可以使用maven tomcat:run目标运行它。 然后,浏览:http:// localhost:9191 / spring-jparepository /
参考: Spring JpaRepository示例(内存中),来自我们的JCG合作伙伴 Jerome Versrynge,在技术说明博客中。
翻译自: https://www.javacodegeeks.com/2013/04/spring-jparepository-example-in-memory.html
spring aop示例