JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
1、 JPA的使用方法
本次使用的Swing + JPA 独立开发的数据库操作。使用的IDE是NetBeans。做的是一个单表的操作不涉及表的对应关系。
步骤:
① 点服务,右击数据库建立新连接。如下图
配置本连接:
点击完成即可;
② 在开发的工程对应的库文件中引入mysql驱动
③ 右击对应的功能包,通过向导、新建一个与单表结构相同的实体类
④ 同样,通过上面的步骤建立一个针对实体类的控制类
③和④都是通过向导自动生成的,很不错,生成的结构如下:
其中SupperLottoTable是实体类 。 SupperLottoTableJpaController是实体类控制类
⑤ 对上面生成的JPA操作,进行应用
package com.shinewaysoft.supperlotto.util;
import com.shinewaysoft.supperlotto.database.SupperLottoTable;
import com.shinewaysoft.supperlotto.database.SupperLottoTableJpaController;
import com.shinewaysoft.supperlotto.database.exceptions.NonexistentEntityException;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
/**
*
* @describe 使用了jxl功能包,用于读取要导入的Excel文件
* @author <guowenbin>
* @date (2012-1-13)
*
*/
public class ReadInXLS {
// public static void main(String[] args) throws IOException{
// ReadInXLS.getExcelFile("C:\\Documents and Settings\\Administrator\\桌面\\新建 Microsoft Excel 工作表.xls");
// }
public ReadInXLS(){}
/*
获得excel文件,并读取第一个sheet对应的内容
*/
public static boolean getExcelFile(String filePath) throws IOException, NonexistentEntityException, Exception{
try {
String oldChar = "\\";
String newChar = "\\\\";
filePath = filePath.replace(oldChar, newChar);
File file = new File(filePath);
if(file.exists()){
Workbook book = Workbook.getWorkbook(file);
// 获得第一个工作表对象
Sheet sheet = book.getSheet(0);
// 得到第一列第一行的单元格
int row = sheet.getRows();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("SupperLotto1.0PU");
SupperLottoTable supperLottoTable = new SupperLottoTable();
SupperLottoTableJpaController controller = new SupperLottoTableJpaController(emf);
Cell[] cell = null;
for(int i=1; i<row ; i++){
cell = sheet.getRow(i);
supperLottoTable.setIssue(Integer.parseInt(cell[0].getContents()));
supperLottoTable.setBefNumber1(Integer.parseInt(cell[1].getContents()));
supperLottoTable.setBefNumber2(Integer.parseInt(cell[2].getContents()));
supperLottoTable.setBefNumber3(Integer.parseInt(cell[3].getContents()));
supperLottoTable.setBefNumber4(Integer.parseInt(cell[4].getContents()));
supperLottoTable.setBefNumber5(Integer.parseInt(cell[5].getContents()));
supperLottoTable.setAftNumber1(Integer.parseInt(cell[6].getContents()));
supperLottoTable.setAftNumber2(Integer.parseInt(cell[7].getContents()));
controller.edit(supperLottoTable);
}
book.close();
} else {
System.out.println("没有找到文件");
return false;
}
} catch (BiffException ex) {
Logger.getLogger(ReadInXLS.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}
}
上面controller.edit(supperLottoTable); 对应的代码为:
public void edit(SupperLottoTable supperLottoTable) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
supperLottoTable = em.merge(supperLottoTable);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = supperLottoTable.getIssue();
if (findSupperLottoTable(id) == null) {
throw new NonexistentEntityException("The supperLottoTable with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
通过上面的例子,可以看出用JPA开发一个单表应用,方便多了。除此之外还有那些好处呢?
不断的通过调用 controller.edit(supperLottoTable); 发现。JPA会在后台做好多处理,这个表对应的主键是:Issue。而我在对同一个Excel文件进行导入的时候,JPA会自动判断,以抛弃已经导入过的数据。如果我修改对应的 Excel 文件,在做导入, JPA也会帮我们分析,并自动修改响应的数据。
由此可知 JPA 基本屏蔽了程序员直接与SQL打交道。开发更加敏捷快速。
对于复杂SQL的支持如何,期待我以后的获知。