也已经使用了一段时间,看什么都不如看官方文档,我这里也只是写个笔记记录一下,如果能帮助到其他人,很开心 .算是个 demoshow 吧.这里也只写了一些我觉得比较有用的地方.其他一些使用知识,请参见官方文档:http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/
1. Spring 配置简述.
- <jpa:repositories base-package="org.e.test.**.dao" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" />
- <tx:annotation-driven transaction-manager="transactionManager" />
- <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
- <property name="packagesToScan">
- <list>
- <value>org.e.test.**.model</value>
- </list>
- </property>
- <property name="jpaProperties">
- <props>
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
- <prop key="hibernate.format_sql">true</prop>
- .....etc
- </props>
- </property>
- </bean>
- <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
- <property name="databasePlatform" value="${hibernate.dialect}" />
- </bean>
- <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="entityManagerFactory" />
- </bean>
<jpa:repositories base-package="org.e.test.**.dao" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="packagesToScan">
<list>
<value>org.e.test.**.model</value>
</list>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">true</prop>
......etc
</props>
</property>
</bean>
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="${hibernate.dialect}" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
2.DAO
@Transactional 方面基本不变,该怎么用怎么用..
假设已有 Entity 类 User
- @Entity
- @Table(name = "t_user")
- public class User
- {
- @Id
- @Column(name = "user_id")
- @GeneratedValue(strategy = GenerationType.AUTO)
- private long userId;
- private String username;
- private String password;
@Entity
@Table(name = "t_user")
public class User
{
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long userId;
private String username;
private String password;
2.1. DAO 类.
可以继承的接口有:
CRUD 功能的接口:
CrudRepository : org.springframework.data.repository.CrudRepository<T, ID>
CRUD + 分页的接口:
PagingAndSortingRepository : org.springframework.data.repository.PagingAndSortingRepository<T, ID>
例如:
- public interface UserDaoextends PagingAndSortingRepository<User, Long>
public interface UserDao extends PagingAndSortingRepository<User, Long>
2.2 DAO 方法
已经定义了 UserDao 接口.不用实现里面的任何方法,spring 会帮你生成对应的代理类.也不用加 @Repository annotation,因为已经继承了 Repository 接口.
对于已有的方法就不做多的说明了,看了方法名就会用的.使用的时候在 Service 层注入对应的 UserDao 代理对象即可.