兑现承诺,给粉丝专门搞了一个SpringBoot2.x整合hibernate5的教程,附带视频+源码,免费下载!

  • 1、导入核心依赖
  • 2、springboot的yml配置文件
  • 3、注册hibernate的依赖
  • 4、POJO类
  • 5、新增和查询
  • 6、源码下载
 步骤 1  导入核心依赖    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
        
        
            org.hibernate
            hibernate-entitymanager
        
        
            org.hibernate
            hibernate-core
        
        
            mysql
            mysql-connector-java
            runtime
        

主要就是导入hibernate的两个依赖,还有mysql的驱动。

 步骤 2  springboot的yml配置文件    
#开发环境
server:
  port: 80
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/myboot?autoReconnect=true
    username: root
    password: 123
    hikari:
      #池中最大链接数
      maximum-pool-size: 50
      #池中链接最长生命周期
      max-lifetime: 120000
      #连接允许在池中闲置的最长时间
      idle-timeout: 600000
      #等待来自池的连接的最大毫秒数
      connection-timeout: 30000
      #池中最小空闲链接数
      minimum-idle: 10

 步骤 3  注册hibernate的依赖    

兑现承诺,给粉丝专门搞了一个SpringBoot2.x整合hibernate5的教程,附带视频+源码,免费下载!_第1张图片

package com.example.myboot.config;
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;

@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class HibernateConfig {
    @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 SessionFactory sessionFactory;

    /** 数据源采用springboot推荐的hikari* */
    @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");

        /**
         * 如果是高版本的mysql,请使用MySQLDialect
         */
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

        LocalSessionFactoryBean lsb=new LocalSessionFactoryBean();
        lsb.setDataSource(dataSource());
        /**
         * 设置实体类的位置
         */
        lsb.setPackagesToScan("com.example.myboot.pojo");
        lsb.setHibernateProperties(properties);

        return lsb;

    }



    @Bean
    public HibernateTemplate hibernateTemplate(){
        HibernateTemplate hibernateTemplate = new HibernateTemplate();
        hibernateTemplate.setSessionFactory(sessionFactory);
        return hibernateTemplate;
    }
}

 步骤 4  POJO类    

兑现承诺,给粉丝专门搞了一个SpringBoot2.x整合hibernate5的教程,附带视频+源码,免费下载!_第2张图片

package com.example.myboot.pojo;

import javax.persistence.*;

@Entity
@Table(name = "t_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "s_id")
    private Long id;
    @Column(name = "s_name")
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

这样在程序启动的时候,就会自动生成 t_user 表。

 步骤 5  新增和查询    
搞一个UserController,里面写新增和查询的方法:

兑现承诺,给粉丝专门搞了一个SpringBoot2.x整合hibernate5的教程,附带视频+源码,免费下载!_第3张图片

package com.example.myboot.controller;

import com.example.myboot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    HibernateTemplate hibernateTemplate;

    @RequestMapping("add")
    @Transactional(rollbackFor = Exception.class)
    public String add(User user){
        hibernateTemplate.saveOrUpdate(user);
        return "success";
    }

    @RequestMapping("findAll")
    public List findAll(User user){
        List users = hibernateTemplate.findByExample(user);
        return users;
    }
}

 步骤 6  源码下载    

见评论

视频教程

SpringBoot2.x 整合hibernate5,稀有教程_哔哩哔哩_bilibiliup主是一位java司机,喜欢撸代码,加微❤:javaxbfs , 可进技术交流,带你进技术交流群,一起小白翻身!公众号:【java小白翻身】我还写了一本书《JavaScript百炼成仙》,欢迎进群来撩,一起交流,共同进步~https://www.bilibili.com/video/BV1d44y1x79t?share_source=copy_web

你可能感兴趣的:(SpringBoot小白读物,hibernate,spring,boot,音视频)