1.背景
最近一直致力于老的ssh框架改造工程,各种冲突各种解,难受的一匹,从最初的硬刚,到现在的基础引擎和技术栈都升级最新一点点往上整合叠加,走出了自己的三分地
这个老项目使用的是ssh框架,springmvc+hibernate3,然后改造后使用的是springboot2.3.4+hibernate5
2.整合
(1).spring官方初始化,添加使用以下pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
(2) 配置文件
#开发环境
server:
port: 8001
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?autoReconnect=true
username: root
password: root
hikari:
#池中最大链接数
maximum-pool-size: 50
#池中链接最长生命周期
max-lifetime: 120000
#连接允许在池中闲置的最长时间
idle-timeout: 600000
#等待来自池的连接的最大毫秒数
connection-timeout: 30000
#池中最小空闲链接数
minimum-idle: 10
jpa:
database-platform: org.hibernate.dialect.MySQLDialect
hibernate:
ddl-auto: update
database: mysql
show-sql: true
open-in-view: false #懒加载时使用,建议false
(3)数据源配置类
因为我要使用hibernateTemplate做查询,所以有此配置
package com.example.hibernate.demo.config;
import com.example.hibernate.demo.pojo.Student;
import com.example.hibernate.demo.util.IdUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
/**
* @author lvxiucai
* @description
* @date 2020/10/22
*/
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class ApplicationConfig {
@Value("${spring.datasource.url}")
private String jdbcUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
// @Autowired
// private DataSource dataSource;
@Autowired
private SessionFactory sessionFactory;
@Bean
public DataSource dataSource(){
return DataSourceBuilder.create()
.url(jdbcUrl)
.username(username)
.password(password)
.driverClassName(driverClassName)
.build();
}
@Bean
public LocalSessionFactoryBean localSessionFactoryBean(){
Properties properties=new Properties();
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
LocalSessionFactoryBean lsb=new LocalSessionFactoryBean();
lsb.setDataSource(dataSource());
// lsb.setAnnotatedClasses(Student.class);这里demo可以这么写,但我们项目不可能就一个实体类,所以用下面的setPackagesToScan方法
lsb.setPackagesToScan("com.example.hibernate.demo.pojo");
lsb.setHibernateProperties(properties);
return lsb;
}
@Bean
public HibernateTemplate hibernateTemplate(){
HibernateTemplate hibernateTemplate = new HibernateTemplate();
hibernateTemplate.setSessionFactory(sessionFactory);
return hibernateTemplate;
}
/*@Bean
public HibernateTransactionManager hibernateTransactionManager(){
HibernateTransactionManager ts = new HibernateTransactionManager();
ts.setSessionFactory(sessionFactory);
return ts;
}*/
}
3.项目地址
https://gitee.com/lvxiucai/sprinboot2.3-hibernate5-demo