±à¼ÍƼö: |
ÎÄÕÂÖ÷Òª½éÉÜSpring
Data¼ò½é,ÈçºÎʹÓÃSpring Data,Spring DataÌṩµÄÏà¹Ø½Ó¿Ú,
Ï£Íû¶ÔÄúµÄѧϰÓÐËù°ïÖú¡£
±¾ÎÄÀ´×ÔÓÚĽ¿ÎÍø£¬ÓÉ»ðÁú¹ûÈí¼þDelores±à¼¡¢ÍƼö¡£ |
|
Ò»¡¢Spring Data¼ò½é
Spring Data¹ÙÍø£ºhttps://spring.io/projects/spring-data
Spring DataÊÇSpringµÄÒ»¸ö×ÓÏîÄ¿£¬ËüÊÇΪÁ˼ò»¯Êý¾Ý¿âµÄ²Ù×÷·ÃÎʶøÉúµÄ£¬Îª¿ª·¢ÕßÌṩÁËͳһµÄ£¬Ò»ÖÂÐԵIJÙ×÷Êý¾Ý¿âµÄ½Ó¿Ú¡£

Spring DataÓÖ°üº¬¶à¸ö×ÓÏîÄ¿£¬³£ÓõÄÓУº
Spring Date JPA£º Ä¿µÄÊÇΪÁ˼õÉÙÊý¾Ý²ãµÄ¿ª·¢Á¿£¬ÌṩÁ˲Ù×÷Êý¾Ý¿âµÄ½Ó¿Ú¡£
Spring Date MongoDB£ºÎª²Ù×÷MongoDBÊý¾Ý¿âÌṩÁ˽ӿÚÖ§³Ö£¬ÔÚ´óÊý¾Ý²ãÓõıȽ϶ࡣ
Spring Date Redis£ºÎª²Ù×÷RedisÊý¾Ý¿âÌṩÁËÏà¹ØµÄ½Ó¿ÚÖ§³Ö¡£
Spring Date Solr£ºÌṩ¶ÔSolrµÄ½Ó¿ÚÖ§³Ö¡£SolrÊÇÒ»¸ö¸ßÐÔÄÜ£¬¾ßÓÐËÑË÷¹¦ÄÜ £¬¶Ô²éѯÐÔÄÜÓÅ»¯µÄÒ»¸öÈ«ÎÄËÑË÷ÒýÇæ¡£
¶þ¡¢ÈçºÎʹÓÃSpring Data
mavenµØÖ·£ºhttps://mvnrepository.com/search?q=Spring+Data
ÔÚÏîÄ¿µÄpom.xmlÎļþÒýÈëÈçÏÂÒÀÀµ¼´¿É£º
1£©JPA:
<!-- https://mvnrepository.com/artifact
/org.springframework.data/spring-data-jpa-->
<dependency> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact /org.hibernate/hibernate-entitymanager
-->
<dependency> <groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager </artifactId>
<version>5.4.2.Final</version>
</dependency> |
£¨2£©MongoDB
<!-- https://mvnrepository.com/artifact /org.springframework.data/spring- data-mongodb
-->
<dependency> <groupId>org.springframework.data </groupId>
<artifactId>spring-data-mongodb </artifactId>
<version>2.1.10.RELEASE</version>
</dependency>
|
£¨3£©Redis
<!-- https://mvnrepository.com/artifact /org.springframework.data /spring-data-redis
-->
<dependency> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.10.RELEASE</version>
</dependency> |
Èý¡¢Spring DataÌṩµÄÏà¹Ø½Ó¿Ú
(1) Repository£º
Repository½Ó¿ÚÊÇSpring DataµÄºËÐĽӿڣ¬Ëü²»ÌṩÈκη½·¨£¬ÊÇÒ»¸ö¿Õ½Ó¿Ú¡£
ÎÒÃǶ¨ÒåµÄ½Ó¿Ú xxxRepository extends Repository£¬±íʾ´Ë½Ó¿ÚÄÉÈëspring¹ÜÀí£¬Ðè°´Ò»¶¨¹æÔò¶¨Òå·½·¨¡£
Èç¹ûÎÒÃDz»Ïë¼Ì³ÐRepository½Ó¿Ú£¬¿ÉÒÔͨ¹ý×¢½âÀ´ÊµÏÖͬÑùµÄ¹¦ÄÜ£¬ÈçÏ£º
@RepositoryDefinition(domainClass = ʵÌåÀàÃû.class,idClass=ʵÌåÀàµÄÖ÷¼ü·â×°ÀàÐÍ.class)
Repository½Ó¿ÚµÄÔ´ÂëÈçÏ£º
package org.springframework.
data.repository;
import org. springframework.stereotype.Indexed;
@Indexed
public interface Repository<T, ID>{} |


(2)CrudRepository£º
CrudRepository½Ó¿Ú¼Ì³ÐRepository£¬ÊµÏÖÁËCURDÏà¹ØµÄ·½·¨ ,¾ßÌåÈçͼ¡£

(3)JpaRepository
paRepository½Ó¿Ú¼Ì³ÐPagingAndSortingRepository£¬ÊµÏÖJPA¹æ·¶Ïà¹ØµÄ·½·¨ £¬¾ßÌåÈçÏ£º

(4)PagingAndSortingRepository
PagingAndSortingRepository½Ó¿Ú¼Ì³ÐCrudRepository,ʵÏÖÁË·Ö²¼ÅÅÐòÏà¹Ø·½·¨¡£
¸Ã½Ó¿Ú°üº¬·ÖÒ³ºÍÅÅÐò¹¦ÄÜ¡£
´øÅÅÐòµÄ²éѯ£ºfindAll(Sort sort)¡£
´øÅÅÐòµÄ·ÖÒ³²éѯ£ºfindAll(Pageable pageable)¡£
CRUDRepository ¼Ì³ÐRepository
PagingAndSortingRepository ¼Ì³ÐCRUDRepository
JpaRepository ¼Ì³ÐPagingAndSortingRepository
Òâζ×ÅÖ»Ðè¼Ì³Ð½Ó¿ÚJpaRepository, ±ãÓÐÁËÒÔÉÏËùÓнӿڵŦÄÜ ¡£
ËÄ¡¢ÊµÕ½Ó¦ÓÃ
1. ÏîÄ¿½á¹¹

2. pom.xmlÎļþÅäÖÃ
<dependencies>
<!-- https://mvnrepository.com/ artifact/mysql/mysql-connector-java
--> <dependency> <groupId>mysql</groupId>
<artifactId>mysql-connector-java </artifactId>
<version>8.0.11</version> </dependency>
<dependency> <groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version> </dependency>
<!-- https://mvnrepository.com/artifact /org.hamcrest/hamcrest-core
--> <dependency> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version> <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact /org.springframework/spring-jdbc
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact /org.springframework/spring-jdbc
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact /org.springframework.data/ spring-data-jpa
-->
<dependency>
<groupId>org.springframework.data </groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact
/org.hibernate/hibernate-entitymanager-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager </artifactId>
<version>5.4.2.Final</version>
</dependency>
</dependencies> |
3. db.propertiesÅäÖãº
#Êý¾Ý¿âÅäÖÃÐÅÏ¢
jdbc.driverClass=com.mysql.cj. jdbc.Driver
jdbc.url=jdbc:mysql:// 127.0.0.1:3306 /test?useSSL=false&serverTimezone= UTC&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=root |
4. beans.xmlÅäÖãº
<?xml version="1.0"
encoding="UTF-8"?>
<beans xmlns="http://www.spring framework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001 /XMLSchema-instance"
xmlns:context="http://www.spring framework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema /beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema /context/spring-context-4.3.xsd">
<context:property-placeholder location="db.properties"/>
<bean id="dataSource" class="org.springframework.jdbc. datasource.DriverManagerDataSource">
<!-- <property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="url" value="jdbc:mysql:///test"/>-->
<property name="driverClassName"
value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework. jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="studentDao" class="com.lhf.spring.dao. StudentDAOSpringJdbcImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans> |
5. beans-new.xmlÅäÖãº
<?xml version="1.0"
encoding="UTF-8"?>
<beans xmlns="http://www.spring framework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001 /XMLSchema-instance"
xmlns:context="http://www.spring framework.org/schema/context"
xmlns:tx="http://www.spring framework.org/schema/tx"
xmlns:jpa="http://www.spring framework.org/schema/data/jpa"
xsi:schemaLocation="http://www.spring framework.org/schema/beans
http:// www.springframework.org/schema/ beans/spring-beans.xsd
http://www.springframework.org/ schema/data/jpa
http://www.springframework.org/ schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/ schema/tx http://www.springframework.org /schema/tx/spring-tx-4.0.xsd
http://www.springframework. org/schema/context
http://www.springframework.org/schema /context/spring-context-4.0.xsd">
<context:property-placeholder location="db.properties"/>
<!--1 ÅäÖÃÊý¾ÝÔ´-->
<bean id="dataSource" class="org.springframework.jdbc. datasource.DriverManagerDataSource">
<!--<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="url" value="jdbc: mysql:///spring_data"/>-->
<property name="driverClassName"
value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--2 ÅäÖÃEntityManagerFactory-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa. LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm. jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan"
value="com.lhf.spring"/>
<!--ÅäÖÃjpaÊôÐÔ-->
<property name="jpaProperties">
<props>
<prop key="hibernate.ejb.naming_strategy"> org.hibernate.cfg.ImprovedNamingStrategy </prop>
<prop key="hibernate.dialect">org.hibernate. dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto"> update</prop>
<!--×Ô¶¯´´½¨±í-->
</props>
</property>
</bean>
<!--3 ÅäÖÃÊÂÎñ¹ÜÀíÆ÷-->
<bean id="transactionManager" class="org.springframework.orm.jpa. JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory"/>
</bean>
<!--4 ÅäÖÃÖ§³Ö×¢½âµÄÊÂÎñ-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--5 ÅäÖÃspring data-->
<jpa:repositories base-package ="com.lhf.spring"
entity-manager-factory-ref="entityManagerFactory"/>
<context:component-scan base-package="com.lhf.spring"/>
</beans> |
6. EmployeeRepository.java½Ó¿Ú
package com.lhf.spring.repository;
import com.lhf.spring.domain.Employee;
import org.springframework.data. jpa.repository.Modifying;
import org.springframework.data. jpa.repository.Query;
import org.springframework.data. repository.Repository;
import org.springframework.data. repository.RepositoryDefinition;
import org.springframework.data. repository.query.Param;
import java.util.List;
/**
* @ClassName: EmployeeRepository
* @Description:
* @Author: liuhefei
* @Date: 2019/8/11
* @blog: https://www.imooc.com /u/1323320/articles
**/
@RepositoryDefinition(domainClass = Employee.class,
idClass = Integer.class)
public interface EmployeeRepository { //extends
Repository<Employee,Integer>{
public Employee findByName(String name);
/**
* ²éѯÒÔname¿ªÍ·£¬Ð¡ÓÚageµÄÊý¾Ý
* @param name
* @param age
* @return
*/
public List<Employee> findByNameStartingWithAndAgeLessThan (String
name, Integer age);
/**
* ²éѯÒÔname½áβ£¬Ð¡ÓÚageµÄÊý¾Ý
* @param name
* @param age
* @return
*/
public List<Employee> findByNameEndingWithAndAgeLessThan (String
name, Integer age);
/**
* ²éѯ¶à¸öname»òСÓÚageµÄÊý¾Ý
* @param name
* @param age
* @return
*/
public List<Employee> findByNameInOrAgeLessThan (List<String>
name, Integer age);
/**
* ²éѯ¶à¸öname²¢ÇÒСÓÚageµÄÊý¾Ý
* @param name
* @param age
* @return
*/
public List<Employee> findByNameInAndAgeLessThan (List<String>
name, Integer age);
/**
* ²éѯid×î´óµÄÊý¾Ý
* @return
*/
@Query("select o from Employee o where
id=(select max(id) from Employee t1)")
public Employee getEmployeeByMaxId();
/**
* ¸ù¾ÝnameºÍage²éѯËùÓÐÆ¥ÅäµÄÊý¾Ý
* @param name
* @param age
* @return
*/
@Query("select o from Employee o where
o.name=?1 and o.age=?2")
public List<Employee> queryParams1(String
name, Integer age);
/**
* ¸ù¾Ýname»òage²éѯËùÓÐÆ¥ÅäµÄÊý¾Ý
* @param name
* @param age
* @return
*/
@Query("select o from Employee o where
o.name=:name or o.age=:age")
public List<Employee> queryParams2(@Param("name")
String name, @Param("age") Integer
age);
/**
* ¸ù¾Ýname½øÐÐlike²éѯ
* @param name
* @return
*/
@Query("select o from Employee o where
o.name like %?1%")
public List<Employee> queryLike1(String
name);
@Query("select o from Employee o where
o.name like %:name%")
public List<Employee> queryLike2 (@Param("name")String
name);
/**
* ²éѯ×ܵļǼÌõÊý
* @return
*/
@Query("select count(*) from Employee")
public long getCount();
@Query(nativeQuery = true, value = "select
count(1) from Employee")
public long getCount1();
/**
* ¸ù¾Ýid¸üÐÂÄêÁä
* @param id
* @param age
*/
@Modifying
@Query("update Employee o set o.age = :age
where o.id = :id")
public void update(@Param("id") Integer
id, @Param("age")Integer age);
} |
7. EmployeeCrudRepository.java½Ó¿Ú£º
package com.lhf.spring.repository;
import com.lhf.spring.domain.Employee;
import org.springframework. data.repository.CrudRepository;
/**
* @ClassName: EmployeeCrudRepository
* @Description:
* @Author: liuhefei
* @Date: 2019/8/11
* @blog: https://www.imooc.com /u/1323320/articles
**/
public interface EmployeeCrud Repository extends
CrudRepository <Employee, Integer> {
} |
8. EmployeeJpaRepository.java½Ó¿Ú
package com.lhf.spring.repository;
import com.lhf.spring.domain.Employee;
import org.springframework.data. jpa.repository.JpaRepository;
/**
* @ClassName: EmployeeJpaRepository
* @Description:
* @Author: liuhefei
* @Date: 2019/8/12
* @blog: https://www.imooc.com /u/1323320/articles
**/
public interface EmployeeJpa Repository extends
JpaRepository <Employee, Integer> {
} |
9. EmployeePagingAndSortingRespsitory.java½Ó¿Ú
package com.lhf.spring.repository;
import com.lhf.spring.domain.Employee;
import org.springframework.data. repository. PagingAndSortingRepository;
/**
* @ClassName: EmployeePagingAnd SortingRespsitory
* @Description: PagingAndSorting RepositoryÖ§³Ö·ÖÒ³ºÍÅÅÐò
* @Author: liuhefei
* @Date: 2019/8/11
* @blog: https://www.imooc.com /u/1323320/articles
**/
public interface EmployeePaging AndSortingRespsitory
extends PagingAndSortingRepository<Employee,
Integer> {
} |
10. EmployeeJpaSpecificationExecutorRepository.java½Ó¿Ú
package com.lhf.spring.repository;
import com.lhf.spring.domain.Employee;
import org.springframework.data.jpa. repository.JpaRepository;
import org.springframework.data.jpa. repository.JpaSpecificationExecutor;
/**
* @ClassName: EmployeeJpaSpecification ExecutorRepository
* @Description:
* @Author: liuhefei
* @Date: 2019/8/12
* @blog: https://www.imooc.com/ u/1323320/articles
**/
public interface Employee JpaSpecificationExecutorRepository
extends JpaRepository<Employee, Integer>,
JpaSpecificationExecutor<Employee> {
} |
|