مشخصات مقاله
-
571
-
0.0
-
3443
-
0
-
0
آموزش Spring with JPA- Java Spring
اسپرینگ با JPA
آموزش Spring Data JPA
API اسپرینگ Data JPA ، کلاس JpaTemplate را برای ادغام برنامه های کاربردی اسپرینگ با JPA ارائه کرده است. JPA (Java Persistent API) ویژگی sun برای حفظ اشیا در برنامه های کاربردی سازمانی است. در حال حاظر به عنوان جایگزین برای beanهای پیچیده entity (complex entity beans) مورد استفاده قرار می گیرد. پیاده سازی ویژگی JPA توسط فروشندگان زیادی از جمله hibernate، Toplink،iBatis، OpenJPA و غیره ارائه شده است.
مزیت قالب spring JPA
نیاز به نوشتن کد قبل و بعد برای حفظ، به روز رسانی، حذف یا جستجوی شی مانند ایجاد نمونه Persistence ، ایجاد نمونه EntityManagerFactory ، ایجاد نمونه EntityTransaction، ایجاد نمونه EntityManager، انجام نمونه EntityTransaction و بستن EntityManager ندارید. در نتیجه در حجم زیادی از کد صرفه جویی می شود. در این مثال، برای پیاده سازی JPA از hibernate استفاده می کنیم.
مثال ادغام اسپرینگ و JPA
مراحل ساده ادغام برنامه کاربردی اسپرینگ و JPA را با هم می بینیم:
1- ایجاد فایل Account.java
2- ایجاد فایل Account.xml
3- ایجاد فایل AccountDao.java
4- ایجاد فایل persistence.xml
5- ایجاد فایل applicationContext.xml
6- ایجاد فایل AccountsClient.java
در این مثال، برنامه کاربردی hibernate را با اسپرینگ ادغام می کنیم. ساختار دایرکتوری مثال JPA با اسپرینگ را می بینیم.
1. Account.java :
این یک کلاس ساده POJO است.
package com.javatpoint;
public class Account {
private int accountNumber;
private String owner;
private double balance;
//no-arg and parameterized constructor
//getters and setters
}
2. Account.xml :
این فایل نگاشت شامل تمام اطلاعات کلاس persistent است.
< entity-mappings version="1.0"
2. xmlns="http://java.sun.com/xml/ns/persistence/orm"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd ">
< entity class="com.javatpoint.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.javatpoint;
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"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. 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/javatpoint/Account.xml< /mapping-file>
< class>com.javatpoint.Account< /class>
< /persistence-unit>
< /persistence>
5. applicationContext.xml :
< ?xml version="1.0" encoding="UTF-8" ?>
< beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:tx="http://www.springframework.org/schema/tx"
5. 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="oracle.jdbc.driver.OracleDriver">< /property>
< property name="url" value="jdbc:oracle:thin:@localhost:1521:xe">< /property>
< property name="username" value="system">< /property>
< property name="password" value="oracle">< /property>
< /bean>
< bean id="hbAdapterBean" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
< property name="showSql" value="true">< /property>
< property name="generateDdl" value="true">< /property>
< property name="databasePlatform" value="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.javatpoint.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 query را روی کنسول نشان می دهد.
6. Accountsclient.java :
package com.javatpoint;
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