JPA 应用
1.pom依赖
<dependencies>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring-data-jpa-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.1-b04</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
</dependency>
<!-- spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<!-- spring对orm框架的支持包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<!-- hibernate-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.0.Final</version>
</dependency>
<!-- hibernate对jpa的实现jar-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.0.Final</version>
</dependency>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!-- spring test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
</dependencies>
2.spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
">
<!-- 1.配置数据源-->
<context:property-placeholder location="classpath*:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--2. 配置一个JPA中非常重要的对象,entityManagerFactory-->
<!-- entityManager类似于mybatis中的SqlSession-->
<!-- entityManagerFactory类似于Mybatis中的SqlSessionFactory-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!--配置包扫描(pojo实体类所在的包)-->
<property name="packagesToScan" value="org.example.jpa.pojo"/>
<!--指定jpa的具体实现,也就是hibernate-->
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
</property>
<!--jpa方言配置,不同的jpa实现对于类似于beginTransaction等细节实现起来是不一样的,
所以传入JpaDialect具体的实现类-->
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"></bean>
</property>
<!--配置具体provider,hibearnte框架的执行细节-->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!--定义hibernate框架的一些细节-->
<!--
配置数据表是否自动创建
因为我们会建立pojo和数据表之间的映射关系
程序启动时,如果数据表还没有创建,是否要程序给创建一下
-->
<property name="generateDdl" value="false"/>
<!--
指定数据库的类型
hibernate本身是个dao层框架,可以支持多种数据库类型的,这里就指定本次使用的什么数据库
-->
<property name="database" value="MYSQL"/>
<!--
配置数据库的方言
hiberante可以帮助我们拼装sql语句,但是不同的数据库sql语法是不同的,所以需要我们注入具体的数据库方言
-->
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
<!--
是否显示sql
操作数据库时,是否打印sql
-->
<property name="showSql" value="true"/>
</bean>
</property>
</bean>
<!--3 事务管理器配置
jdbcTemplate/mybatis 使用的是DataSourceTransactionManager
jpa规范:JpaTransactionManager
-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!--4 引用上面创建的entityManagerFactory
<jpa:repositories> 配置jpa的dao层细节
base-package:指定dao层接口所在包
-->
<jpa:repositories base-package="org.example.jpa.dao" entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager"/>
<!--5、声明式事务配置-->
<!--
<tx:annotation-driven/>
-->
<!--6、配置spring包扫描-->
<context:component-scan base-package="org.example.jpa"/>
</beans>
3.实体、dao
package org.example.jpa.pojo;
import javax.persistence.*;
/**
* 实体类(在类中要使用注解建立实体类和数据表之间的映射关系以及属性和字段的映射关系)
* 1、实体类和数据表映射关系
* @Entity
* @Table
*
* 2、实体类属性和表字段的映射关系
* @Id 标识主键
* @GeneratedValue 标识主键的生成策略
* @Column 建立属性和字段映射
*/
@Entity
@Table(name = "tb_resume")
public class Resume {
@Id
/**
* 生成策略经常使用的两种:
* GenerationType.IDENTITY:依赖数据库中主键自增功能 Mysql
* GenerationType.SEQUENCE:依靠序列来产生主键 Oracle
*/
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "address")
private String address;
@Column(name = "phone")
private String phone;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Resume{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
package org.example.jpa.dao;
import org.example.jpa.pojo.Resume;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* 一个符合SpringDataJpa要求的Dao层接口是需要继承 JpaRepository 和 JpaSpecificationExecutor
* <p>
* JpaRepository<操作的实体类类型,主键类型>
* 封装了基本的CRUD操作
* <p>
* JpaSpecificationExecutor<操作的实体类类型>
* 封装了复杂的查询(分页、排序等)
*/
public interface ResumeDao extends JpaRepository<Resume, Long>, JpaSpecificationExecutor<Resume> {
@Query("from Resume where id=?1 and name=?2")
public List<Resume> findByJpql(Long id, String name);
/**
* 使用原生sql语句查询,需要将nativeQuery属性设置为true,默认为false(jpql)
*
* @param name
* @param address
* @return
*/
@Query(value = "select * from tb_resume where name like ?1 and address like ?2", nativeQuery = true)
public List<Resume> findBySql(String name, String address);
/**
* 方法命名规则查询
* 按照name模糊查询(like)
* 方法名以findBy开头
* -属性名(首字母大写)
* -查询方式(模糊查询、等价查询),如果不写查询方式,默认等价查询
*/
public List<Resume> findByNameLikeAndAddress(String name, String address);
}