📜  Spring Data JPA教程

📅  最后修改于: 2020-12-04 08:08:06             🧑  作者: Mango

Spring Data JPA教程

Spring Data JPA API提供了JpaTemplate类,以将Spring应用程序与JPA集成。

JPA(Java持久性API)是用于在企业应用程序中持久存储对象的sun规范。当前,它用作复杂实体bean的替代品。

许多供应商提供了JPA规范的实现,例如:

  • 冬眠
  • 顶联
  • iBatis
  • OpenJPA等

Spring JpaTemplate的优势

您无需编写前后代码即可持久,更新,删除或搜索对象,例如创建持久性实例,创建EntityManagerFactory实例,创建EntityTransaction实例,创建EntityManager实例,提交EntityTransaction实例以及关闭EntityManager。

因此,它节省了大量代码

在此示例中,我们将使用休眠模式来实现JPA。

Spring和JPA集成的示例

让我们看看将Spring应用程序与JPA集成的简单步骤:

  • 创建Account.java文件
  • 创建Account.xml文件
  • 创建AccountDao.java文件
  • 创建persistence.xml文件
  • 创建applicationContext.xml文件
  • 创建AccountsClient.java文件

在此示例中,我们将把休眠应用程序与spring集成在一起。让我们看看带有Spring的jpa示例的目录结构。

这是一个简单的POJO类。

package com.javatpoint;

public class Account {
    private int accountNumber;
    private String owner;
    private double balance;
        //no-arg and parameterized constructor
        //getters and setters
}

该映射文件包含持久类的所有信息。


    
    
        
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 getAllAccounts(){
        List accounts =template.find("select acc from Account acc");
        return accounts;
    }
}


    
    
        com/javatpoint/Account.xml
        com.javatpoint.Account
    



    
      
    
     
        
        
        
        
    
          
       
         
         
         
     
    
    
         
         
     
     
     
          
      
      
      
          
      
       
        
    
          

generateDdl属性将自动创建表。

showSql属性将在控制台上显示sql查询。

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 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