SpringData JPA的学习笔记之环境搭建

一、环境搭建

1、加入jar包

  spring jar+jpa jar +springData jar
>>SpringData jar包
 
 
2、配置applicationContext.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4 xmlns:context="http://www.springframework.org/schema/context"
 5 xmlns:tx="http://www.springframework.org/schema/tx"
 6 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
11  
12 <!-- 配置自动扫描的包 -->
13 <context:component-scan base-package="com.atguigu.springdata"></context:component-scan>
14  
15 <!-- 1. 配置数据源 -->
16 <context:property-placeholder location="classpath:db.properties"/>
17  
18 <bean id="dataSource"
19 class="com.mchange.v2.c3p0.ComboPooledDataSource">
20 <property name="user" value="${jdbc.user}"></property>
21 <property name="password" value="${jdbc.password}"></property>
22 <property name="driverClass" value="${jdbc.driverClass}"></property>
23 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
24  
25 <!-- 配置其他属性 -->
26 </bean>
27  
28 <!-- 2. 配置 JPA 的 EntityManagerFactory -->
29 <bean id="entityManagerFactory"
30 class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
31 <property name="dataSource" ref="dataSource"></property>
32 <property name="jpaVendorAdapter">
33 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
34 </property>
35 <property name="packagesToScan" value="com.atguigu.springdata"></property>
36 <property name="jpaProperties">
37 <props>
38 <!-- 二级缓存相关 -->
39 <!--
40 <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
41 <prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop>
42 -->
43 <!-- 生成的数据表的列的映射策略 -->
44 <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
45 <!-- hibernate 基本属性 -->
46 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
47 <prop key="hibernate.show_sql">true</prop>
48 <prop key="hibernate.format_sql">true</prop>
49 <prop key="hibernate.hbm2ddl.auto">update</prop>
50 </props>
51 </property>
52 </bean>
53  
54 <!-- 3. 配置事务管理器 -->
55 <bean id="transactionManager"
56 class="org.springframework.orm.jpa.JpaTransactionManager">
57 <property name="entityManagerFactory" ref="entityManagerFactory"></property>
58 </bean>
59  
60 <!-- 4. 配置支持注解的事务 -->
61 <tx:annotation-driven transaction-manager="transactionManager"/>
62  
63 <!-- 5. 配置 SpringData -->
64 <!-- 加入 jpa 的命名空间 -->
65 <!-- base-package: 扫描 Repository Bean 所在的 package -->
66 <jpa:repositories base-package="com.atguigu.springdata"
67 entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
68  
69 </beans>

 

 
//db.properties
1 jdbc.user=root
2 jdbc.password=123456
3 jdbc.jdbcUrl=jdbc:mysql:///jpa
4 jdbc.driverClass=com.mysql.jdbc.Driver

 

3、创建实体类Person
package com.atguigu.springdata;
 
import java.util.Date;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name="t_person")
@Entity
public class Person {
 
private Integer id;
private String lastname;
private String email;
private Date birthday;
@GeneratedValue
@Id
public 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 getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Person [id=" + id + ", lastname=" + lastname + ", email=" + email + ", birthday=" + birthday + "]";
}
 
 
}

  

4、继承Repository接口
Repository<Class<T>clazz,Object obj>,其中Class<T>clazz是实体类,obj是主键类型
package com.atguigu.springdata.dao;
 
import org.springframework.data.repository.Repository;
 
import com.atguigu.springdata.Person;
 
public interface PersonRespository extends Repository<Person,Integer>{
 
Person getByLastname(String lastname);
 
}

  

>>Repository接口说明:
  • Repository 接口是 Spring Data 的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法 
  • public interface Repository<T, ID extends Serializable> { } 
  • Spring Data可以让我们只定义接口,只要遵循 Spring Data的规范,就无需写实现类。  
  • 与继承 Repository 等价的一种方式,就是在持久层接口上使用 @RepositoryDefinition 注解,并为其指定 domainClass 和 idClass 属性。如下两种方式是完全等价的
 1 package com.atguigu.springdata.dao;
 2  
 3 import org.springframework.data.repository.RepositoryDefinition;
 4  
 5 import com.atguigu.springdata.Person;
 6  
 7 @RepositoryDefinition(domainClass=Person.class,idClass=Integer.class)
 8 public interface PersonRepository2 {
 9 Person getByLastname(String lastname);
10 }
11  

 

 
>>Repository的子接口
     基础的 Repository 提供了最基本的数据访问功能,其几个子接口则扩展了一些功能。它们的继承关系如下: 
  • Repository: 仅仅是一个标识,表明任何继承它的均为仓库接口类
  • CrudRepository: 继承 Repository,实现了一组 CRUD 相关的方法 
  • PagingAndSortingRepository: 继承 CrudRepository,实现了一组分页排序相关的方法 
  • JpaRepository: 继承 PagingAndSortingRepository,实现一组 JPA 规范相关的方法 
  • 自定义的 XxxxRepository 需要继承 JpaRepository,这样的 XxxxRepository 接口就具备了通用的数据访问控制层的能力。
  • JpaSpecificationExecutor: 不属于Repository体系,实现一组 JPA Criteria 查询相关的方法 
 
posted @ 2015-11-03 19:09  hzhh123  阅读(469)  评论(0编辑  收藏  举报