Springboot版本是2.1.3.RELEASE
  
  1、依赖
  
  List-1.1
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-jdbc</artifactId>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-data-jpa</artifactId>
  
  </dependency>
  
  <dependency>
  
  <groupId>mysql</groupId>
  
  <artifactId>mysql-connector-java</artifactId>
  
  <version>5.1.47</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-web</artifactId>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-test</artifactId>
  
  <scope>test</scope>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.projectlombok</groupId>
  
  <artifactId>lombok</artifactId>
  
  <version>1.18.2</version>
  
  </dependency>
  
  2、项目整体结构
  
  图2.1
  
  bootstrap.yml内容如下,我们不需要手动创建数据库表,jpa/hiberate会自动会为我们创建的
  
  server:
  
  port: 9092
  
  servlet:
  
  context-path: /serviceB
  
  spring:
  
  application:
  
  name: cat-service-b
  
  datasource:
  
  type: com.zaxxer.hikari.HikariDataSource
  
  driver-class-name: com.mysql.jdbc.Driver
  
  username: root
  
  password: ******
  
  url: jdbc:mysql://pig-mysql:3306/cat?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
  
  database: mysql
  
  hibernate:
  
  ddl-auto: update
  
  naming:
  
  physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
  
  show-sql: true
  
  properties:
  
  hibernate:
  
  format_sql: true
  
  physical-strategy的值为org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy,那么当实体的属性为驼峰结构时,对应到数据库表的字段上,会用"_"隔开。
  
  3、代码详解
  
  List-3.1 BaseEntity的内容,所有的实体都要继承这个类
  
  import lombok.Data;
  
  import org.springframework.data.annotation.CreatedBy;
  
  import org.springframework.data.annotation.CreatedDate;
  
  import org.springframework.data.annotation.LastModifiedDate;
  
  import javax.persistence.GeneratedValue;
  
  import javax.persistence.GenerationType;
  
  import javax.persistence.Id;
  
  import javax.persistence.MappedSuperclass;
  
  import java.util.Date;
  
  @Data
  
  @MappedSuperclass
  
  public class BaseEntity {
  
  @Id
  
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  
  protected Integer id;
  
  /** 创建人 */
  
  @CreatedBy
  
  protected String creator;
  
  /** 创建时间 */
  
  @CreatedDate
  
  protected Date createDate;
  
  /** 更新时间,默认是当前时间 */
  
  @LastModifiedDate
  
  protected Date updateDate = new Date();
  
  /** 状态 0 表示删除, 1表示可操作 */
  
  protected Integer status = 1;
  
  public BaseEntity() {
  
  if (null == this.id && null == this.createDate) {
  
  this.createDate = new Date();
  
  }
  
  }
  
  }
  
  List-3.2 User的内容
  
  import lombok.Data;
  
  import lombok.ToString;
  
  import org.hibernate.annotations.SQLDelete;
  
  import org.hibernate.annotations.Where;
  
  import javax.persistence.Entity;
  
  import javax.persistence.Table;
  
  @Data
  
  @ToString
  
  @Entity
  
  @Table(name = "cat_user")
  
  @SQLDelete(sql = "update cat_user set status = 0 where id = ?")
  
  @Where(clause = "status <> 0")
  
  public class User extends BaseEntity{
  
  private String name;
  
  private Integer age;
  
  }
  
  List-3.3 UserRepository的内容
  
  import com.mjduan.project.catserviceb.entity.User;
  
  import org.springframework.data.repository.CrudRepository;
  
  public interface UserRepository extends CrudRepository<User, Integer> {
  
  }
  
  List-3.4 UserController的内容
  
  import com.mjduan.project.catserviceb.entity.User;
  
  import com.mjduan.project.catserviceb.repository.UserRepository;
  
  import lombok.extern.slf4j.Slf4j;
  
  import org.springframework.beans.factory.annotation.Autowired;
  
  import org.springframework.web.bind.annotation.GetMapping;
  
  import org.springframework.web.bind.annotation.PathVariable;
  
  import org.springframework.web.bind.annotation.RestController;
  
  import java.util.Optional;
  
  @Slf4j
  
  @RestController
  
  public class UserController {
  
  @Autowired
  
  private UserRepository userRepository;
  
  @GetMapping(value = www.dasheng178.com"/queryUser/{id}")
  
  public User queryUser(@PathVariable(value = "id") Integer id) {
  
  log.info("查询用户,id={}", id);
  
  Optional<User> optionalUser = userRepository.findById(id);
  
  User user = optionalUser.isPresent() ? optionalUser.get() : null;
  
  log.info("返回,{}", user);
  
  return user;
  
  }
  
  @GetMapping(value = "/saveUser/{name}")
  
  public User saveUser(@PathVariable(www.fengshen157.com/ value = "name") String name) {
  
  log.info("新增用户,name={}", name);
  
  User user = new User();
  
  user.setAge(20);
  
  user.setName(name);
  
  User save = userRepository.save(user);
  
  log.info("返回,{}", save);
  
  return save;
  
  }
  
  }
  
  4、验证
  
  在浏览器地址栏中输入
  
  List-4.1
  
  #保存name为Tom的用户
  
  http://localhost:9092/serviceB/saveUser/Tom
  
  #查询Id为1的用户
  
  http://localhost:9092/serviceB/queryUser/1
  
  一些思考:
  
  自动创建表结构,我们不需要手动去创建,我们修改实体的时候,系统会自动更新数据库中的表结构。
  
  所有实体都继承BaseEntity,那么每个实体对应的数据库表,在创建日期、更新日期等共有属性都同一了,这样在一定程度上便于代码理解和系统维护。
  
  5、Reference
  
  Springboot配置mysql连接的部分配置参考:https://github.com/pristinecore/springbootsample/blob/master/springbootsample/src/main/resources/database.properties
  
  格式化SQL输出的参考:https://www.yongshiyule178.com stackoverflow.com/questions/25720396/how-to-set-hibernate-format-sql-in-spring-boot

SpringBoot之使用jpa/hibernate的更多相关文章

  1. springboot 集成 jpa/hibernate

    pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  2. SpringBoot + Jpa(Hibernate) 架构基本配置

    1.基于springboot-1.4.0.RELEASE版本测试 2.springBoot + Hibernate + Druid + Mysql + servlet(jsp) 一.maven的pom ...

  3. javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful

    一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...

  4. 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用

    问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...

  5. [读书笔记] 四、SpringBoot中使用JPA 进行快速CRUD操作

    通过Spring提供的JPA Hibernate实现,进行快速CRUD操作的一个栗子~. 视图用到了SpringBoot推荐的thymeleaf来解析,数据库使用的Mysql,代码详细我会贴在下面文章 ...

  6. SpringBoot整合StringData JPA

    目录 SpringBoot整合StringData JPA application.yml User.class UserRepository.java UserController SpringBo ...

  7. SpringBoot整合SpringData JPA入门到入坟

    首先创建一个SpringBoot项目,目录结构如下: 在pom.xml中添加jpa依赖,其它所需依赖自行添加 <dependency> <groupId>org.springf ...

  8. Spring Boot + Jpa(Hibernate) 架构基本配置

    本文转载自:https://blog.csdn.net/javahighness/article/details/53055149 1.基于springboot-1.4.0.RELEASE版本测试 2 ...

  9. Springboot spring data jpa 多数据源的配置01

    Springboot spring data jpa 多数据源的配置 (说明:这只是引入了多个数据源,他们各自管理各自的事务,并没有实现统一的事务控制) 例: user数据库   global 数据库 ...

随机推荐

  1. [学习笔记]PCL使用心得

    最近开始做研究生毕设,有一部分因为没有什么好的思路,就把以前用过的PCL点云搬出来,重新用源码装了一遍PCL,一开始装的过程中没什么大问题,在后面用的时候碰到了很多小问题,特此记录. 1.PCL版本问 ...

  2. OpenGL学习笔记(1) 画一个三角形

    最近找实习有一丢丢蛋疼,沉迷鬼泣5,四周目通关,又不想写代码,写篇笔记复习一下,要好好学图形学啊 用OpenGL画一个三角形 项目的简介 记录一下跟着learnOpenGL学习的过程 笔记里的代码放在 ...

  3. vim文本编辑工具(全)

    VIM文本编辑工具 编辑模式 i    在当前字符前插入I   在光标所在的行首插入a 在当前字符后插入A 在光标所在行尾插入o 在当前行的下一行插入新的一行O 在当前行的上一行插入新的一行 s   ...

  4. 客户端传入数据的校验-RestController进阶

    使用Hibernate Validator进行数据校验 Bean Validation注解(需要加入相关依赖,在SpringBoot中可以直接使用,SpringBoot会帮我们直接加入) @Null ...

  5. Docker--删除容器实例和镜像

    一.删除容器实例 使用命令docker rm 容器ID或者容器名 1.docker ps -a查询已有的实例 [root@cxt data]# docker ps -a 2.docker rm 容器I ...

  6. jsp内置对象 转发与重定向的区别

    jsp 内置对象  转发与重定向的比较 重定向和转发有一个重要的不同:当使用转发时,JSP容器将使用一个内部的方法来调用目标页面,新的页面继续处理同一个请求,而浏览器将不会知道这个过程. 与之相反,重 ...

  7. AtCoder | ARC103 | 瞎讲报告

    目录 ARC 103 A.//// B.Robot Arms C.Tr/ee D.Distance Sums ARC 103 窝是传送门QwQ A.//// 题意 : 给你\(n\)(\(n\)为偶数 ...

  8. DockerCon2017前瞻 - Docker企业版体验

    DockerCon 2017将于四月17号在美国Austin召开.在去年DockerCon上,Docker公司一系列的发布吹响了进军企业市场的号角.今天,容器技术已经愈发成熟,被越来越多的企业所关注和 ...

  9. # 团队UML设计

    团队信息 学号 姓名 博客链接 124 王彬(组长) 点击这里 206 赵畅 点击这里 215 胡展瑞 点击这里 320 李恒达 点击这里 131 佘岳昕 点击这里 431 王源 点击这里 206 陈 ...

  10. oh my god 四则运算

    Week1地址:https://git.coding.net/leiqh549/four.git 需求分析: 1.一个生成n道四则运算的程序,要求数字在0-100间,运算符在3-5个之间且运算符至少包 ...