3. 数据访问层采用JPA
1) jar准备
Hibernate EntityManager
下载页面:
http://hibernate.org/30.html
下载链接:
http://sourceforge.net/project/showfiles.php?group_id=40712&package_id=156160
下载的文件hibernate-entitymanager-3.4.0.GA.zip放在D:\JavaTools下
解压后将
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\hibernate-entitymanager.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\javassist.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\ejb3-persistence.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\hibernate-annotations.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\hibernate-commons-annotations.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\hibernate-core.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\ slf4j-api.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\test\slf4j-log4j12.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\test\commons-collections.jar
复制到/WEB-INF/lib
然后给各个文件改名,加上版本号,即javassist-3.4.jar
ejb3-persistence-1.0.2GA.jar
hibernate-annotations-3.4.0GA.jar
hibernate-commons-annotations-3.1.0GA.jar
hibernate-core-3.3.0SP1.jar
hibernate-entitymanager-3.4.0GA.jar
版本号可以在jar文件中的META-INF\MANIFEST.MF文件中找到。然后将同名的版本较低的jar删除,选中项目名称,按F5刷新,这时可能会有错误提示,因为旧的jar被删除了,这时需要重新添加jar,这一操作过程以后不再重复说明。
2)Class文件修改
2.1)在Package Explorer树形列表中选中Member.java文件,点右键,Refactor-->Move…-->Create Package…,包名com.albertsong.bitrac.model,Finish,OK。
通过以上步骤,我们已经使用Eclipse的重构功能移动了Member.java的位置。
2.2)在Java Build Path中加入ejb3-persistence-1.0.2GA.jar,编译使用了JPA的类时要用到。
2.3)修改后的Member.java
- package com . albertsong . bitrac . model ;
- import java . util . Date ;
- import javax . persistence . Entity ;
- import javax . persistence . GeneratedValue ;
- import javax . persistence . Id ;
- import javax . persistence . Table ;
- @ Entity
- @ Table ( name = " BT_Member " )
- public class Member {
- @ Id
- @ GeneratedValue
- private int id ;
- private short grade ;
- private String username ;
- private String password ;
- private String validate ;
- private boolean conserve ;
- private String contact ;
- private String picture ;
- private String explain ;
- private int numPost ;
- private int numComm ;
- private int numFile ;
- private Date publish ;
- private String address ;
- public int getId () {
- return id ;
- }
- public void setId ( int id ) {
- this . id = id ;
- }
- public short getGrade () {
- return grade ;
- }
- public void setGrade ( short grade ) {
- this . grade = grade ;
- }
- public String getUsername () {
- return username ;
- }
- public void setUsername ( String username ) {
- this . username = username ;
- }
- public String getPassword () {
- return password ;
- }
- public void setPassword ( String password ) {
- this . password = password ;
- }
- public String getValidate () {
- return validate ;
- }
- public void setValidate ( String validate ) {
- this . validate = validate ;
- }
- public boolean isConserve () {
- return conserve ;
- }
- public void setConserve ( boolean conserve ) {
- this . conserve = conserve ;
- }
- public String getContact () {
- return contact ;
- }
- public void setContact ( String contact ) {
- this . contact = contact ;
- }
- public String getPicture () {
- return picture ;
- }
- public void setPicture ( String picture ) {
- this . picture = picture ;
- }
- public String getExplain () {
- return explain ;
- }
- public void setExplain ( String explain ) {
- this . explain = explain ;
- }
- public int getNumPost () {
- return numPost ;
- }
- public void setNumPost ( int numPost ) {
- this . numPost = numPost ;
- }
- public int getNumComm () {
- return numComm ;
- }
- public void setNumComm ( int numComm ) {
- this . numComm = numComm ;
- }
- public int getNumFile () {
- return numFile ;
- }
- public void setNumFile ( int numFile ) {
- this . numFile = numFile ;
- }
- public Date getPublish () {
- return publish ;
- }
- public void setPublish ( Date publish ) {
- this . publish = publish ;
- }
- public String getAddress () {
- return address ;
- }
- public void setAddress ( String address ) {
- this . address = address ;
- }
- }
2.4)修改后的MemberDaoImpl.java
- package com . albertsong . bitrac . dao ;
- import java . util . List ;
- import javax . persistence . EntityManager ;
- import javax . persistence . PersistenceContext ;
- import javax . persistence . Query ;
- import org . apache . commons . logging . Log ;
- import org . apache . commons . logging . LogFactory ;
- import org . springframework . transaction . annotation . Transactional ;
- @ Transactional
- public class MemberDaoImpl implements MemberDao {
- private Log log = LogFactory . getLog ( MemberDaoImpl . class ) ;
- private EntityManager em ;
- @ PersistenceContext
- public void setEntityManager ( EntityManager em ) {
- this . em = em ;
- }
- private EntityManager getEntityManager (){
- return em ;
- }
- @ SuppressWarnings ( " unchecked " )
- @ Override
- public boolean exist ( String username , String password ) {
- StringBuffer buffer = new StringBuffer ( " select m from Member m " ) ;
- buffer . append ( " where m.username=' " ) . append ( username ) . append ( " ' " ) ;
- buffer . append ( " and m.password=MD5(' " ) . append ( password ) . append ( " ') " ) ;
- log . debug ( buffer . toString ()) ;
- Query query = getEntityManager () . createQuery ( buffer . toString ()) ;
- List ls = query . getResultList () ;
- return ls . size () == 1 ? true : false ;
- }
- }