当前位置:   article > 正文

Springboot 整合 hibernate 和 jpa_spring.jpa.properties.hibernate.dialect

spring.jpa.properties.hibernate.dialect

hibernate 和 jpa 的关系这里就不介绍了,你可以这么理解:hibernate用jpa的方式来实现

1.引入依赖  (jpa里面已经有了hiebrnate的依赖包)

  1. <!--整合hibernate要jpa-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-jpa</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>mysql</groupId>
  8. <artifactId>mysql-connector-java</artifactId>
  9. </dependency>

2.修改 application.properties

datasource的参数跟你整合mybatis一样,不过整合hibernate还得指定jpa的配置

  1. #datasource
  2. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  3. spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
  4. spring.datasource.username=root
  5. spring.datasource.password=123
  6. #jpa配置
  7. spring.jpa.database = MYSQL
  8. # Show or not log for each sql query
  9. spring.jpa.show-sql = true
  10. # Hibernate ddl auto (create, create-drop, update)
  11. spring.jpa.hibernate.ddl-auto = update
  12. # Naming strategy
  13. spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
  14. # stripped before adding them to the entity manager)
  15. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

3.创建实体类(这里用注解式开发,没有了.hbm.xml文件)

通过注解将表名和类名,字段名和属性名关联起来,还有指定主键生成策略

  1. package com.liqiye.springbootdemo.entity;
  2. import java.io.Serializable;
  3. import javax.persistence.Column;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.GenerationType;
  6. import javax.persistence.Id;
  7. import javax.persistence.MappedSuperclass;
  8. import javax.persistence.Entity;
  9. import javax.persistence.Table;
  10. /**
  11. * @author liqiye
  12. * @description hibernate的实体类
  13. * @date 2019/5/11
  14. */
  15. @Entity // @Entity标识一个实体类,任何Hibernate映射对象都要有这个注解
  16. @Table(name = "Person")
  17. public class Person implements Serializable{
  18. @Id
  19. // @GeneratedValue(strategy = GenerationType.AUTO) // 这个和默认一样是以Oracle的方式来实现自增主键,会自动生成hibernate_sequence表
  20. @GeneratedValue(strategy = GenerationType.IDENTITY)
  21. protected Long id;
  22. @Column(name = "name")
  23. protected String name;
  24. public Long getId() {
  25. return id;
  26. }
  27. public void setId(Long id) {
  28. this.id = id;
  29. }
  30. public String getName() {
  31. return name;
  32. }
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36. @Override
  37. public String toString() {
  38. return "Person{" +
  39. "id=" + id +
  40. ", name='" + name + '\'' +
  41. '}';
  42. }
  43. }

4.定义数据库操作接口

类似mybatis的mapper接口一样,包含操作数据库的方法,注入到service层使用,这里用的jpa,继承 JpaRepository 类

 JpaRepository 的子类已经实现了简单的增删改查的方法,我们也可以自定义HQL的方法

  1. package com.liqiye.springbootdemo.mapper;
  2. import com.liqiye.springbootdemo.entity.Person;
  3. import org.springframework.data.jpa.repository.JpaRepository;
  4. import org.springframework.data.jpa.repository.Query;
  5. import org.springframework.data.repository.query.Param;
  6. // hibernate的接口,springboot整合hibernate用的是jpa,接口要集成JpaRepository
  7. // 里面已经集成了几个方法,额外的查询需要我们手动添加方法HQL
  8. public interface PersonRepository extends JpaRepository<Person,Integer> {
  9. public Person findById(Long id); // 这个在集成的类的子类里面有明确的sql实现,不用再写SQL或者HQL,也可以不写,直接在service里用子类的
  10. public Person save(Person user); // 同上
  11. @Query(value = "SELECT p FROM Person p WHERE name=:name") // 这是HQL :xx 指传入参数,跟下面注解@Param对应
  12. // @Query(value = "SELECT * FROM Person WHERE name=?", nativeQuery = true) // 这是SQL nativeQuery为true代表使用SQL语言
  13. public Person findByName(@Param("name") String name);
  14. }

5.service 

  1. package com.liqiye.springbootdemo.service;
  2. import com.liqiye.springbootdemo.entity.Person;
  3. import com.liqiye.springbootdemo.mapper.PersonRepository;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. /**
  7. * @author liqiye
  8. * @description
  9. * @date 2019/5/11
  10. */
  11. @Service
  12. public class PersonService {
  13. @Autowired
  14. private PersonRepository personRepository;
  15. public Person findById(Long id){
  16. return personRepository.findById(id);
  17. }
  18. public Person findByName(String name){
  19. return personRepository.findByName(name);
  20. }
  21. public void save(Person person){
  22. personRepository.save(person);
  23. }
  24. }

5.controller

  1. package com.liqiye.springbootdemo.controller;
  2. import com.liqiye.springbootdemo.entity.Person;
  3. import com.liqiye.springbootdemo.service.PersonService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. /**
  9. * @author liqiye
  10. * @description
  11. * @date 2019/5/11
  12. */
  13. @Controller
  14. public class PersonController {
  15. @Autowired
  16. private PersonService personService;
  17. @RequestMapping("/findById")
  18. @ResponseBody
  19. public String findById(String id){
  20. return personService.findById(Long.parseLong(id)).toString();
  21. }
  22. @RequestMapping("/findByName")
  23. @ResponseBody
  24. public String findByName(String name){
  25. return personService.findByName(name).toString();
  26. }
  27. @RequestMapping("/save")
  28. @ResponseBody
  29. public String save(){
  30. Person person = new Person();
  31. person.setName("张三");
  32. personService.save(person);
  33. return "插入成功";
  34. }
  35. }

6.测试

运行项目,发现数据库自动创建了person 表

发起接口请求 http://localhost:8080/save 成功插入数据

发起接口请求 http://localhost:8080/findById?id=1 成功返回一条person数据

发起接口请求 http://localhost:8080/findByName?name=张三 成功返回一条person数据

 

总结:整体下来,跟整合mybatis的用法大同小异,只不过其中的具体实现有点差别。

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

闽ICP备14008679号