在 pom.xml 中引入 spring jpa
以及测试包
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-test
test
org.springframework
spring-test
org.springframework
spring-test
5.0.5.RELEASE
test
在 application.properties
中添加数据源和 hibernate
的配置
spring.datasource.url=jdbc:mysql://localhost:3306/mysql
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming.physical-strategy = org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
使用 application.yml
的配置为
### datasource Configuration
spring:
datasource:
url: jdbc:mysql://localhost:3306/mysql
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
### hibernate Configuration
jpa:
database: mysql
show-sql: true
hibernate:
ddl-auto: update
naming:
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
bean 文件增加注释使之与数据库表结构对应
package com.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* @Description 类描述
* @author 欧阳
* @since 2019年4月7日 下午12:29:13
* @version V1.0
*/
@Getter
@Setter
@ToString
@Entity
@Table(name="t_user")
public class Users2 {
@Id
private String id;
@Column(name="name")
private String name;
}
添加文件 src/main/java/com/dao/UsersDao2.java
package com.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.bean.Users2;
/**
* @Description 整合Hibernate Dao
* @author 欧阳
* @since 2019年4月11日 下午6:14:26
* @version V1.0
*/
public interface UsersDao2 extends JpaRepository {
@Query(value = "FROM Users2 WHERE name = :name")
public Users2 findName1(@Param("name") String name);
@Query(value = "SELECT * FROM t_user WHERE name=?1", nativeQuery = true)
public Users2 findName2(String name);
}
增加 Service 接口 src/main/java/com/service/HibernateService.java
并实现 src/main/java/com/service/impl/HibernateServiceImpl.java
。
HibernateService 接口代码
package com.service;
import java.util.List;
import com.bean.Users2;
/**
* @Description 整合Hibernate Service
* @author 欧阳
* @since 2019年4月11日 下午2:17:37
* @version V1.0
*/
public interface HibernateService {
/**
* 添加用户
* @param user
*/
public void insertUser(Users2 user);
/**
* 更新用户
* @param user
*/
public void updateUser(Users2 user);
/**
* 查询用户
* @param user
* @return
*/
public List selectUser();
/**
* 删除用户
* @param id
*/
public void deleteUser(String id);
}
HibernateServiceImpl 类
package com.service.impl;
import java.util.List;
import javax.persistence.EntityManagerFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bean.Users2;
import com.dao.UsersDao2;
import com.service.HibernateService;
/**
* @Description 整合Hibernate Service
* @author 欧阳
* @since 2019年4月11日 下午6:16:09
* @version V1.0
*/
@Service
public class HibernateServiceImpl implements HibernateService {
@Autowired
private UsersDao2 userDao2;
@Autowired
private EntityManagerFactory entityManagerFactory;
public SessionFactory getSessionFactory() {
return entityManagerFactory.unwrap(SessionFactory.class);
}
@Override
@Transactional
public void insertUser(Users2 user) {
userDao2.save(user);
// int num = 1/0; //放开测试事务。预期结果为添加失败,库中无数据。测试结果与预期一致
}
@Override
@Transactional
public void updateUser(Users2 user) {
userDao2.save(user);
}
@Override
public List selectUser() {
String sql = "SELECT u FROM Users2 u ";
@SuppressWarnings("unchecked")
List resultList = getSessionFactory().openSession()
.createQuery(sql)
.getResultList();
userDao2.findName1("张三");
userDao2.findName2("张三");
return resultList;
}
@Override
@Transactional
public void deleteUser(String id) {
userDao2.deleteById(id);
}
}
注意:通过使用 @Transactional
注解添加事务,在 insertUser
方法中的插入数据代码后增加异常代码 int num = 1/0;
测试事务是否起作用,实际测试结果表明测试结果与预期一致。
添加测试类测试 HibernateServiceImpl
package com.service.impl;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.App;
import com.bean.Users2;
import com.service.HibernateService;
/**
* @Description 整合Hibernate 测试增删改查
* @author 欧阳
* @since 2019年4月11日 下午6:59:23
* @version V1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes={App.class})
public class HibernateServiceImplTest {
@Autowired
private HibernateService hibernateService;
@Test
public void testInsertUser() {
hibernateService.insertUser(new Users2("12", "lisi"));
System.out.println("Yes");
}
@Test
public void testUpdateUser() {
hibernateService.updateUser(new Users2("12", "李四"));
System.out.println("Yes");
}
@Test
public void testSelectUser() {
List users = hibernateService.selectUser();
for(Users2 user2 : users) {
System.out.println(user2);
}
System.out.println("Yes");
}
@Test
public void testDeleteUser() {
hibernateService.deleteUser("12");
System.out.println("Yes");
}
}
测试结果全部通过,增删改查功能正常,springboot 整合 Hibernate 完成。