点击这里给我发消息 点击这里给我发消息

在Spring框架中集成JPA实例

添加时间:2013-12-7
    相关阅读: 数据库 框架

    Hibernate 3.2支持JPA注解,Spring集成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 queryMyUser(String queryString);
public List queryAll();
}


    这里,使用了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 queryMyUser(String queryString) {
return (List)getJpaTemplate().find(queryString);
}
@SuppressWarnings("unchecked")
public List queryAll() {
return (List)getJpaTemplate().find("from MyUser");
}
}


    因为继承了JpaDaoSupport,索引需要获取一个JpaTemplate来实现访问数据库,在Spring的配置文件中要注入一个org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean。

[1] [2] 下一页

咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册