上次我们讲述了《Spring-Data-Jpa概述与接口》,接下来我们再讲讲Spring-Data-Jpa环境配置与实际应用。
Spring-Data 方法定义规范与使用配置
简单条件查询: 查询某一个实体类或者集合
按照 Spring Data 的规范,查询方法以 find | read | get 开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性以首字母大写。
例如:定义一个 Entity 实体类:
class User{ private String firstName; private String lastName; }
使用And条件连接时,应这样写:
findByLastNameAndFirstName(String lastName,String firstName);
条件的属性名称与个数要与参数的位置与个数一一对应
直接在接口中定义查询方法,如果是符合规范的,可以不用写实现,目前支持的关键字写法如下:
@Query注解
这种查询可以声明在 Repository 方法中,摆脱像命名查询那样的约束,将查询直接在相应的接口方法中声明,结构更为清晰,这是 Spring data 的特有实现。
@Query来指定本地查询
自定义 Repository 方法
定义一个接口: 声明要添加的, 并自实现的方法
提供该接口的实现类: 类名需在要声明的 Repository 后添加 Impl, 并实现方法
声明 Repository 接口, 并继承 1) 声明的接口
使用.
注意: 默认情况下, Spring Data 会在 base-package 中查找 "接口名Impl" 作为实现类. 也可以通过 repository-impl-postfix 声明后缀.
环境配置与实际应用
pom.xml 文件坐标配置
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <slf4j.version>1.7.0</slf4j.version> <junit.version>4.12</junit.version> <spring.version>4.3.2.RELEASE</spring.version> <hibernate.version>4.3.11.Final</hibernate.version> </properties> <dependencies> <!-- slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 数据库依赖 --> <!-- mysql数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- spring3 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- springdatajpa --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>1.7.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.5.1.RELEASE</version> </dependency> <!-- aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.0</version> </dependency> <!-- hibernate4 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3.1</version> </dependency> </dependencies> jdbc.properties 文件 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/jpa?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root 上海尚学堂java 加薇芯 java8733获取更多资料 spring.xml 文件配置 <?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.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.shsxt"></context:component-scan> <!-- 1. 配置数据源 --> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></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.shsxt.entity"></property> <property name="jpaProperties"> <props> <!-- 生成的数据表的列的映射策略 --> <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.shsxt.dao" entity-manager-factory-ref="entityManagerFactory" repository-impl-postfix="Impl" ></jpa:repositories> </beans>
Dao 接口实例代码
package com.shsxt.dao; import com.shsxt.entity.User; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import java.util.List; /** * Created by lp on 2017/12/21. */ public interface UserRepository extends UserDao,JpaRepository<User,Integer> { /* 命名查询约束定义 */ // 根据属性字段精确查询 public List<User> findByUserName(String userName); // 根据属性字段模糊匹配 public List<User> findByUserNameLike(String userName); // 多条件匹配 public List<User> findByUserNameAndUserPwd(String userName,String userPwd); // 返回分页对象 页码 每页大小 根据用户名模糊匹配用户记录 页码 每页大小设置 public Page<User> findByUserNameLike(String userName, Pageable pageable); // 返回分页列表数据 public List<User> findByUserPwdLike(String userPwd, Pageable pageable); /* query查询 参数传递01:占位符 上海尚学堂java 加薇心 java8733 获取更多相关技术信息资料 */ @Query("select u from User u where u.userName like %?1%") public List<User> UserByUserName(String userName); @Query("select u from User u where u.userName=?1 and u.userPwd=?2") public List<User> loadUserByUserNameAndUserPwd(String userName,String userPwd); /** *参数传递02:命名参数 * @param userName * @param userPwd * @return */ @Query("select u from User u where u.userName like :userName and u.userPwd =:userPwd") public List<User> loadUserByUserNameAndUserPwd02(@Param("userName") String userName,@Param("userPwd") String userPwd); /** * 占位符添加模糊匹配标志 */ @Query("select u from User u where u.userName like %:userName%") public List<User> loadUserByUserName02(@Param("userName") String userName,Pageable pageable); /** * 本地sql 查询支持 */ @Query(value = "select * from t_user u where u.user_name like %?1%",nativeQuery = true) public List<User> loadUserByUserName03(String userName); }
来源:https://www.cnblogs.com/shsxt/p/8205991.html