赞
踩
hibernate 和 jpa 的关系这里就不介绍了,你可以这么理解:hibernate用jpa的方式来实现
- <!--整合hibernate要jpa-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
datasource的参数跟你整合mybatis一样,不过整合hibernate还得指定jpa的配置
- #datasource
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
- spring.datasource.username=root
- spring.datasource.password=123
-
- #jpa配置
- spring.jpa.database = MYSQL
- # Show or not log for each sql query
- spring.jpa.show-sql = true
- # Hibernate ddl auto (create, create-drop, update)
- spring.jpa.hibernate.ddl-auto = update
- # Naming strategy
- spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
- # stripped before adding them to the entity manager)
- spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
通过注解将表名和类名,字段名和属性名关联起来,还有指定主键生成策略
- package com.liqiye.springbootdemo.entity;
-
- import java.io.Serializable;
-
- import javax.persistence.Column;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.MappedSuperclass;
- import javax.persistence.Entity;
- import javax.persistence.Table;
-
- /**
- * @author liqiye
- * @description hibernate的实体类
- * @date 2019/5/11
- */
- @Entity // @Entity标识一个实体类,任何Hibernate映射对象都要有这个注解
- @Table(name = "Person")
- public class Person implements Serializable{
-
- @Id
- // @GeneratedValue(strategy = GenerationType.AUTO) // 这个和默认一样是以Oracle的方式来实现自增主键,会自动生成hibernate_sequence表
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- protected Long id;
- @Column(name = "name")
- protected 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;
- }
-
- @Override
- public String toString() {
- return "Person{" +
- "id=" + id +
- ", name='" + name + '\'' +
- '}';
- }
- }
类似mybatis的mapper接口一样,包含操作数据库的方法,注入到service层使用,这里用的jpa,继承 JpaRepository 类
JpaRepository 的子类已经实现了简单的增删改查的方法,我们也可以自定义HQL的方法
- package com.liqiye.springbootdemo.mapper;
-
- import com.liqiye.springbootdemo.entity.Person;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.data.jpa.repository.Query;
- import org.springframework.data.repository.query.Param;
-
- // hibernate的接口,springboot整合hibernate用的是jpa,接口要集成JpaRepository
- // 里面已经集成了几个方法,额外的查询需要我们手动添加方法HQL
-
- public interface PersonRepository extends JpaRepository<Person,Integer> {
- public Person findById(Long id); // 这个在集成的类的子类里面有明确的sql实现,不用再写SQL或者HQL,也可以不写,直接在service里用子类的
-
- public Person save(Person user); // 同上
-
- @Query(value = "SELECT p FROM Person p WHERE name=:name") // 这是HQL :xx 指传入参数,跟下面注解@Param对应
- // @Query(value = "SELECT * FROM Person WHERE name=?", nativeQuery = true) // 这是SQL nativeQuery为true代表使用SQL语言
- public Person findByName(@Param("name") String name);
-
- }
- package com.liqiye.springbootdemo.service;
-
- import com.liqiye.springbootdemo.entity.Person;
- import com.liqiye.springbootdemo.mapper.PersonRepository;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- /**
- * @author liqiye
- * @description
- * @date 2019/5/11
- */
- @Service
- public class PersonService {
-
- @Autowired
- private PersonRepository personRepository;
-
- public Person findById(Long id){
- return personRepository.findById(id);
- }
-
- public Person findByName(String name){
- return personRepository.findByName(name);
- }
-
- public void save(Person person){
- personRepository.save(person);
- }
-
- }
- package com.liqiye.springbootdemo.controller;
-
- import com.liqiye.springbootdemo.entity.Person;
- import com.liqiye.springbootdemo.service.PersonService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- /**
- * @author liqiye
- * @description
- * @date 2019/5/11
- */
- @Controller
- public class PersonController {
-
- @Autowired
- private PersonService personService;
-
- @RequestMapping("/findById")
- @ResponseBody
- public String findById(String id){
- return personService.findById(Long.parseLong(id)).toString();
- }
-
- @RequestMapping("/findByName")
- @ResponseBody
- public String findByName(String name){
- return personService.findByName(name).toString();
- }
-
- @RequestMapping("/save")
- @ResponseBody
- public String save(){
- Person person = new Person();
- person.setName("张三");
- personService.save(person);
- return "插入成功";
- }
-
- }
运行项目,发现数据库自动创建了person 表
发起接口请求 http://localhost:8080/save 成功插入数据
发起接口请求 http://localhost:8080/findById?id=1 成功返回一条person数据
发起接口请求 http://localhost:8080/findByName?name=张三 成功返回一条person数据
总结:整体下来,跟整合mybatis的用法大同小异,只不过其中的具体实现有点差别。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。