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...................");
}

}

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