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. shell中与运算 cut切分行 if while综合在一起的一个例子

    前言: 公司要统计 treasury库hive表磁盘空间,写了个脚本,如下: 查询hive仓库表占用hdfs文件大小: hadoop fs -du -h  /user/hive/warehouse/t ...

  2. linux常用命令总结(含选项参数)

    • 用户切换 su              切换到root用户并不切换环境 su - root   切换到root用户并切换环境 su  redhat  切换到redhat不切换环境 • cd切换目 ...

  3. selenium的基本定位方式总结

    Selenium提供了8种定位方式. id name class name tag name link text partial link text xpath css selector 这8种定位方 ...

  4. python3.6环境中django2.0与xadmin0.6结合的后台管理

    1.xadmin简介 django的admin管理后台页面很简洁,对个人来说做后台管理非常简单:xadmin的比较admin优化界面,看着也舒服. xadmin界面效果如下: 2.xadmin安装 从 ...

  5. Mysql数据库的隔离级别

    Mysql数据库的隔离级别有四种 1.read umcommitted   读未提交(当前事务可以读取其他事务没提交的数据,会读取到脏数据) 2.read committed 读已提交(当前事务不能读 ...

  6. 软件工程-东北师大站-第十二次作业(PSP)

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图

  7. oracle和mysql对时间与字符串的转换

    1,oracle to_date(#{item.value},'YYYY-MM-DD hh24-mi-ss') to_char(CRERATE_TIME,'YYYY-MM-DD hh24-mi-ss' ...

  8. [BUAA软工]第零次博客作业---问题回答

    [BUAA软工]第0次博客作业 项目 内容 这个作业属于哪个课程 北航软工 这个作业的要求在哪里 第0次个人作业 我在这个课程的目标是 学习如何以团队的形式开发软件,提升个人软件开发能力 这个作业在哪 ...

  9. scanf() scanf_s() 区别

    写博原因:这几天由于小学期的缘故,接触到了好多C代码,在VS2013中编译的时候,遇到了如下问题: 错误 1 error C4996: 'scanf': This function or variab ...

  10. BETA随笔6/7

    前言 我们居然又冲刺了·六 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 过去两天完成了哪些任务 新方案代码比之前的更简单,但是对场景的要求相应变高了,已经实现,误差感人 代码 ...