`
lixucheng
  • 浏览: 80287 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

如何把JDBC方式的代码修改JPA方式的代码

    博客分类:
  • JPA
阅读更多
欢迎来到李绪成的一亩三分地,有用的拿走,没有用的告诉我。
本文介绍如何把JDBC应用改成JPA应用。
JDBC应用改成JPA程序,需要把原来通过JDBC API访问数据库的代码替换成使用JPA代码。
JDBC访问数据库的主要工作包括:
n得到JDBC驱动程序;
n使用DriverManagerConnectionStatementResultSet等;
而使用JPA完成数据的操作包括:
n得到JDBC驱动程序;
n得到持久性提供者相关类库和配置文件;
n提供实体类;
n使用PersistenceEntityManagerFactoryEntity等接口。
要完成修改,需要完成如下工作:
n提供配置文件persistence.xml文件和持久性提供者相关类库;
n需要把原来表示信息的普通JavaBean修改实体类;
nJDBC代码修改为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;
publicclass Bookimplements 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() {
returnthis.bid;
}
publicvoid setBid(String bid) {
this.bid = bid;
}
public String getBname() {
returnthis.bname;
}
publicvoid setBname(String bname) {
this.bname = bname;
}
public Float getPrice() {
returnthis.price;
}
publicvoid setPrice(Float price) {
this.price = price;
}
public String getAuthor() {
returnthis.author;
}
publicvoid setAuthor(String author) {
this.author = author;
}
public String getPress() {
returnthis.press;
}
publicvoid 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;
/**
*Bookentity.@authorMyEclipsePersistenceTools
*/
@Entity
@Table(name="book"
,catalog="entitytest"
)
publicclass Bookimplements java.io.Serializable {
// Fields
private String bid;
private String bname;
private Float price;
private String author;
private String press;
// Constructors
/**defaultconstructor*/
public Book() {
}
/**minimalconstructor*/
public Book(String bid, String bname) {
this.bid = bid;
this.bname = bname;
}
/**fullconstructor*/
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() {
returnthis.bid;
}
publicvoid setBid(String bid) {
this.bid = bid;
}
@Column(name="BNAME", nullable=false, length=30)
public String getBname() {
returnthis.bname;
}
publicvoid setBname(String bname) {
this.bname = bname;
}
@Column(name="PRICE", precision=12, scale=0)
public Float getPrice() {
returnthis.price;
}
publicvoid setPrice(Float price) {
this.price = price;
}
@Column(name="AUTHOR", length=30)
public String getAuthor() {
returnthis.author;
}
publicvoid setAuthor(String author) {
this.author = author;
}
@Column(name="PRESS", length=30)
public String getPress() {
returnthis.press;
}
publicvoid setPress(String press) {
this.press = press;
}
}
3、访问代码修改如下
修改前的代码:
// 使用JDBC
publicvoid 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
publicvoidCouri
分享到:
评论

相关推荐

    JPA深度刘宝宝剖析版第一讲

    JPA 的 API:用来操作实体对象,执行CRUD操作,框架在后台完成所有的事情,开发者从繁琐的 JDBC和 SQL代码中解脱出来。 查询语言(JPQL):这是持久化操作中很重要的一个方面,通过面向对象而非面向数据库的查询...

    code-generator:通用jdbc代码生成器,使用自定义免费标记模板为每个使用关系型数据库项目生成通用代码

    通用JDBC代码生成器通过xml配置文件和用户自定义的freemarker模板生成代码,支持自定义参数适用于任何使用java连接关系型数据库的项目,既是mybatis,ibatis,Hibernate或jpa,mybatis-puls等,无论何时开头,每个...

    基于springboot的web项目最佳实践+源代码+文档说明

    # 基于springboot的web项目最佳实践 ...3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

    一个基于SpringBoot的微服务脚手架+源代码+文档说明

    - 持久层框架使用Spring Data JPA/JDBC - 数据源默认使用HikariCP - 使用SpringFox生成API文档 - 提供Swagger-UI可测试API界面 - 提供独立的HTML静态文档生成模块 - logback分别打印-info与-error双日志 - ...

    基于SpringBoot+Hibernate+Shiro的库存管理系统+源代码+文档说明

    ### 背景 一民营企业专业从事灯具照明...3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

    ProSpringProject:经过Pro Spring 4th Edition修改后的源代码-Source code modification

    经过Pro Spring 4th Edition修改后的源代码: : 章节 ch2:入门 ch3:在Spring介绍IoC和DI ch4:详细的Spring配置 ch5:介绍Spring AOP ch6:Spring JDBC支持 ch7:在Spring中使用Hibernate ch8:使用JPA2在...

    权限管理系统:使用SpringBootWebFlux + Shiro + JPA + JavaScala,实现基于数据库细粒度动态权限管理系统

    SpringBoot数据Jpa 阿帕克史郎 React堆 高速缓存 雷迪斯 自由标记 功能 控制器初步 服务初步 Dao依赖于JDBC和ORM,暂未更改成初始 去除Druid和多数据源 TODO链式调用,尽量去掉显示调用subscribe (分支rm-...

    Spring-generator一键生成数据库文件

    Spring-generator 是基于 javafx8 开发的图形界面 Spring 代码生成器,使用 Apache FreeMarker 作为代码文件的模板,用户可以一键将数据库中的表生成为任意风格的 .java 代码文件(比如经典的三层模型)。 Spring-...

    Spring面试题

    1. 对JDBC访问数据库的代码做了封装,大大简化了数据访问层繁琐的重复性代码。 2. Hibernate是一个基于JDBC的主流持久化框架,是一个优秀的ORM实现。他很大程度的简化DAO层的编码工作 3. hibernate使用Java反射...

    支持多数据库的ORM框架ef-orm.zip

    事实上JPA的几种数据查询方式存在青黄不接的问题。选择查询语言xxQL,项目面临后续维护困难,跨数据库移植性差;选择Criteria API,代码臃肿,操作繁琐,很多人望而却步。EF的设计思想是使人早日摆脱拼装SQL/HQL/...

    Java SpringBoot课件+源码视频教程

    61、_SpringBoot_数据访问-JDBC&自动配置原理 62、_SpringBoot_数据访问-整合Druid&配置数据源监控 63、_SpringBoot_数据访问-整合MyBatis(一)-基础环境搭建 64、_SpringBoot_数据访问-整合MyBatis(二)-注解...

    springboot学习思维笔记.xmind

    spring-boot-starter-data-jpa spring-boot-starter-data-mongodb spring-boot-starter-data-rest spring-boot-starter-data-solr spring-boot-starter-freemarker spring-boot-...

    Hibernate实战(第2版 中文高清版)

     1.3.2 用SQL/JDBC手工编写持久层   1.3.3 使用序列化   1.3.4 面向对象的数据库系统   1.3.5 其他选项   1.4 ORM   1.4.1 什么是ORM   1.4.2 一般的ORM问题   1.4.3 为什么选择ORM   1.4.4 ...

    ExtJS开发的BLOG程序

    1、直接下载的war文件放到Tomcat的webapps...3、如果是下载的源代码,要在Eclipse里面继续开发,则直接在命令行切换到源码目录,先执行mvneclipse:eclpse,然后在Eclipse直接把该工程导入Eclipse即可,详细可以参考:...

Global site tag (gtag.js) - Google Analytics