欢迎来到李绪成的一亩三分地,有用的拿走,没有用的告诉我。
本文介绍如何把JDBC应用改成JPA应用。
把JDBC
应用改成JPA
程序,需要把原来通过JDBC API
访问数据库的代码替换成使用JPA
代码。
JDBC
访问数据库的主要工作包括:
n
得到JDBC
驱动程序;
n
使用DriverManager
,Connection
,Statement
,ResultSet
等;
而使用JPA
完成数据的操作包括:
n
得到JDBC
驱动程序;
n
得到持久性提供者相关类库和配置文件;
n
提供实体类;
n
使用Persistence
、EntityManagerFactory
和Entity
等接口。
要完成修改,需要完成如下工作:
n
提供配置文件persistence.xml
文件和持久性提供者相关类库;
n
需要把原来表示信息的普通JavaBean
修改实体类;
n
把JDBC
代码修改为JPA
代码。
下面以图书添加为例介绍。
1
、提供配置文件persistence.xml
文件和持久性提供者相关类库
可以参考JP
两个相关实验中的过程,添加JPA
支持,可以为工程添加类库,并且会生成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="bookstore_PU"
transaction-type="RESOURCE_LOCAL">
<
provider
>
oracle.toplink.essentials.PersistenceProvider
</
provider
>
<
class
>
beans.Book
</
class
>
<
properties
>
<
property
name="toplink.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<
property
name="toplink.jdbc.url"
value="jdbc:mysql://localhost:3306/entitytest" />
<
property
name="toplink.jdbc.user" value="root" />
<
property
name="toplink.jdbc.password" value="root" />
</
properties
>
</
persistence-unit
>
</
persistence
>
2
、把原来的JavaBean
修改为实体类
可以使用MyEclipse
中的功能,参考JPA
的两个相关实验。
修改前JavaBean
的代码如下:
package
beans;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.Id;
import
javax.persistence.Table;
public
class
Book
implements
java.io.Serializable {
private
String
bid
;
private
String
bname
;
private
Float
price
;
private
String
author
;
private
String
press
;
public
Book() {
}
public
Book(String bid, String bname) {
this
.
bid
= bid;
this
.
bname
= bname;
}
public
Book(String bid, String bname, Float price, String author, String press) {
this
.
bid
= bid;
this
.
bname
= bname;
this
.
price
= price;
this
.
author
= author;
this
.
press
= press;
}
public
String getBid() {
return
this
.
bid
;
}
public
void
setBid(String bid) {
this
.
bid
= bid;
}
public
String getBname() {
return
this
.
bname
;
}
public
void
setBname(String bname) {
this
.
bname
= bname;
}
public
Float getPrice() {
return
this
.
price
;
}
public
void
setPrice(Float price) {
this
.
price
= price;
}
public
String getAuthor() {
return
this
.
author
;
}
public
void
setAuthor(String author) {
this
.
author
= author;
}
public
String getPress() {
return
this
.
press
;
}
public
void
setPress(String press) {
this
.
press
= press;
}
}
修改的内容包括:
n
在类上使用@Entity
声明Bean
类为实体类;
n
在类上使用@Table
声明与数据库中表的对应关系;
n
在主键属性上(
或者get
方法上)
使用@Id
标注主键属性;
n
在属性上使用@Column
标注属性与表中列的对应关系;
修改后JavaBean
的代码如下:
package
beans;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.Id;
import
javax.persistence.Table;
/**
*
Book
entity.
@author
MyEclipse
Persistence
Tools
*/
@Entity
@Table(name="book"
,catalog="entitytest"
)
public
class
Book
implements
java.io.Serializable {
// Fields
private
String
bid
;
private
String
bname
;
private
Float
price
;
private
String
author
;
private
String
press
;
// Constructors
/**
default
constructor
*/
public
Book() {
}
/**
minimal
constructor
*/
public
Book(String bid, String bname) {
this
.
bid
= bid;
this
.
bname
= bname;
}
/**
full
constructor
*/
public
Book(String bid, String bname, Float price, String author, String press) {
this
.
bid
= bid;
this
.
bname
= bname;
this
.
price
= price;
this
.
author
= author;
this
.
press
= press;
}
// Property accessors
@Id
@Column(name="BID", unique=true, nullable=false, length=13)
public
String getBid() {
return
this
.
bid
;
}
public
void
setBid(String bid) {
this
.
bid
= bid;
}
@Column(name="BNAME", nullable=false, length=30)
public
String getBname() {
return
this
.
bname
;
}
public
void
setBname(String bname) {
this
.
bname
= bname;
}
@Column(name="PRICE", precision=12, scale=0)
public
Float getPrice() {
return
this
.
price
;
}
public
void
setPrice(Float price) {
this
.
price
= price;
}
@Column(name="AUTHOR", length=30)
public
String getAuthor() {
return
this
.
author
;
}
public
void
setAuthor(String author) {
this
.
author
= author;
}
@Column(name="PRESS", length=30)
public
String getPress() {
return
this
.
press
;
}
public
void
setPress(String press) {
this
.
press
= press;
}
}
3
、访问代码修改如下
修改前的代码:
//
使用
JDBC
public
void
addBook(String bookid, String bookname, String author,
float
price,String press) {
Connection con =
null
;
PreparedStatement stmt =
null
;
try
{
//
指出连接数据库所需要的驱动程序
Class.forName(
"oracle.jdbc.driver.OracleDriver"
);
//
建立与数据库之间的连接
con = DriverManager.getConnection(
"jdbc:oracle:thin:@myserver:1521:mydb"
,
"scott"
,
"tiger"
);
//
编写查询数据库信息的
SQL
语句
String sql =
"insert into book values(?,?,?,?,?)"
;
//
创建语句对象,用于执行
SQL
语句
stmt = con.prepareStatement(sql);
stmt.setString(1, bookid);
stmt.setString(2, bookname);
stmt.setFloat(3, price);
stmt.setString(4, author);
stmt.setString(5, press);
//
执行
SQL
语句得到结果集
stmt.executeUpdate();
}
catch
(Exception e) {
System.
out
.println(e.getMessage());
}
finally
{
//
关闭相关对象
if
(stmt !=
null
)
try
{
stmt.close();
}
catch
(Exception ee) {
}
if
(con !=
null
)
try
{
con.close();
}
catch
(Exception ee) {
}
}
}
修改后的代码:
//
使用
JPA
public
void
addBookJPA(String bookid, String bookname, String author,
float
price,String press) {
//
创建图书对象
Book book =
new
Book();
book.setBid(bookid);
book.setBname(bookname);
book.setAuthor(author);
book.setPress(press);
book.setPrice(price);
//
使用
JPA
完成操作
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"bookstore_PU"
);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(book);
em.getTransaction().commit();
em.close();
emf.close();
}