天天看點

JPA資料操作彙總,常用的資料操作方法都在這了

前言

寫部落格總結,最近公司進新人,寫了個内部文檔順便整理了一下jap的資料操作demo

正文

第一種方式:

根據客戶名稱查詢客戶,使用jpql的形式查詢,配置jpql語句,使用的@Query注解

@Query(value="from Customer where custName = ?1")
    public Customer findJpql(String custName);      

第二種方式:

使用sql的形式查詢,

Query:配置sql查詢

value:sql語句
nativeQuery:查詢方式
  true:sql查詢
  false:jpql查詢      
@Query(value="select * from cst_customer where cust_name like ?1",nativeQuery = true)
    public List<Object [] > findSql(String name);      

第三種方式:

方法名的約定:
findBy: 查詢
  對象種的屬性名稱(首字母大寫): 查詢條件
  例如:資料庫中的字段 cust_name  方法名:findByCustName
  預設情況: 預設使用的是等于的方式查詢      
列舉兩種種特殊情況:
1.findBy  + 屬性名稱 + “查詢方式(Like | isnull)”
方法名:findByCustNameLike
2.多條件查詢
  findBy + 屬性名 + “查詢方式”   + “多條件的連接配接符(and|or)”  + 屬性名 + “查詢方式”      

第四種方式:

使用字元串進行拼接的方式,一定要注意引用的EntityManager的類

package com.demo.dao.imp;

import com.demo.dao.IFindByStr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.math.BigInteger;
import java.util.List;

@Service
public class FindByStr implements IFindByStr {
    @Autowired
    private EntityManager entityManager;
    @Override
    public List findStr(){
        //查詢語句string拼接結果
        StringBuffer sql = new StringBuffer("SELECT * " +"FROM cst_customer " +"where 1=1 ");
        //轉換資料格式
        String sqlStr = sql.toString();
        //字元串傳入到Query中
        javax.persistence.Query query = entityManager.createNativeQuery(sqlStr);
        //查詢資料庫
        List list = query.getResultList();
        return list;
    }
}      

下邊就是貼的代碼了

JPA資料操作彙總,常用的資料操作方法都在這了

整體的結構是這樣的,兩個接口,一個實體類,一個實作類

package com.demo.dao;

import java.util.List;


public interface IFindByStr {
    //字元串拼接sql
    public List findStr();
}      
package com.demo.dao;

import com.demo.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.math.BigInteger;
import java.util.List;

public interface CustomerDao extends JpaRepository<Customer,Long> ,JpaSpecificationExecutor<Customer> {

    @Query(value="from Customer where custName = ?1")
    public Customer findJpql(String custName);


    /**
     *  sql  :update cst_customer set cust_name = ? where cust_id = ?
     *  jpql : update Customer set custName = ? where custId = ?
     */
    @Query(value = " update Customer set custName = ?2 where custId = ?1 ")
    @Modifying
    public void updateCustomer(long custId, String custName);

    @Query(value="select * from cst_customer where cust_name like ?1",nativeQuery = true)
    public List<Object [] > findSql(String name);


    public Customer findByCustName(String custName);

    public List<Customer> findByCustNameLike(String custName);

    //使用客戶名稱模糊比對和客戶所屬行業精準比對的查詢
    public Customer findByCustNameLikeAndCustIndustry(String custName, String custIndustry);



}      
package com.demo.domain;

import javax.persistence.*;

@Entity
@Table(name="cst_customer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="cust_id")
    private Long custId;
    @Column(name="cust_address")
    private String custAddress;
    @Column(name="cust_industry")
    private String custIndustry;
    @Column(name="cust_level")
    private String custLevel;
    @Column(name="cust_name")
    private String custName;
    @Column(name="cust_phone")
    private String custPhone;
    @Column(name="cust_source")
    private String custSource;

    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }

    public String getCustAddress() {
        return custAddress;
    }

    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }

    public String getCustIndustry() {
        return custIndustry;
    }

    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }

    public String getCustLevel() {
        return custLevel;
    }

    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }

    public String getCustSource() {
        return custSource;
    }

    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "custId=" + custId +
                ", custAddress='" + custAddress + '\'' +
                ", custIndustry='" + custIndustry + '\'' +
                ", custLevel='" + custLevel + '\'' +
                ", custName='" + custName + '\'' +
                ", custPhone='" + custPhone + '\'' +
                ", custSource='" + custSource + '\'' +
                '}';
    }
}      

結束