您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關在Spring框架中如何集成JPA實例,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
Hibernate 3.2支持JPA注解,Spring集成JPA是可選的。
實體類MyUser,使用JPA注解實現到數據庫表myUser的映射,如下所示:
package org.shirdrn.entity; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Transient; @Entity public class MyUser { private Long id; private String userName; private String password; private String gender; private Integer age; private Integer birthYear; private String addr; private String email; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="userName") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Column(name="password") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Column(name="gender") public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Column(name="age") public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Column(name="addr") public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Column(name="email") public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Transient public Integer getBirthYear() { return new Integer(2008-age); } public void setBirthYear(Integer age) { this.birthYear = new Integer(2008-age); } } |
其中,birthYear不是數據庫中的字段,使用JPA的@Transient注解,在映射的時候,會忽略掉該成員。
持久層DAO接口如下:
package org.shirdrn.dao; import java.util.List; import org.shirdrn.entity.MyUser; import org.springframework.transaction.annotation.Transactional; @Transactional public interface MyUserDAO { public void createMyUser(MyUser myUser); public void deleteMyUser(MyUser myUser); public void updateMyUser(MyUser myUser); public List |
這里,使用了JPA注解,聲明事務。
DAO實現類,如下所示:
package org.shirdrn.dao.impl; import java.util.List; import org.shirdrn.dao.MyUserDAO; import org.shirdrn.entity.MyUser; import org.springframework.orm.jpa.support.JpaDaoSupport; public class MyUserDAOImpl extends JpaDaoSupport implements MyUserDAO { public void createMyUser(MyUser myUser) { getJpaTemplate().persist(myUser); } public void deleteMyUser(MyUser myUser) { MyUser dbMyUser = getJpaTemplate().find(MyUser.class, myUser.getId()); getJpaTemplate().remove(dbMyUser); } public void updateMyUser(MyUser myUser) { MyUser dbMyUser = getJpaTemplate().find(MyUser.class, myUser.getId()); if(myUser.getUserName() != null){ dbMyUser.setUserName(myUser.getUserName()); } if(myUser.getAddr() != null){ dbMyUser.setAddr(myUser.getAddr()); } getJpaTemplate().merge(dbMyUser); } @SuppressWarnings("unchecked") public List @SuppressWarnings("unchecked") public List |
因為繼承了JpaDaoSupport,索引需要獲取一個JpaTemplate來實現訪問數據庫,在Spring的配置文件中要注入一個org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean。
Spring的配置文件applicationContext.xml的內容如下所示:
<BEANSXMLNS="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
|
在META-INF目錄下,還要加一個persistence.xml配置文件,如下所示:
<PERSISTENCEXMLNS="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"version="1.0">
|
接著,對增刪改查操作進行測試,如下所示:
插入記錄:
packageorg.shirdrn.test; importorg.shirdrn.dao.MyUserDAO; importorg.shirdrn.entity.MyUser; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassTestCreateMyUser{ publicstaticvoidmain(String[]args){ MyUserDAOmyUserDAO=(MyUserDAO)ctx.getBean("myUserDAOImpl"); MyUsermyUser=newMyUser(); myUser.setUserName("JohnXa"); myUser.setPassword("123456"); myUser.setGender("男"); myUser.setAge(newInteger(25)); myUser.setAddr("NewYork"); myUser.setEmail("john@hotmail.com"); myUserDAO.createMyUser(myUser); } } |
刪除記錄:
packageorg.shirdrn.test; importorg.shirdrn.dao.MyUserDAO; importorg.shirdrn.entity.MyUser; importorg.springframework.context.ApplicationContext; importorg.springframework.ClassPathXmlApplicationContext; publicclassTestDeleteMyUser{ publicstaticvoidmain(String[]args){ ApplicationContextctx=newClassPathXml("applicationContext.xml"); MyUserDAOmyUserDAO=(MyUserDAO)ctx.getBean("myUserDAOImpl"); MyUsermyUser=newMyUser(); myUser.setId(newLong(29)); myUserDAO.deleteMyUser(myUser); |
修改記錄:
packageorg.shirdrn.test; importorg.shirdrn.dao.MyUserDAO; importorg.shirdrn.entity.MyUser; importorg.springframework.context.ApplicationContext; importorg.springframework.context.ClassPathXmlApplicationContext; publicclassTestUpdateMyUser{ |
查詢記錄:
packageorg.shirdrn.test; importjava.util.List; importorg.shirdrn.dao.MyUserDAO; importorg.shirdrn.entity.MyUser; importorg.springframework.context.ApplicationContext; importorg.springframework.context.ClassPathXmlApplicationContext; publicclassTestQueryAllMyUser{ publicstaticvoidmain(String[]args){ ApplicationContextctx=newClassPathXmContext("applicationContext.xml"); MyUserDAOmyUserDAO=(MyUserDAO)ctx.getBean("myUserDAOImpl"); List |
關于“在Spring框架中如何集成JPA實例”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。