MyEclipse8.5下配置JPA (Without Springframework)

JPA即Java Persistence API。和Hibernate类似,属于数据库中间件(DM),实现了对象-关系映射(ORM)。具备很多优势,据说是以后的主流趋势。今天在MyEclipse8.5下配置了一下,没有利用Spring,实现了在最基本的Java工程下的数据库插入,删除,修改和查询。

好了,给出大概步骤和代码:

1,Myeclipse8.5新建一个Java工程,这里命名为JpaTest。然后再左侧Package Explorer里面JpaTest工程名上鼠标右键 ->MyEclipse ->Add JPA Capabilities,弹出如下对话框:


Persistence Provider选择默认的Toplink,其他也都选择默认的就好了,点击Next,进入下图对话框:





Persistence unit name默认为JpaTestPU,Driver一项由于MyEclipse和SQL Server2008先前已经做过设置,下拉列表就有com.microsoft.sqlserver.jdbc.SQLServerDriver。Catalog/Schema选择数据库名,这里还是选我的那个TestDB。Finish后,会发现src/META-INF目录下自动生成了一个persistence.xml。

最后我的persistence.xml的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="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">

<persistence-unit name="JpaTestPU" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>

<class>domain.Test_Account</class> <!-- 自己加的持久化类 -->

<properties>
<property name = "toplink.jdbc.driver" value = "com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name = "toplink.jdbc.url" value = "jdbc:sqlserver://localhost:1433;DatabaseName=TestDB"/>
<property name = "toplink.jdbc.user" value = "sa"/>
<property name = "toplink.jdbc.password" value = "1234"/>

<!-- 这里还可以设置数据库连接池参数 -->
<!-- 具体可以参考http://zhousbin3355.blog.sohu.com/115173955.html-->
</properties>
</persistence-unit>

</persistence>

新建持久化类(对应于表test_account),数据访问类和测试类,目录结构如下图:




Test_Account.java是对应于表test_account的持久化类,Test_AccountDAO.java是数据访问类,包maintest下的Main.java是包含主函数的测试类。

持久化类Test_Account.java的代码如下:
import .......;

@Entity
@Table(name="test_account")
public class Test_Account implements Serializable {

@Id
private String account; //主键
private String password;
private double balance;

//这里的3个属性对应于表test_account的3列


public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}

数据访问类Test_AccountDAO.java代码如下(具体javax.persistence. EntityManager的API用法参考http://lia.deis.unibo.it/Courses/TecnologieWeb0708/materiale/laboratorio/guide/j5eeapi/javax/persistence/EntityManager.html和http://lia.deis.unibo.it/Courses/TecnologieWeb0708/materiale/laboratorio/guide/j5eeapi/javax/persistence/Query.html):
import ...........;

public class Test_AccountDAO {

private EntityManager em;
private EntityManagerFactory emf;

public Test_AccountDAO(){
emf=Persistence.createEntityManagerFactory("JpaTestPU");
em=emf.createEntityManager();
}
public void save(Test_Account bean){ //插入
em.getTransaction().begin();
em.persist(bean);
em.getTransaction().commit();
}
public void update(Test_Account been) { //修改
em.getTransaction().begin();
em.merge(been);
em.getTransaction().commit();
}
public void remove(Test_Account been){ //删除
em.getTransaction().begin();
em.remove(been);
em.getTransaction().commit();
}
public Test_Account findByKey(String account) { //按主键查询
Test_Account tmpp=em.find(Test_Account.class, account);
return tmpp;
}
public List getAllItems()
{
List list=em.createQuery("select c from Test_Account c").getResultList();//Jpa查询语句
if(list==null||list.size()==0)return null;
return list;
}
public Test_Account findByAccount(String account)
{
Object temp=em.createQuery("select c from Test_Account c where c.account="+account).getSingleResult();
if(temp==null)return null;
return (Test_Account)temp;
}
}

测试类Main.java代码如下:

import ........;

public class Main {


public static void main(String[] args) {

Test_AccountDAO dao=new Test_AccountDAO();

Test_Account temp=new Test_Account();
temp.setAccount("abcdef");
temp.setPassword("555551");
temp.setBalance(1245.32);
dao.save(temp); //插入

Test_Account dedaoAccount=dao.findByKey("33268");
dedaoAccount.setPassword("hhhhhhhh");
dao.update(dedaoAccount); //更新

Test_Account fordelete=dao.findByKey("232323dd");
dao.remove(fordelete); //删除


List result=dao.getAllItems();
if(result!=null){
for(int i=0;i<result.size();i++)
{
Test_Account tee=(Test_Account)result.get(i);
System.out.println(tee.getAccount()+" "+tee.getPassword()+" "+tee.getBalance());
}
}

Test_Account tee=dao.findByAccount("110110");
System.out.println(tee.getAccount()+" "+tee.getPassword()+" "+tee.getBalance());

System.out.println("success...................");
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章