基础学习笔记.
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>
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;
2.1. DAO 类.
可以继承的接口有:
CRUD 功能的接口:
CrudRepository : org.springframework.data.repository.CrudRepository<T, ID>
CRUD + 分页的接口:
PagingAndSortingRepository : org.springframework.data.repository.PagingAndSortingRepository<T, ID>
例如:
- public interface UserDao extends PagingAndSortingRepository<User, Long>
2.2 DAO 方法
已经定义了 UserDao 接口.不用实现里面的任何方法,spring 会帮你生成对应的代理类.也不用加 @Repository annotation,因为已经继承了 Repository 接口.
对于已有的方法就不做多的说明了,看了方法名就会用的.使用的时候在 Service 层注入对应的 UserDao 代理对象即可.
自定义方法.
例1.
根据命名规则写对应的抽象方法即可.根据 username 查找 user.只需在 UserDao 中 填写如下方法.
- public List<User> findByUsername(String username);
例2.
根据两个属性查询.
- public List<User> findByUsernameAndPassword(String username,String password);
附方法定义规则表:
http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/
Table 2.2. Supported keywords inside method names 部分.
例3.
锁:
可以使用 @Lock , 里面填入的是 JPA 的 LockModeType
*
- @Lock(LockModeType.PESSIMISTIC_READ)
- public List<User> findByUsername(String username);
对应的 sql 就是:
- select * from t_user where username=? lock in share mode
*
- @Lock(LockModeType.PESSIMISTIC_WRITE)
- public List<User> findByUsername(String username);
对应的 sql 就是:
- select * from t_user where username=? for update
例4.
分页
- public List<User> findByUsername(String username, Pageable pageable);
Pageable 是一个定义分页属性的类.可以创建 Pageable 的实现类 PageRequest对象使用.
- PageRequest pr = new PageRequest(int page, int size);
- PageRequest pr = new PageRequest(int page, int size, Sort sort);
- etc...
例5.
自己定义 query 语句.
@Query
- @Query("select u from User u where u.username= ?1")
- User findByUsername(String username);