作者:哈铁鸡西青年志愿者 | 来源:互联网 | 2023-10-12 11:11
spring Boot 整合Spring Data JPA之Hibernate JPA(七)
在spring boot整合spring data jpa之前先了解一下Hibernate JPA
JPA:由 Sun 公司提供了一对对于持久层操作的标准(接口+文档) Hibernate:是 Gavin King 开发的一套对于持久层操作的自动的 ORM 框架。 Hibernate JPA:是在 Hibernate3.2 版本那种提供了对于 JPA 的标准的实现。提供了一套按 照 JPA 标准来实现持久层开发的 API 个人学习完之后到感觉:JPA其实不算是一套框架,更加像一套规则,或者是标准。
一、 Spring 整合 Hibernate JPA
在Hibernate项目中导入HIbernateJPA到jar包,这里我用但是web项目,不是maven项目。 配置xml到不同,Hibernate配置如下
<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: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/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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="driverClass" value="${jdbc.driver.class}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
props>
property>
<property name="packagesToScan">
<list>
<value>com.bjsxt.pojovalue>
list>
property>
bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.bjsxt"/>
beans>
Hibernate JPA配置文件
<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: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/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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="driverClass" value="${jdbc.driver.class}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL"/>
<property name="generateDdl" value="true"/>
<property name="showSql" value="true"/>
bean>
property>
<property name="packagesToScan">
<list>
<value>com.bjsxt.pojovalue>
list>
property>
bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.bjsxt"/>
beans>
二、 Hibernate JPA 的 CRUD 操作
1.实现接口,编写DaoImpl,这里只做参考
@Repository
public class UsersDaoImpl implements UsersDao {
@PersistenceContext(name="entityManagerFactory")
private EntityManager entityManager;
@Override
public void insertUsers(Users users) {
this.entityManager.persist(users);
}
@Override
public void updateUsers(Users users) {
this.entityManager.merge(users);
}
@Override
public void deleteUsers(Users users) {
Users u = this.selectUsersById(users.getUserid());
this.entityManager.remove(u);
}
@Override
public Users selectUsersById(Integer userid) {
return this.entityManager.find(Users.class, userid);
}
@Override
public List<Users> selectUserByName(String username) {
return this.entityManager.createQuery(" from Users where username = :abc").setParameter("abc", username).getResultList();
}
@Override
public List<Users> selectUserByNameUseSQL(String username) {
return this.entityManager.createNativeQuery("select * from t_users where username = ?", Users.class).setParameter(1, username).getResultList();
}
@Override
public List<Users> selectUserByNameUseCriteria(String username) {
CriteriaBuilder builber = this.entityManager.getCriteriaBuilder();
CriteriaQuery<Users> query = builber.createQuery(Users.class);
Root<Users> root = query.from(Users.class);
Predicate cate = builber.equal(root.get("username"), username);
query.where(cate);
TypedQuery<Users> typeQuery = this.entityManager.createQuery(query);
return typeQuery.getResultList();
}
}