Spring Data JPA API提供了JpaTemplate类以将spring应用程序与JPA集成。
JPA(Java持久性API)是用于在企业应用程序中持久存储对象的sun规范。当前, 它用作复杂实体bean的替代品。
许多供应商提供了JPA规范的实现, 例如:
- Hibernate
- Toplink
- iBatis
- OpenJPA等
因此, 它节省了大量代码。
在此示例中, 我们将使用Hibernate模式来实现JPA。
Spring和JPA集成的示例
让我们看一下将Spring应用程序与JPA集成的简单步骤:
- 创建Account.java文件
- 创建Account.xml文件
- 创建AccountDao.java文件
- 创建persistence.xml文件
- 创建applicationContext.xml文件
- 创建AccountsClient.java文件
文章图片
1) Account.java
这是一个简单的POJO类。
package com.srcmini;
public class Account {
private int accountNumber;
private String owner;
private double balance;
//no-arg and parameterized constructor
//getters and setters
}
2)Account.xml
该映射文件包含持久类的所有信息。
<
entity-mappings version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd ">
<
entity class="com.srcmini.Account">
<
table name="account100">
<
/table>
<
attributes>
<
id name="accountNumber">
<
column name="accountnumber"/>
<
/id>
<
basic name="owner">
<
column name="owner"/>
<
/basic>
<
basic name="balance">
<
column name="balance"/>
<
/basic>
<
/attributes>
<
/entity>
<
/entity-mappings>
3) AccountDao.java
package com.srcmini;
import java.util.List;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class AccountsDao{
JpaTemplate template;
public void setTemplate(JpaTemplate template) {
this.template = template;
}
public void createAccount(int accountNumber, String owner, double balance){
Account account = new Account(accountNumber, owner, balance);
template.persist(account);
}
public void updateBalance(int accountNumber, double newBalance){
Account account = template.find(Account.class, accountNumber);
if(account != null){
account.setBalance(newBalance);
}
template.merge(account);
}
public void deleteAccount(int accountNumber){
Account account = template.find(Account.class, accountNumber);
if(account != null)
template.remove(account);
}
public List<
Account>
getAllAccounts(){
List<
Account>
accounts =template.find("select acc from Account acc");
return accounts;
}
}
4)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="ForAccountsDB">
<
mapping-file>
com/srcmini/Account.xml<
/mapping-file>
<
class>
com.srcmini.Account<
/class>
<
/persistence-unit>
<
/persistence>
5)applicationContext.xml
<
?xml version="1.0" encoding="UTF-8"?>
<
beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<
tx:annotation-driven transaction-manager="jpaTxnManagerBean" proxy-target-class="true"/>
<
bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<
property name="driverClassName" value="http://www.srcmini.com/oracle.jdbc.driver.OracleDriver">
<
/property>
<
property name="url" value="http://www.srcmini.com/jdbc:oracle:thin:@localhost:1521:xe">
<
/property>
<
property name="username" value="http://www.srcmini.com/system">
<
/property>
<
property name="password" value="http://www.srcmini.com/oracle">
<
/property>
<
/bean>
<
bean id="hbAdapterBean" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<
property name="showSql" value="http://www.srcmini.com/true">
<
/property>
<
property name="generateDdl" value="http://www.srcmini.com/true">
<
/property>
<
property name="databasePlatform" value="http://www.srcmini.com/org.hibernate.dialect.OracleDialect">
<
/property>
<
/bean>
<
bean id="emfBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<
property name="dataSource" ref="dataSourceBean">
<
/property>
<
property name="jpaVendorAdapter" ref="hbAdapterBean">
<
/property>
<
/bean>
<
bean id="jpaTemplateBean" class="org.springframework.orm.jpa.JpaTemplate">
<
property name="entityManagerFactory" ref="emfBean">
<
/property>
<
/bean>
<
bean id="accountsDaoBean" class="com.srcmini.AccountsDao">
<
property name="template" ref="jpaTemplateBean">
<
/property>
<
/bean>
<
bean id="jpaTxnManagerBean" class="org.springframework.orm.jpa.JpaTransactionManager">
<
property name="entityManagerFactory" ref="emfBean">
<
/property>
<
/bean>
<
/beans>
generateDdl属性将自动创建表。
showSql属性将在控制台上显示sql查询。
6) Accountsclient.java
package com.srcmini;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class AccountsClient{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountsDao accountsDao = context.getBean("accountsDaoBean", AccountsDao.class);
accountsDao.createAccount(15, "Jai Kumar", 41000);
accountsDao.createAccount(20, "Rishi ", 35000);
System.out.println("Accounts created");
//accountsDao.updateBalance(20, 50000);
//System.out.println("Account balance updated");
/*List<
Account>
accounts = accountsDao.getAllAccounts();
for (int i = 0;
i <
accounts.size();
i++) {
Account acc = accounts.get(i);
System.out.println(acc.getAccountNumber() + " : " + acc.getOwner() + " (" + acc.getBalance() + ")");
}*///accountsDao.deleteAccount(111);
//System.out.println("Account deleted");
}
}
输出
Hibernate: insert into account100 (balance, owner, accountnumber) values (?, ?, ?)
Hibernate: insert into account100 (balance, owner, accountnumber) values (?, ?, ?)
Accounts created
【Spring Data JPA教程入门介绍】下载此示例(使用Myeclipse IDE开发)
推荐阅读
- Spring MVC多视图页面示例图解
- Spring表达式语言(SPEL)使用教程介绍
- Spring SPEL运算符用法
- Spring使用ORM框架示例
- Spring SimpleJdbcTemplate示例详解
- 慕课网/安卓攻城狮视频学习及练习
- 慕课网/安卓攻城狮视频学习及练习——简陋计算器
- Android LinearLayout 自动换行
- Android 适配(drawable文件夹)图片适配