package com.ytkj.entity; import javax.persistence.*; import java.io.Serializable; /** * @Entity * 作用:指定當前類是實體類。 * @Table * 作用:指定實體類和表之間的對應關系。 * 屬性: * name:指定數據庫表的名稱 * @Id * 作用:指定當前字段是主鍵。 * @GeneratedValue * 作用:指定主鍵的生成方式。。 * 屬性: * strategy :指定主鍵生成策略。 * @Column * 作用:指定實體類屬性和數據庫表之間的對應關系 * 屬性: * name:指定數據庫表的列名稱。 * unique:是否唯一 * nullable:是否可以為空 * inserttable:是否可以插入 * updateable:是否可以更新 * columnDefinition: 定義建表時創建此列的DDL * secondaryTable: 從表名。如果此列不建在主表上(默認建在主表),該屬性定義該列所在從表的名字搭建開發環境[重點] * * 客戶實體類 * 配置映射關系 * 實體類和表映射 * 實體類屬性和表字段映射 */ @Entity @Table(name = "cst_customer") public class Customer implements Serializable { /** * 聲明主鍵配置 */ @Id /** * 配置主鍵的生成策略 */ @GeneratedValue(strategy = GenerationType.IDENTITY) /** * 指定實體類屬性和數據庫表之間的對應關系 */ @Column(name ="cust_id") private Long custId;//客戶主鍵 @Column(name = "cust_name") private String custName;//客戶名稱 @Column(name ="cust_source" ) private String custSource;//客戶來源 @Column(name = "cust_industry") private String custIndustry;//客戶行業 @Column(name ="cust_level") private String custLevel;//客戶級別 @Column(name ="cust_address") private String custAddress;//客戶地址 @Column(name = "cust_phone") private String custPhone;//客戶電話 public Long getCustId() { return custId; } public void setCustId(Long custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } 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 getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } @Override public String toString() { return "Customer{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custIndustry='" + custIndustry + '\'' + ", custLevel='" + custLevel + '\'' + ", custAddress='" + custAddress + '\'' + ", custPhone='" + custPhone + '\'' + '}'; } }
package com.ytkj.dao; import com.ytkj.entity.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.util.List; /** * JpaRepository<實體類類型,主鍵類型>:用來完成基本CRUD操作 * JpaSpecificationExecutor<實體類類型>:用於復雜查詢(分頁等查詢操作) */ public interface CustomerDao2 extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> { /** * 使用sql查詢 * nativeQuery = true:使用sql查詢 * nativeQuery = false:使用jpql查詢,默認就是false */ @Query(value = "select * from cst_customer",nativeQuery = true) List<Customer> findAll(); /** * 使用sql條件查詢 * 占位符 * 方法參數順序盡量和占位符位置一樣 * nativeQuery = true:使用sql查詢 * nativeQuery = false:使用jpql查詢,默認就是false */ @Query(value = "select * from cst_customer where cust_name=? ",nativeQuery = true) Customer findByName(String name); }
import com.ytkj.dao.CustomerDao2; import com.ytkj.entity.Customer; import jdk.nashorn.internal.parser.Lexer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class)//聲明spring提供的單元測試環境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 public class SpringdatajpaSqlTest { @Autowired CustomerDao2 customerDao2; @Test public void findAll(){ List<Customer> list = customerDao2.findAll(); for (int i=0;i<list.size();i++){ Customer customer = list.get(i); System.out.println(customer); } } @Test public void findByName(){ Customer customer = customerDao2.findByName("中國人"); System.out.println(customer); } }