springdata笔记记录

来源:互联网 发布:无限制搜索工具mac 编辑:程序博客网 时间:2024/06/07 12:31
<?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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置自动扫描的包 --><context:component-scan base-package="com.atguigu.springdata"></context:component-scan><!-- 1. 配置数据源 --><context:property-placeholder location="classpath:db.properties"/><bean id="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><!-- 配置其他属性 --></bean><!-- 2. 配置 JPA 的 EntityManagerFactory --><bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="jpaVendorAdapter"><bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean></property><property name="packagesToScan" value="com.atguigu.springdata"></property><property name="jpaProperties"><props><!-- 二级缓存相关 --><!--  <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop><prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop>--><!-- 生成的数据表的列的映射策略 --><prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop><!-- hibernate 基本属性 --><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"></property></bean><!-- 4. 配置支持注解的事务 --><tx:annotation-driven transaction-manager="transactionManager"/><!-- 5. 配置 SpringData --><!-- 加入  jpa 的命名空间 --><!-- base-package: 扫描 Repository Bean 所在的 package --><jpa:repositories base-package="com.atguigu.springdata"entity-manager-factory-ref="entityManagerFactory"></jpa:repositories></beans>



db.properties

jdbc.user=rootjdbc.password=1230jdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc:mysql:///jpa

Person.java

package com.atguigu.springdata;import java.util.Date;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.Table;@Table(name="JPA_PERSONS")@Entitypublic class Person {private Integer id;private String lastName;private String email;private Date birth;private Address address;private Integer addressId;@GeneratedValue@Idpublic Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}@Column(name="ADD_ID")public Integer getAddressId() {return addressId;}public void setAddressId(Integer addressId) {this.addressId = addressId;}@JoinColumn(name="ADDRESS_ID")@ManyToOnepublic Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}@Overridepublic String toString() {return "Person [id=" + id + ", lastName=" + lastName + ", email="+ email + ", brith=" + birth + "]";}}

PersonService.java

package com.atguigu.springdata;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Servicepublic class PersonService {@Autowiredprivate PersonRepsotory personRepsotory;@Transactionalpublic void savePersons(List<Person> persons){personRepsotory.save(persons);}@Transactionalpublic void updatePersonEmail(String email, Integer id){personRepsotory.updatePersonEmail(id, email);}}


PersonRepsotory.java

package com.atguigu.springdata;import java.util.Date;import java.util.List;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 org.springframework.data.repository.query.Param;/** * 1. Repository 是一个空接口. 即是一个标记接口 * 2. 若我们定义的接口继承了 Repository, 则该接口会被 IOC 容器识别为一个 Repository Bean. * 纳入到 IOC 容器中. 进而可以在该接口中定义满足一定规范的方法.  *  * 3. 实际上, 也可以通过 @RepositoryDefinition 注解来替代继承 Repository 接口 *//** * 在 Repository 子接口中声明方法 * 1. 不是随便声明的. 而需要符合一定的规范 * 2. 查询方法以 find | read | get 开头 * 3. 涉及条件查询时,条件的属性用条件关键字连接 * 4. 要注意的是:条件属性以首字母大写。 * 5. 支持属性的级联查询. 若当前类有符合条件的属性, 则优先使用, 而不使用级联属性.  * 若需要使用级联属性, 则属性之间使用 _ 进行连接.  *///@RepositoryDefinition(domainClass=Person.class,idClass=Integer.class)public interface PersonRepsotory extends JpaRepository<Person, Integer>,JpaSpecificationExecutor<Person>, PersonDao{//根据 lastName 来获取对应的 PersonPerson getByLastName(String lastName);//WHERE lastName LIKE ?% AND id < ?List<Person> getByLastNameStartingWithAndIdLessThan(String lastName, Integer id);//WHERE lastName LIKE %? AND id < ?List<Person> getByLastNameEndingWithAndIdLessThan(String lastName, Integer id);//WHERE email IN (?, ?, ?) OR birth < ?List<Person> getByEmailInAndBirthLessThan(List<String> emails, Date birth);//WHERE a.id > ?List<Person> getByAddress_IdGreaterThan(Integer id);//查询 id 值最大的那个 Person//使用 @Query 注解可以自定义 JPQL 语句以实现更灵活的查询@Query("SELECT p FROM Person p WHERE p.id = (SELECT max(p2.id) FROM Person p2)")Person getMaxIdPerson();//为 @Query 注解传递参数的方式1: 使用占位符. @Query("SELECT p FROM Person p WHERE p.lastName = ?1 AND p.email = ?2")List<Person> testQueryAnnotationParams1(String lastName, String email);//为 @Query 注解传递参数的方式1: 命名参数的方式. @Query("SELECT p FROM Person p WHERE p.lastName = :lastName AND p.email = :email")List<Person> testQueryAnnotationParams2(@Param("email") String email, @Param("lastName") String lastName);//SpringData 允许在占位符上添加 %%. @Query("SELECT p FROM Person p WHERE p.lastName LIKE %?1% OR p.email LIKE %?2%")List<Person> testQueryAnnotationLikeParam(String lastName, String email);//SpringData 允许在占位符上添加 %%. @Query("SELECT p FROM Person p WHERE p.lastName LIKE %:lastName% OR p.email LIKE %:email%")List<Person> testQueryAnnotationLikeParam2(@Param("email") String email, @Param("lastName") String lastName);//设置 nativeQuery=true 即可以使用原生的 SQL 查询@Query(value="SELECT count(id) FROM jpa_persons", nativeQuery=true)long getTotalCount();//可以通过自定义的 JPQL 完成 UPDATE 和 DELETE 操作. 注意: JPQL 不支持使用 INSERT//在 @Query 注解中编写 JPQL 语句, 但必须使用 @Modifying 进行修饰. 以通知 SpringData, 这是一个 UPDATE 或 DELETE 操作//UPDATE 或 DELETE 操作需要使用事务, 此时需要定义 Service 层. 在 Service 层的方法上添加事务操作. //默认情况下, SpringData 的每个方法上有事务, 但都是一个只读事务. 他们不能完成修改操作!@Modifying@Query("UPDATE Person p SET p.email = :email WHERE id = :id")void updatePersonEmail(@Param("id") Integer id, @Param("email") String email);} 

Address.java

package com.atguigu.springdata;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;@Table(name="JPA_ADDRESSES")@Entitypublic class Address {private Integer id;private String province;private String city;@GeneratedValue@Idpublic Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getProvince() {return province;}public void setProvince(String province) {this.province = province;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}}

下面的这两个类是自己实现 Repository接口,注意实现类的命名,必须以Impl结尾,否则无法识别

PersonDao.java

package com.atguigu.springdata;public interface PersonDao {void test();}

PersonRepsotoryImpl.java

package com.atguigu.springdata;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;public class PersonRepsotoryImpl implements PersonDao {@PersistenceContextprivate EntityManager entityManager;@Overridepublic void test() {Person person = entityManager.find(Person.class, 11);System.out.println("-->" + person);}}

----------------------------------------------------------


test.java

package com.atguigu.springdata.test;import java.io.FileNotFoundException;import java.io.IOException;import java.sql.SQLException;import java.util.ArrayList;import java.util.Arrays;import java.util.Date;import java.util.List;import javax.persistence.criteria.CriteriaBuilder;import javax.persistence.criteria.CriteriaQuery;import javax.persistence.criteria.Path;import javax.persistence.criteria.Predicate;import javax.persistence.criteria.Root;import javax.sql.DataSource;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Sort;import org.springframework.data.domain.Sort.Direction;import org.springframework.data.domain.Sort.Order;import org.springframework.data.jpa.domain.Specification;import com.atguigu.springdata.Person;import com.atguigu.springdata.PersonRepsotory;import com.atguigu.springdata.PersonService;import com.atguigu.springdata.commonrepositorymethod.AddressRepository;public class SpringDataTest {private ApplicationContext ctx = null;private PersonRepsotory personRepsotory = null;private PersonService personService;{ctx = new ClassPathXmlApplicationContext("applicationContext.xml");personRepsotory = ctx.getBean(PersonRepsotory.class);personService = ctx.getBean(PersonService.class);}@Testpublic void testCommonCustomRepositoryMethod(){ApplicationContext ctx2 = new ClassPathXmlApplicationContext("classpath:com/atguigu/springdata/commonrepositorymethod/applicationContext2.xml");AddressRepository addressRepository = ctx2.getBean(AddressRepository.class);addressRepository.method();}@Testpublic void testCustomRepositoryMethod(){personRepsotory.test();}/** * 目标: 实现带查询条件的分页. id > 5 的条件 *  * 调用 JpaSpecificationExecutor 的 Page<T> findAll(Specification<T> spec, Pageable pageable); * Specification: 封装了 JPA Criteria 查询的查询条件 * Pageable: 封装了请求分页的信息: 例如 pageNo, pageSize, Sort */@Testpublic void testJpaSpecificationExecutor(){int pageNo = 3 - 1;int pageSize = 5;PageRequest pageable = new PageRequest(pageNo, pageSize);//通常使用 Specification 的匿名内部类Specification<Person> specification = new Specification<Person>() {/** * @param *root: 代表查询的实体类.  * @param query: 可以从中可到 Root 对象, 即告知 JPA Criteria 查询要查询哪一个实体类. 还可以 * 来添加查询条件, 还可以结合 EntityManager 对象得到最终查询的 TypedQuery 对象.  * @param *cb: CriteriaBuilder 对象. 用于创建 Criteria 相关对象的工厂. 当然可以从中获取到 Predicate 对象 * @return: *Predicate 类型, 代表一个查询条件.  */@Overridepublic Predicate toPredicate(Root<Person> root,CriteriaQuery<?> query, CriteriaBuilder cb) {Path path = root.get("id");Predicate predicate = cb.gt(path, 5);return predicate;}};Page<Person> page = personRepsotory.findAll(specification, pageable);System.out.println("总记录数: " + page.getTotalElements());System.out.println("当前第几页: " + (page.getNumber() + 1));System.out.println("总页数: " + page.getTotalPages());System.out.println("当前页面的 List: " + page.getContent());System.out.println("当前页面的记录数: " + page.getNumberOfElements());}@Testpublic void testJpaRepository(){Person person = new Person();person.setBirth(new Date());person.setEmail("xy@atguigu.com");person.setLastName("xyz");person.setId(28);Person person2 = personRepsotory.saveAndFlush(person);System.out.println(person == person2);}@Testpublic void testPagingAndSortingRespository(){//pageNo 从 0 开始. int pageNo = 6 - 1;int pageSize = 5;//Pageable 接口通常使用的其 PageRequest 实现类. 其中封装了需要分页的信息//排序相关的. Sort 封装了排序的信息//Order 是具体针对于某一个属性进行升序还是降序. Order order1 = new Order(Direction.DESC, "id");Order order2 = new Order(Direction.ASC, "email");Sort sort = new Sort(order1, order2);PageRequest pageable = new PageRequest(pageNo, pageSize, sort);Page<Person> page = personRepsotory.findAll(pageable);System.out.println("总记录数: " + page.getTotalElements());System.out.println("当前第几页: " + (page.getNumber() + 1));System.out.println("总页数: " + page.getTotalPages());System.out.println("当前页面的 List: " + page.getContent());System.out.println("当前页面的记录数: " + page.getNumberOfElements());}@Testpublic void testCrudReposiory(){List<Person> persons = new ArrayList<>();for(int i = 'a'; i <= 'z'; i++){Person person = new Person();person.setAddressId(i + 1);person.setBirth(new Date());person.setEmail((char)i + "" + (char)i + "@atguigu.com");person.setLastName((char)i + "" + (char)i);persons.add(person);}personService.savePersons(persons);}@Testpublic void testModifying(){//personRepsotory.updatePersonEmail(1, "mmmm@atguigu.com");personService.updatePersonEmail("mmmm@atguigu.com", 1);}@Testpublic void testNativeQuery(){long count = personRepsotory.getTotalCount();System.out.println(count);}@Testpublic void testQueryAnnotationLikeParam(){//List<Person> persons = personRepsotory.testQueryAnnotationLikeParam("%A%", "%bb%");//System.out.println(persons.size());//List<Person> persons = personRepsotory.testQueryAnnotationLikeParam("A", "bb");//System.out.println(persons.size());List<Person> persons = personRepsotory.testQueryAnnotationLikeParam2("bb", "A");System.out.println(persons.size());}@Testpublic void testQueryAnnotationParams2(){List<Person> persons = personRepsotory.testQueryAnnotationParams2("aa@atguigu.com", "AA");System.out.println(persons);}@Testpublic void testQueryAnnotationParams1(){List<Person> persons = personRepsotory.testQueryAnnotationParams1("AA", "aa@atguigu.com");System.out.println(persons);}@Testpublic void testQueryAnnotation(){Person person = personRepsotory.getMaxIdPerson();System.out.println(person);}@Testpublic void testKeyWords2(){List<Person> persons = personRepsotory.getByAddress_IdGreaterThan(1);System.out.println(persons);}@Testpublic void testKeyWords(){List<Person> persons = personRepsotory.getByLastNameStartingWithAndIdLessThan("X", 10);System.out.println(persons);persons = personRepsotory.getByLastNameEndingWithAndIdLessThan("X", 10);System.out.println(persons);persons = personRepsotory.getByEmailInAndBirthLessThan(Arrays.asList("AA@atguigu.com", "FF@atguigu.com", "SS@atguigu.com"), new Date());System.out.println(persons.size());}@Testpublic void testHelloWorldSpringData() throws FileNotFoundException, IOException, InstantiationException, IllegalAccessException{System.out.println(personRepsotory.getClass().getName());Person person = personRepsotory.getByLastName("AA");System.out.println(person);}@Testpublic void testJpa(){}@Testpublic void testDataSource() throws SQLException {DataSource dataSource = ctx.getBean(DataSource.class);System.out.println(dataSource.getConnection());}}