Spring+JPA如何集成Hibernate
这篇文章主要介绍Spring+JPA如何集成Hibernate,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
1.JPA简介
Java Persistence API (JPA),为开发人员提供了一种对象/关系映射(O-R Mapping)工具,用来管理 Java 应用中的关系数据。JPA是EJB3.0的一部分,现在公认为ORM行业标准。JPA本身只是一个规范,而不是产品; 它本身不能进行持续性或其他任何东西。JPA只是一组接口,需要一个实现。JPA允许通过标准的XML格式和注解格式声明,用于定义Java类如何映射到关系数据库表的映射规则。。JPA也定义了在对数据库中的对象处理查询和事务运行时的EntityManager的API。JPA定义一个对象级查询语言,JPQL,以允许从所述数据库中的对象的查询。JPA常用的解决方案:
EclipseLink (Eclipse)
Hibernate (RedHat)
Open JPA (Apache)
DataNucleus
Ebean (SourceForge)
TopLink Essentials (Glassfish)
TopLink (Oracle)
Kodo (Oracle)
JPA 规范也不是一成不变的。从2006年的JPA1.0,到2009年JPA2.0,最后到2013年JPA2.1。版本之间的特性具体参照
https://en.wikibooks.org/wiki/Java_Persistence/What_is_JPA%3F
https://en.wikipedia.org/wiki/Java_Persistence_API
2.Spring Hibernate JPA整合
准备
表
CREATE TABLE `Employee` (
`id` int(11) unsigned NOT NULL,
`name` varchar(20) DEFAULT NULL,
`role` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.1项目结构
2.2 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-relational-data-access</artifactId>
<version>0.1.0</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h3database</groupId>
<artifactId>h3</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
<repository>
<id>org.jboss.repository.releases</id>
<name>JBoss Maven Release Repository</name>
<url>https://repository.jboss.org/nexus/content/repositories/releases</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>1.1.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
使用了platform-bom,方便了包的依赖管理。
依赖树结构
2.3 persistence.xml
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JPAExamples">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
</persistence-unit>
</persistence>
2.4 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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!--数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/exampledb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="META-INF/persistence.xml"/>
<property name="persistenceUnitName" value="JPAExamples"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaDialect" ref="jpaDialect"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="database" value="MYSQL"/>
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
<bean id="entityManager" factory-bean="entityManagerFactory" factory-method="createEntityManager"></bean>
<!-- Jpa 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<!-- 启动对@AspectJ(面向切面)注解的支持 -->
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.journaldev.spring.jpa"></context:component-scan>
</beans>
有的对象,不是必须的。为了保持完整性,做了保留。
可以配置entityManagerFactory的packagesToScan属性,没有配置也是可以的。
2.5 entity
package com.journaldev.spring.jpa.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
private int id;
private String name;
private String role;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@Override
public String toString() {
return "{ID=" + id + ",Name=" + name + ",Role=" + role + "}";
}
}
2.6 接口
public interface EmployeeDAO {
//Create
public void save(Employee employee);
//Read
public Employee getById(int id);
//Update
public void update(Employee employee);
//Delete
public void deleteById(int id);
//Get All
public List<Employee> getAll();
}
实现类
package com.journaldev.spring.jpa.dao;
import com.journaldev.spring.jpa.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.transaction.Transactional;
import java.util.List;
@Transactional
@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
@PersistenceContext
EntityManager em;
@Override
public void save(Employee employee) {
em.persist(employee);
}
@Override
public Employee getById(int id) {
return em.find(Employee.class, id);
}
public void update(Employee employee) {
em.merge(employee);
}
@Override
public void deleteById(int id) {
em.remove(this.getById(id));
}
@Override
public List<Employee> getAll() {
CriteriaBuilder builder =em.getCriteriaBuilder();
final CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
return this.em.createQuery(query).getResultList();
}
}
2.7测试
测试类
import com.journaldev.spring.jpa.dao.EmployeeDAO;
import com.journaldev.spring.jpa.model.Employee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;
import javax.annotation.Resource;
import java.util.Random;
@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class SpringJPATest extends AbstractTransactionalJUnit4SpringContextTests {
@Resource
private EmployeeDAO employeeDAO;
@Test
public void testSave(){
Employee emp = new Employee();
int rand = new Random().nextInt(1000);
emp.setId(rand);
emp.setName("Pankaj");
emp.setRole("Java Developer");
employeeDAO.save(emp);
}
@Test
public void testUpdate(){
Employee emp = new Employee();
int rand = new Random().nextInt(1000);
emp.setId(rand);
emp.setName("Pankaj");
emp.setRole("Java Developer");
employeeDAO.save(emp);
emp.setName(emp.getName()+"_update");
employeeDAO.update(emp);
}
}
OR
import com.journaldev.spring.jpa.dao.EmployeeDAO;
import com.journaldev.spring.jpa.model.Employee;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Random;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
final EmployeeDAO employeeDAO = ctx.getBean(EmployeeDAO.class);
Employee emp = new Employee();
int rand = new Random().nextInt(1000);
emp.setId(rand);
emp.setName("Pankaj");
emp.setRole("Java Developer");
employeeDAO.save(emp);
Employee employee = employeeDAO.getById(rand);
employee.setName(employee.getName() +"_update");
employeeDAO.update(employee);
}
}
有一点需要说明。默认使用的日志是logback.号称比log4j更优秀的一款。
默认的日志配置logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="org.hibernate" level="debug" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>
以上是“Spring+JPA如何集成Hibernate”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341