当前位置:   article > 正文

SpringBoot+Hibernate搭建项目_springboot hibernate配置

springboot hibernate配置

        近期整理了对象关系映射对象的知识点,其中就涉及到了Hibernate相关的知识及内容,在此记录一下,以免忘记!PS:虽然不一定能用的上,有备无患吧。

一、搭建项目

1)打开开发工具,选择maven项目,默认使用下一步,设定项目名称直至完成。

在pom.xml中添加依赖,依赖如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.4</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>org.example</groupId>
  12. <artifactId>myhibernet</artifactId>
  13. <version>1.0-SNAPSHOT</version>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-web</artifactId>
  18. </dependency>
  19. <!--导入hibernate-->
  20. <dependency>
  21. <groupId>org.hibernate</groupId>
  22. <artifactId>hibernate-entitymanager</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.hibernate</groupId>
  26. <artifactId>hibernate-core</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>mysql</groupId>
  30. <artifactId>mysql-connector-java</artifactId>
  31. <scope>runtime</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-data-jpa</artifactId>
  36. </dependency>
  37. </dependencies>
  38. </project>

2)建立yml配置文件完成链接数据库:

  1. server:
  2. port: 80
  3. spring:
  4. datasource:
  5. url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT
  6. driver-class-name: com.mysql.cj.jdbc.Driver
  7. username: root
  8. password: xxxxxxxxx
  9. hikari:
  10. #池中最大链接数
  11. maximum-pool-size: 50
  12. #池中链接最长生命周期
  13. max-lifetime: 120000
  14. #连接允许在池中闲置的最长时间
  15. idle-timeout: 600000
  16. #等待来自池的连接的最大毫秒数
  17. connection-timeout: 30000
  18. #池中最小空闲链接数
  19. minimum-idle: 10

3)配置启动类

  1. package com.jl.nyxy;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class MyhibernetApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(MyhibernetApplication.class,args);
  8. }
  9. }

4)定义配置类

  1. @Configuration
  2. @EnableJpaRepositories
  3. @EnableTransactionManagement
  4. public class HibernateConfig {
  5. @Value("${spring.datasource.url}")
  6. private String jdbcUrl;
  7. @Value("${spring.datasource.username}")
  8. private String username;
  9. @Value("${spring.datasource.password}")
  10. private String password;
  11. @Value("${spring.datasource.driver-class-name}")
  12. private String driverClassName;
  13. @Autowired
  14. private SessionFactory sessionFactory;
  15. @Bean
  16. public DataSource dataSource(){
  17. return DataSourceBuilder.create()
  18. .url(jdbcUrl)
  19. .username(username)
  20. .password(password)
  21. .driverClassName(driverClassName)
  22. .build();
  23. }
  24. @Bean
  25. public LocalSessionFactoryBean localSessionFactoryBean(){
  26. Properties properties=new Properties();
  27. properties.setProperty("hibernate.show_sql", "true");
  28. properties.setProperty("hibernate.format_sql", "true");
  29. properties.setProperty("hibernate.hbm2ddl.auto", "update");
  30. /**
  31. * 如果是高版本的mysql,请使用MySQLDialect,低版本使用MySQL5Dialect
  32. */
  33. properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
  34. LocalSessionFactoryBean lsb=new LocalSessionFactoryBean();
  35. lsb.setDataSource(dataSource());
  36. /**
  37. * 设置实体类的位置
  38. */
  39. lsb.setPackagesToScan("com.jl.nyxy.pojo");
  40. lsb.setHibernateProperties(properties);
  41. return lsb;
  42. }
  43. @Bean
  44. public HibernateTemplate hibernateTemplate(){
  45. HibernateTemplate hibernateTemplate = new HibernateTemplate();
  46. hibernateTemplate.setSessionFactory(sessionFactory);
  47. return hibernateTemplate;
  48. }
  49. }

5)建立实体类

  1. import javax.persistence.*;
  2. @Entity
  3. @Table(name = "person")
  4. public class Person {
  5. @Id
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)
  7. @Column(name = "id")
  8. private int id;
  9. @Column(name = "personName")
  10. private String personName;
  11. @Column(name = "personEmail")
  12. private String personEmail;
  13. @Column(name = "personPhone")
  14. private String personPhone;
  15. @Column(name = "contactText")
  16. private String contactText;
  17. /**
  18. *省略了get和set
  19. **/
  20. }

6)配置控制类

  1. @RestController
  2. @RequestMapping("person")
  3. public class PersonController {
  4. @Autowired
  5. HibernateTemplate hibernateTemplate;
  6. @RequestMapping("findAll")
  7. public List<Person> findAll(Person person){
  8. List<Person> persons = hibernateTemplate.findByExample(person);
  9. return persons;
  10. }
  11. }

以上项目搭建完成,启动SpringBoot服务进行测试。

二、测试

访问接口,观察数据库中的数据库已经查出。

查出结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/815320
推荐阅读
相关标签
  

闽ICP备14008679号