Spring Data JPA完成客戶的基本CRUD操做java
使用Spring Data JPA,須要整合Spring與Spring Data JPA,而且須要提供JPA的服務提供者hibernate,因此須要導入spring相關座標,hibernate座標,數據庫驅動座標等mysql
<properties>web <spring.version>4.2.4.RELEASE</spring.version>spring <hibernate.version>5.0.7.Final</hibernate.version>sql <slf4j.version>1.6.6</slf4j.version>數據庫 <log4j.version>1.2.12</log4j.version>express <c3p0.version>0.9.1.2</c3p0.version>api <mysql.version>5.1.6</mysql.version>app </properties>框架
<dependencies> <!-- junit單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency>
<!-- spring beg --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency>
<!-- spring end -->
<!-- hibernate beg --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.1.Final</version> </dependency> <!-- hibernate end -->
<!-- c3p0 beg --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>${c3p0.version}</version> </dependency> <!-- c3p0 end -->
<!-- log end --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end -->
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency>
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.9.0.RELEASE</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.4.RELEASE</version> </dependency>
<!-- el beg 使用spring data jpa 必須引入 --> <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>2.2.4</version> </dependency>
<dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.el</artifactId> <version>2.2.4</version> </dependency> <!-- el end --> </dependencies>
|
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- 1.dataSource 配置數據庫鏈接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/jpa" /> <property name="user" value="root" /> <property name="password" value="111111" /> </bean>
<!-- 2.配置entityManagerFactory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="cn.itcast.entity" /> <property name="persistenceProvider"> <bean class="org.hibernate.jpa.HibernatePersistenceProvider" /> </property> <!--JPA的供應商適配器--> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="false" /> <property name="database" value="MYSQL" /> <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /> <property name="showSql" value="true" /> </bean> </property> <property name="jpaDialect"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> </property> </bean>
<!-- 3.事務管理器--> <!-- JPA事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean>
<!-- 整合spring data jpa--> <jpa:repositories base-package="cn.itcast.dao" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
<!-- 4.txAdvice--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="get*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice>
<!-- 5.aop--> <aop:config> <aop:pointcut id="pointcut" expression="execution(* cn.itcast.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /> </aop:config>
<context:component-scan base-package="cn.itcast"></context:component-scan>
<!--組裝其它 配置文件-->
</beans>
|
咱們使用昨天案例中的Customer實體類對象,已經配置好了映射關係
package cn.itcast.entity;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;
/** * * * 全部的註解都是使用JPA的規範提供的註解, * * 因此在導入註解包的時候,必定要導入javax.persistence下的 */ @Entity //聲明實體類 @Table(name="cst_customer") //創建實體類和表的映射關係 public class Customer {
@Id//聲明當前私有屬性爲主鍵 @GeneratedValue(strategy=GenerationType.IDENTITY) //配置主鍵的生成策略 @Column(name="cust_id") //指定和表中cust_id字段的映射關係 private Long custId;
@Column(name="cust_name") //指定和表中cust_name字段的映射關係 private String custName;
@Column(name="cust_source")//指定和表中cust_source字段的映射關係 private String custSource;
@Column(name="cust_industry")//指定和表中cust_industry字段的映射關係 private String custIndustry;
@Column(name="cust_level")//指定和表中cust_level字段的映射關係 private String custLevel;
@Column(name="cust_address")//指定和表中cust_address字段的映射關係 private String custAddress;
@Column(name="cust_phone")//指定和表中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; } }
|
Spring Data JPA是spring提供的一款對於數據訪問層(Dao層)的框架,使用Spring Data JPA,只須要按照框架的規範提供dao接口,不須要實現類就能夠完成數據庫的增刪改查、分頁查詢等方法的定義,極大的簡化了咱們的開發過程。
在Spring Data JPA中,對於定義符合規範的Dao層接口,咱們只須要遵循如下幾點就能夠了:
1.建立一個Dao層接口,並實現JpaRepository和JpaSpecificationExecutor
2.提供相應的泛型
package cn.itcast.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import cn.itcast.entity.Customer;
/** * JpaRepository<實體類類型,主鍵類型>:用來完成基本CRUD操做 * JpaSpecificationExecutor<實體類類型>:用於複雜查詢(分頁等查詢操做) */ public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> { } |
這樣咱們就定義好了一個符合Spring Data JPA規範的Dao層接口
完成了Spring Data JPA的環境搭建,而且編寫了符合Spring Data JPA 規範的Dao層接口以後,就可使用定義好的Dao層接口進行客戶的基本CRUD操做
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:applicationContext.xml") public class CustomerDaoTest {
@Autowired private CustomerDao customerDao;
/** * 保存客戶:調用save(obj)方法 */ @Test public void testSave() { Customer c = new Customer(); c.setCustName("傳智播客"); customerDao.save(c); }
/** * 修改客戶:調用save(obj)方法 * 對於save方法的解釋:若是執行此方法是對象中存在id屬性,即爲更新操做會先根據id查詢,再更新 * 若是執行此方法中對象中不存在id屬性,即爲保存操做 * */ @Test public void testUpdate() { //根據id查詢id爲1的客戶 Customer customer = customerDao.findOne(1l); //修改客戶名稱 customer.setCustName("傳智播客順義校區"); //更新 customerDao.save(customer); }
/** * 根據id刪除:調用delete(id)方法 */ @Test public void testDelete() { customerDao.delete(1l); }
/** * 根據id查詢:調用findOne(id)方法 */ @Test public void testFindById() { Customer customer = customerDao.findOne(2l); System.out.println(customer); } } |
在客戶的案例中,咱們發如今自定義的CustomerDao中,並無提供任何方法就可使用其中的不少方法,那麼這些方法到底是怎麼來的呢?答案很簡單,對於咱們自定義的Dao接口,因爲繼承了JpaRepository和JpaSpecificationExecutor,因此咱們可使用這兩個接口的全部方法。
在使用Spring Data JPA時,通常實現JpaRepository和JpaSpecificationExecutor接口,這樣就可使用這些接口中定義的方法,可是這些方法都只是一些聲明,沒有具體的實現方式,那麼在 Spring Data JPA中它又是怎麼實現的呢?
經過對客戶案例,以debug斷點調試的方式,經過分析Spring Data JPA的原來來分析程序的執行過程
咱們以findOne方法爲例進行分析
l 代理子類的實現過程
經過JDKDynamicAopProxy 建立動態代理對象
動態代理對象:simpleJpaRepository 實現了JapRepository 和 JpaSpecificationExecutor。對基本增刪改查進行了封裝。最終經過EntityManager完成增刪改查操做
斷點執行到方法上時,咱們能夠發現注入的customerDao對象,本質上是經過JdkDynamicAopProxy生成的一個代理對象
l 代理對象中方法調用的分析
當程序執行的時候,會經過JdkDynamicAopProxy的invoke方法,對customerDao對象生成動態代理對象。根據對Spring Data JPA介紹而知,要想進行findOne查詢方法,最終仍是會出現JPA規範的API完成操做,那麼這些底層代碼存在於何處呢?答案很簡單,都隱藏在經過JdkDynamicAopProxy生成的動態代理對象當中,而這個動態代理對象就是SimpleJpaRepository
經過SimpleJpaRepository的源碼分析,定位到了findOne方法,在此方法中,返回em.find()的返回結果,那麼em又是什麼呢?
帶着問題繼續查找em對象,咱們發現em就是EntityManager對象,而他是JPA原生的實現方式,因此咱們獲得結論Spring Data JPA只是對標準JPA操做進行了進一步封裝,簡化了Dao層代碼的開發
在繼承JpaRepository,和JpaRepository接口後,咱們就可使用接口中定義的方法進行查詢
l 繼承JpaRepository後的方法列表
delete
刪除
findAll
查詢全部
findOne
直接加載
getOne
底層是getRefresh()。是延遲加載的
save:
save方法保存的對象主鍵爲null,視爲保存。主鍵存在值,視爲修改
l 繼承JpaSpecificationExecutor的方法列表
繼承了以 Specification 爲參數的動態查詢方法
使用Spring Data JPA提供的查詢方法已經能夠解決大部分的應用場景,可是對於某些業務來講,咱們還須要靈活的構造查詢條件,這時就可使用@Query註解,結合JPQL的語句方式完成查詢
@Query 註解的使用很是簡單,只需在方法上面標註該註解,同時提供一個JPQL查詢語句便可
public interface CustomerDao extends JpaRepository<Customer, Long>,JpaSpecificationExecutor<Customer> { //@Query 使用jpql的方式查詢。 @Query(value="from Customer") public List<Customer> findAllCustomer();
//@Query 使用jpql的方式查詢。?1表明參數的佔位符,其中1對應方法中的參數索引 @Query(value="from Customer where custName = ?1") public Customer findCustomer(String custName); } |
此外,也能夠經過使用 @Query 來執行一個更新操做,爲此,咱們須要在使用 @Query 的同時,用 @Modifying 來將該操做標識爲修改查詢,這樣框架最終會生成一個更新的操做,而非查詢
@Query(value="update Customer set custName = ?1 where custId = ?2") @Modifying public void updateCustomer(String custName,Long custId); |
Spring Data JPA一樣也支持sql語句的查詢,以下:
/** * nativeQuery : 使用本地sql的方式查詢 */ @Query(value="select * from cst_customer",nativeQuery=true) public void findSql(); |
顧名思義,方法命名規則查詢就是根據方法的名字,就能建立查詢。只須要按照Spring Data JPA提供的方法命名規則定義方法的名稱,就能夠完成查詢工做。Spring Data JPA在程序執行的時候會根據方法名稱進行解析,並自動生成查詢語句進行查詢
按照Spring Data JPA 定義的規則,查詢方法以findBy開頭,涉及條件查詢時,條件的屬性用條件關鍵字鏈接,要注意的是:條件屬性首字母需大寫。框架在進行方法名解析時,會先把方法名多餘的前綴截取掉,而後對剩下部分進行解析。
//方法命名方式查詢(根據客戶名稱查詢客戶) public Customer findByCustName(String custName); |
具體的關鍵字,使用方法和生產成SQL以下表所示
Keyword |
Sample |
JPQL |
||
And |
findByLastnameAndFirstname |
… where x.lastname = ?1 and x.firstname = ?2 |
||
Or |
findByLastnameOrFirstname |
… where x.lastname = ?1 or x.firstname = ?2 |
||
Is,Equals |
findByFirstnameIs, findByFirstnameEquals |
… where x.firstname = ?1 |
||
Between |
findByStartDateBetween |
… where x.startDate between ?1 and ?2 |
||
LessThan |
findByAgeLessThan |
… where x.age < ?1 |
||
LessThanEqual |
findByAgeLessThanEqual |
… where x.age ⇐ ?1 |
||
GreaterThan |
findByAgeGreaterThan |
… where x.age > ?1 |
||
GreaterThanEqual |
findByAgeGreaterThanEqual |
… where x.age >= ?1 |
||
After |
findByStartDateAfter |
… where x.startDate > ?1 |
||
Before |
findByStartDateBefore |
… where x.startDate < ?1 |
||
IsNull |
findByAgeIsNull |
… where x.age is null |
||
IsNotNull,NotNull |
findByAge(Is)NotNull |
… where x.age not null |
||
Like |
findByFirstnameLike |
… where x.firstname like ?1 |
||
NotLike |
findByFirstnameNotLike |
… where x.firstname not like ?1 |
||
StartingWith |
findByFirstnameStartingWith |
… where x.firstname like ?1 (parameter bound with appended %) |
||
EndingWith |
findByFirstnameEndingWith |
… where x.firstname like ?1 (parameter bound with prepended %) |
||
Containing |
findByFirstnameContaining |
… where x.firstname like ?1 (parameter bound wrapped in %) |
||
OrderBy |
findByAgeOrderByLastnameDesc |
… where x.age = ?1 order by x.lastname desc |
||
Not |
findByLastnameNot |
… where x.lastname <> ?1 |
||
In |
findByAgeIn(Collection ages) |
… where x.age in ?1 |
||
NotIn |
findByAgeNotIn(Collection age) |
… where x.age not in ?1 |
||
TRUE |
findByActiveTrue() |
… where x.active = true |
||
FALSE |
findByActiveFalse() |
… where x.active = false |
||
IgnoreCase |
findByFirstnameIgnoreCase |
… where UPPER(x.firstame) = UPPER(?1) |