【Spring Data JPA】02 快速上手
完成一个CRUD
- 创建工程导入依赖坐标
- 配置Spring的配置文件
- 配置ORM的实体类,绑定映射关系
- 编写一个符合SpringDataJpa的dao接口
Maven依赖坐标
<properties> <spring.version>5.0.2.RELEASE</spring.version> <hibernate.version>5.0.7.Final</hibernate.version> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</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-context-support</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-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.1.Final</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.9.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.6.RELEASE</version> </dependency> <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.el</artifactId> <version>2.2.4</version> </dependency> </dependencies>
配置Spring容器【context.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:aop="http://www.springframework.org/schema/aop" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd " > <!-- 创建数据库连接池 【数据源】 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.cj.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql:///jpa?serverTimezone=Asia/Shanghai"/> <property name="user" value="root"/> <property name="password" value="123456" /> </bean> <!-- 实体管理器工厂--> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <!-- 数据源信息 --> <property name="dataSource" ref="dataSource" /> <!-- 要扫描的包的位置,扫描这些带有JPA注解的实体类 --> <property name="packagesToScan" value="cn.dai.entity" /> <!-- JPA规范的实现者 hibernate --> <property name="persistenceProvider"> <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/> </property> <!-- JPA实现者的适配器--> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="MYSQL"/> <!-- 数据库类型 --> <property name="generateDdl" value="false"/> <!-- 是否自动创建数据库表?--> <property name="showSql" value="true"/> <!-- 执行是否输出SQL语句? --> <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/> <!-- 数据库平台方言设置 --> <!--<property name="prepareConnection" value="true" />--> </bean> </property> <property name="jpaDialect"> <!-- JPA也有方言。。。--> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/> </property> </bean> <!-- 配置事务管理器 --> <bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- 整合SpringDataJpa --> <jpa:repositories base-package="cn.dai.dao" transaction-manager-ref="jpaTransactionManager" entity-manager-factory-ref="jpaTransactionManager" /> <!-- 声明式事务【省略】 --> <!-- 配置实体类包扫描 --> <context:component-scan base-package="cn.dai.entity" /> </beans>
数据库类型是以一个枚举的形式提供的
这是枚举类:
数据库方言:数据库支持的特有语法
每个版本都不一样
编写实体类,并绑定隐射关系
【打上@Entity注解,就是会注解值报错,不知道为什么】
@Entity @Table(name = "cst_customer") public class Customer { public Customer() { } @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cust_id") private Long custId; @Column(name = "cust_name") private String custName; @Column(name = "cust_source") private String custSource; @Column(name = "cust_level") private String custLevel; @Column(name = "cust_industry") private String custIndustry; @Column(name = "cust_phone") private String custPhone; @Column(name = "cust_address") private String custAddress; public Long getCustId() { return custId; } public void setCustId(Long custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustIndustry() { return custIndustry; } public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } public String getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } @Override public String toString() { return "entity{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custLevel='" + custLevel + '\'' + ", custIndustry='" + custIndustry + '\'' + ", custPhone='" + custPhone + '\'' + ", custAddress='" + custAddress + '\'' + '}'; } }
编写Dao接口,并符合SpringDataJpa的规范
public interface CustomerDao extends JpaRepository<cn.dai.entity.Customer, java.lang.Long>, // 实现SpringDataJpa的规范 JpaSpecificationExecutor<cn.dai.entity.Customer> { // 泛型 <T, ID> 表示的意思: // T : 操作的实体类类型 // TD : 实体类主键的类型 // JpaRepository 封装了CRUD // JpaSpecificationExecutor 封装了复杂查询【分页】 }
编写测试类测试:
package cn.dai.test; import cn.dai.dao.CustomerDao; import cn.dai.entity.Customer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author ArkD42 * @file SpringDataJPA * @create 2020 - 05 - 27 - 12:08 */ @RunWith(SpringJUnit4ClassRunner.class) //不需要单元测试,交给Spring的单元测试测试 @ContextConfiguration(locations = "classpath:application-context.xml") // 调用容器(有两种形式,配置文件和注册类,这里使用的文件配置,当然是调用配置文件) public class CustomerDaoTest { //自动装配对象,获取 @Autowired private CustomerDao customerDao; @Test //不要忘记测试注解 public void queryOne(){ // 调用SpringDataJpa封装的接口实现方法 Customer customer = customerDao.findOne(3l); System.out.println(customer); } }
这个错误是因为Spring-Test版本太高导致
【也不能说太高把,就是和Spring系列的框架版本不一致,总的划分是4版本和5版本,这是大版本的一个分水岭】
【所以在Maven坐标导入的时候要注意一下这个问题】
又出现错误,容器加载失败
经过排错,发现是BEAN写错了,干
最后测试成功!