本文主要介绍Spring JPA如何集成Hibernate。很详细,有一定的参考价值。感兴趣的朋友一定要看!
1.JPA简介
Java持久性API (JPA)为开发人员在Java应用程序中管理关系数据提供了一个对象/关系映射工具。JPA是EJB3.0的一部分,现在被公认为ORM行业标准。JPA本身只是规范,不是产品;它本身无法维持或维持任何其他东西。JPA只是一组接口,需要实现。JPA允许标准XML格式和注释格式的声明,用于定义Java类如何映射到关系数据库表的映射规则。JPA还定义了EntityManager在处理数据库中对象的查询和事务时的API。JPA定义了一种对象级查询语言JPQL,允许从数据库中的对象进行查询。JPA的常见解决方案:
月食
冬眠(红帽)
开放JPA (Apache)
数据核心
Ebean (SourceForge)
TopLink Essentials (Glassfish)
TopLink(甲骨文)
鼓童(甲骨文)
JPA规范也不是一成不变的。从2006年的JPA1.0到2009年的JPA2.0,最后到2013年的JPA2.1。版本之间属性的特定引用
https://en.wikibooks.org/wiki/Java_Persistence/What_is_JPA?
https://en.wikipedia.org/wiki/Java_Persistence_API
2.Spring Hibernate JPA集成
准备
桌子
CREATETABLE`Employee `(
` id`int(11)unsignedNOTNULL,
` name`varchar(20)DEFAULTNULL,
` role`varchar(20)DEFAULTNULL,
PRIMARYKEY(` id `) ENGINE=innoddefaultcharset=utf8;2.1项目结构
2.2 pom.xml
?xmlversion='1.0 '编码='UTF-8 '?
project xmlns=' http://aven . Apache . org/POM/4 . 0 . 0 '
xmlns : xsi=' http://www . w3 . org/2001/XMLSchema-instance '
xsi : schema location=' http://aven . Apache . org/POM/4 . 0 . 0http://aven . Apache . org/xsd/maven-4 . 0 . 0 . xsd '
模型版本4 . 0 . 0/模型版本
groupIdorg.springframework/groupId
artifactIdgs-关系数据访问/artifactId
版本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; /** * Created by zhaoguoyu on 2016/6/22. */ @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; /** * Created by zhaoguoyu on 2016/6/22. */ @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; /** * Created by zhaoguoyu on 2016/6/22. */ 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”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/113125.html