当前位置:   article > 正文

springBoot+hibernate(SessionFaction)整合的两套方法_springboot整合hibernate sessionfactory

springboot整合hibernate sessionfactory

springBoot+hibernate(SessionFaction)这个东西是一个朋友让我帮忙搭建的,他说在网上找了很多,很多都是打着springBoot+hibernate的幌子,写的是boot+jpa的代码,最近用不上,查着还浪费时间,因此做此笔记。

application.properties方式

server.port=8081

spring.datasource.url=jdbc:mysql://localhost:3306/shopuu
spring.datasource.username=root
spring.datasource.password=ROOT
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在写个配置类

package com.shopuu.boot.config;

import org.hibernate.SessionFactory;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SessionFac {

    @Bean
    public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
        return hemf.getSessionFactory();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

这就算完成了,boot 默认端口8080,此处修改为8081

这里写图片描述

然后写个service 就可以进行调用了

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shopuu.boot.config.User;

@Service("userService")
public class UserSrevice {

    @Autowired
    private SessionFactory sessionFaction;


    public  void sace(User u){
        System.out.println(sessionFaction.getClass());
        sessionFaction.openSession().save(u);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

配置类初始化Session

    application.properties
  • 1
server.port=8081

#spring.datasource.url=jdbc:mysql://localhost:3306/shopuu
#spring.datasource.username=root
#spring.datasource.password=ROOT
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

因为配置文件不加载数据库东西,所以 SpringApplication.java也要做相应的修改,排除数据库的初始化

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

配置类SessiFaction.java

package com.shopuu.boot.config;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SessiFaction {

    @Bean(name="openSession")
    public Session openSession(){
        //StandardServiceRegistryBuilder默认从resource下加载hibernate.cfg.xml
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();

        SessionFactory sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();

        Session openSession = sessionFactory.openSession();
        return openSession;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

new StandardServiceRegistryBuilder() 加载hibernate.cfg.xml

这里写图片描述

然后写个service 就可以了


    @Autowired
    private Session openSession;

    public List getUser(){
        String sql="select * from shopuu_user";

        List list = openSession.createSQLQuery(sql).list();
        return list;
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/815323
推荐阅读
相关标签
  

闽ICP备14008679号