在Spring(4.3.22)中集成Hibernate(5.4.0)
Posted Coreqi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Spring(4.3.22)中集成Hibernate(5.4.0)相关的知识,希望对你有一定的参考价值。
(1)pom中添加相关依赖
1 <dependency> 2 <groupId>org.hibernate</groupId> 3 <artifactId>hibernate-core</artifactId> 4 <version>5.4.0.Final</version> 5 </dependency> 6 7 <dependency> 8 <groupId>org.springframework</groupId> 9 <artifactId>spring-orm</artifactId> 10 <version>4.3.22.RELEASE</version> 11 </dependency>
(2)声明数据源(Druid),声明Hibernate的Session工厂,声明Hibernate的事务管理器
1 package cn.coreqi.config; 2 3 import com.alibaba.druid.pool.DruidDataSource; 4 import org.hibernate.SessionFactory; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.orm.hibernate5.HibernateTransactionManager; 8 import org.springframework.orm.hibernate5.LocalSessionFactoryBean; 9 import org.springframework.transaction.annotation.EnableTransactionManagement; 10 11 import javax.sql.DataSource; 12 import java.util.Properties; 13 @EnableTransactionManagement 14 @Configuration 15 public class JdbcConfig { 16 @Bean 17 public DataSource dataSource(){ 18 DruidDataSource ds = new DruidDataSource(); 19 ds.setDriverClassName("com.mysql.jdbc.Driver"); 20 ds.setUrl("jdbc:mysql://localhost:3306/hibernatedemo?serverTimezone=UTC"); 21 ds.setUsername("root"); 22 ds.setPassword("123456"); 23 return ds; 24 } 25 26 @Bean 27 public LocalSessionFactoryBean sessionFactory(DataSource dataSource){ 28 LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); 29 sessionFactoryBean.setDataSource(dataSource); 30 sessionFactoryBean.setPackagesToScan(new String[]{"cn.coreqi.entities"}); 31 Properties props = new Properties(); 32 props.setProperty("dialect","org.hibernate.dialect.MySQL8Dialect"); 33 props.setProperty("show_sql","true"); 34 props.setProperty("format_sql","true"); 35 props.setProperty("hbm2ddl.auto","update"); 36 return sessionFactoryBean; 37 } 38 @Bean 39 public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){ 40 return new HibernateTransactionManager(sessionFactory); 41 } 42 }
(3)编写dao
1 package cn.coreqi.dao.SpringHibernate; 2 3 import cn.coreqi.entities.Users; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.query.Query; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Repository; 9 import org.springframework.transaction.annotation.Transactional; 10 11 import javax.persistence.criteria.CriteriaBuilder; 12 import javax.persistence.criteria.CriteriaQuery; 13 import javax.persistence.criteria.Root; 14 import java.util.List; 15 16 @Repository 17 @Transactional 18 public class UsersSpringHibernate { 19 @Autowired 20 private SessionFactory sessionFactory; 21 22 private Session currentSession(){ 23 return sessionFactory.getCurrentSession(); 24 } 25 26 public int count(){ 27 return getAll().size(); 28 } 29 30 public Users getUserByUserName(String username){ 31 CriteriaBuilder builder = currentSession().getCriteriaBuilder(); 32 CriteriaQuery<Users> query = builder.createQuery(Users.class); 33 Root<Users> root = query.from(Users.class); 34 query.select(root).where(builder.equal(root.get("UserName"),username)); 35 Query<Users> q = currentSession().createQuery(query); 36 return q.getSingleResult(); 37 } 38 39 public List<Users> getAll(){ 40 CriteriaBuilder builder = currentSession().getCriteriaBuilder(); 41 CriteriaQuery<Users> query = builder.createQuery(Users.class); 42 Root<Users> root = query.from(Users.class); 43 query.select(root); 44 Query<Users> q=currentSession().createQuery(query); 45 List<Users> users=q.getResultList(); 46 return users; 47 } 48 public Users getUserById(Integer id){ 49 return (Users)currentSession().get(Users.class,id); 50 } 51 }
Hibernate5和Hibernate4有些区别,具体的dao编写方式请参考这篇文章 https://www.boraji.com/hibernate-5-criteria-query-example
以上是关于在Spring(4.3.22)中集成Hibernate(5.4.0)的主要内容,如果未能解决你的问题,请参考以下文章