Spring Data JPA 简单介绍
- tangfl - BlogJava-首页技术区考虑到公司应用中数据库访问的多样性和复杂性,目前正在开发UDSL(统一数据访问层),开发到一半的时候,偶遇SpringData工程. 于是就花了点时间了解SpringData,可能UDSL II期会基于SpringData做扩展. 介绍:针对关系型数据库,KV数据库,Document数据库,Graph数据库,Map-Reduce等一些主流数据库,采用统一技术进行访问,并且尽可能简化访问手段.
<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>
@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;
public interface UserDao extends PagingAndSortingRepository<User, Long>
public List<User> findByUsername(String username);
public List<User> findByUsernameAndPassword(String username,String password);
@Lock(LockModeType.PESSIMISTIC_READ) public List<User> findByUsername(String username);
select * from t_user where username=? lock in share mode
@Lock(LockModeType.PESSIMISTIC_WRITE) public List<User> findByUsername(String username);
select * from t_user where username=? for update
@QueryHints() public List<User> findByUsername(String username, Pageable pageable);
PageRequest pr = new PageRequest(int page, int size); PageRequest pr = new PageRequest(int page, int size, Sort sort); etc...
@QueryHints(value = { @QueryHint(name = "name", value = "value")})