符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
这篇文章将为大家详细讲解有关在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的内容如下所示:
|
在META-INF目录下,还要加一个persistence.xml配置文件,如下所示:
|
接着,对增删改查操作进行测试,如下所示:
插入记录:
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实例”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。