spring.jpa.properties.hibernate.hbm2ddl.auto=
有四种配置方式,分别如下:

是hibernate的配置属性,其主要作用是:自动创建、更新、验证数据库表结构。该参数的几种配置如下:

  • create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
  • create-drop:每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
  • update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会。
  • validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。

在上面配置中要注意的是“spring.jpa.hibernate.ddl-auto”这个属性,它有五个value值选着分别是:
1. none 永远以数据表字段为准,不做任何修改
2. validate 加载hibernate时,验证创建数据库表结构,会和数据库中的表进行比较,不会创建新表,但是会插入新值
3. create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因
4. create-drop 加载hibernate时创建,退出是删除表结构
5. update 加载hibernate自动更新数据库结构

一个Sample,第一张图看下目录结构

JPA依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

重点看下Dao包下的BookResposibility,自带的API,都不需要具体写实现

package com.example.demo.dao;

import com.example.demo.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; public interface BookRepository extends JpaRepository<Book, Long> { List<Book> findByAuthor(String author); List<Book> findAll(); List<Book> findByName(String name); Book findById(long id); @Transactional
@Modifying
@Query("update Book set name = ?1 where id = ?2")
int updateBookName(String name, long id); }

  

还有我的Controller文件,主要的新增,查询,查询列表,更新操作  CRUD

package com.example.demo.controller;

import com.example.demo.entity.Book;
import com.example.demo.entity.BookPo;
import com.example.demo.dao.BookRepository;
import com.example.demo.response.ResVoid;
import com.example.demo.response.Response;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; @RestController
public class BookController { @Value("${uid}")
String uid; @Autowired
private BookRepository bookRepository; @ApiOperation(value = "新增书籍信息", notes = "新增书籍信息")
@ApiImplicitParam(dataType = "BookPo", name = "bookpo", value = "新增书籍信息")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ResVoid save(@RequestBody BookPo bookpo) {
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
String date = sdf.format(dt);
String bookname = bookpo.getName();
String bookauthor = bookpo.getAuthor();
int booknumber = bookpo.getNumber();
float bookprice = bookpo.getPrice(); bookRepository.save(new Book(bookname, bookauthor, date, booknumber, bookprice, date, date, uid, uid));
ResVoid re = new ResVoid();
re.setCode(200);
re.setMessage("Success");
return re; //bookRepository.save(new Book("书名11", "作者", "上架时间", 100, 23, "创建时间", "更新时间", "创建uid", "更新uid"));
//bookRepository.save(new Book("书名12", "作者", "20190504", 100, 23, "20190504", date, "3", "4"));
//bookRepository.save(new Book("西游记10", "吴承恩", "20190504", 100, 20, "20190504", date, "2", "3"));
} @ApiOperation(value = "根据作者名字或者笔名查找书籍", notes = "根据作者名字或者笔名查找书籍")
@ApiImplicitParam(dataType = "String", name = "author", value = "根据作者名字或者笔名查找书籍", paramType = "path",required = true)
@RequestMapping(value = "/findByAuthor/{author}", method = RequestMethod.GET)
public Response findByAuthor(@PathVariable("author") String author) {
List<Book> result = bookRepository.findByAuthor(author);
System.out.println("一共有多少条呢----" + result.size());
for (Book book : result) {
System.out.println(book);
}
System.out.println("================================");
Response re = new Response();
re.setCode(200);
re.setMessage("Success");
re.setSize(result.size());
re.setObject(result);
return re;
} @ApiOperation(value = "根据书籍名称进行找书", notes = "根据书籍名称进行找书")
@ApiImplicitParam(dataType = "String", name = "name", value = "根据书籍名称进行找书", paramType = "path",required = true)
@RequestMapping(value = "/findByName/{name}", method = RequestMethod.GET)
public Response findByName(@PathVariable String name) {
List<Book> result = bookRepository.findByName(name); Response re = new Response();
re.setCode(200);
re.setMessage("Success");
re.setSize(result.size());
re.setObject(result);
return re;
} @ApiOperation(value = "更新书籍信息", notes = "更新书籍信息")
@ApiImplicitParam(dataType = "Book", name = "book", value = "更新书籍信息", required = true)
@RequestMapping(value = "/updateBookName", method = RequestMethod.POST)
public Response updateBookName(@RequestBody Book book) { int count = bookRepository.updateBookName("三国演义", 2);
System.out.println(count); List<Book> result = bookRepository.findAll(); Response re = new Response();
re.setCode(200);
re.setMessage("Success");
re.setSize(result.size());
re.setObject(result);
return re; } @ApiOperation(value = "更新书籍信息", notes = "更新书籍信息")
@ApiImplicitParam(dataType = "Book", name = "book", value = "更新书籍信息", required = true)
@RequestMapping(value = "/updateBook", method = RequestMethod.POST)
public Response updateBook(@RequestBody Book book) { Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
String date = sdf.format(dt); Book book_orignial = bookRepository.findById(book.getId()); book.setOntime(book_orignial.getOntime());
book.setCreatuid(book_orignial.getCreatuid());
book.setCreatedt(book_orignial.getCreatedt());
book.setUpdatedt(date);
book.setUpdateuid(uid); bookRepository.save(book); List<Book> result = bookRepository.findAll(); Response re = new Response();
re.setCode(200);
re.setMessage("Success");
re.setSize(result.size());
re.setObject(result);
return re; }
}

  

Spring boot 中Hibernate 使用的更多相关文章

  1. Spring Boot中的事务管理

    原文  http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...

  2. 在Spring Boot中使用数据缓存

    春节就要到了,在回家之前要赶快把今年欠下的技术债还清.so,今天继续.Spring Boot前面已经预热了n篇博客了,今天我们来继续看如何在Spring Boot中解决数据缓存问题.本篇博客是以初识在 ...

  3. 在Spring Boot中使用数据库事务

    我们在前面已经分别介绍了如何在Spring Boot中使用JPA(初识在Spring Boot中使用JPA)以及如何在Spring Boot中输出REST资源(在Spring Boot中输出REST资 ...

  4. 在Spring Boot中输出REST资源

    前面我们我们已经看了Spring Boot中的很多知识点了,也见识到Spring Boot带给我们的各种便利了,今天我们来看看针对在Spring Boot中输出REST资源这一需求,Spring Bo ...

  5. 初识在Spring Boot中使用JPA

    前面关于Spring Boot的文章已经介绍了很多了,但是一直都没有涉及到数据库的操作问题,数据库操作当然也是我们在开发中无法回避的问题,那么今天我们就来看看Spring Boot给我们提供了哪些疯狂 ...

  6. Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题

      (转载)Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题   这几天在用spring boot开发项目, 在开发的过程中遇到一个问题hibernate在执 ...

  7. spring boot中的约定优于配置

    Spring Boot并不是一个全新的框架,而是将已有的Spring组件整合起来. Spring Boot可以说是遵循约定优于配置这个理念产生的.它的特点是简单.快速和便捷. 既然遵循约定优于配置,则 ...

  8. 学习Spring Boot:(二十三)Spring Boot 中使用 Docker

    前言 简单的学习下怎么在 Spring Boot 中使用 Docker 进行构建,发布一个镜像,现在我们通过远程的 docker api 构建镜像,运行容器,发布镜像等操作. 这里只介绍两种方式: 远 ...

  9. Spring Boot中Starter是什么

    比如我们要在Spring Boot中引入Web MVC的支持时,我们通常会引入这个模块spring-boot-starter-web,而这个模块如果解压包出来会发现里面什么都没有,只定义了一些POM依 ...

随机推荐

  1. [LeetCode] 301. Remove Invalid Parentheses_Hard tag:BFS

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  2. [LeetCode] 200. Number of Islands_ Medium tag: BFS

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  3. [LeetCode] questions conclustion_Path in Tree

    Path in Tree: [LeetCode] 112. Path Sum_Easy tag: DFS       input: root, target,   return True if exi ...

  4. 训练/验证/测试集设置;偏差/方差;high bias/variance;正则化;为什么正则化可以减小过拟合

    1. 训练.验证.测试集 对于一个需要解决的问题的样本数据,在建立模型的过程中,我们会将问题的data划分为以下几个部分: 训练集(train set):用训练集对算法或模型进行训练过程: 验证集(d ...

  5. Python Pandas找到缺失值的位置

    python pandas判断缺失值一般采用 isnull(),然而生成的却是所有数据的true/false矩阵,对于庞大的数据dataframe,很难一眼看出来哪个数据缺失,一共有多少个缺失数据,缺 ...

  6. Bagging Classifier+Regressor

    from sklearn.ensemble import BaggingRegressor Bagging通过引入随机化增大每个估计器之间的差异. 参数介绍: base_estimator:Objec ...

  7. LCD驱动

    LCD的驱动情况比较多. 对于一般的LCD,驱动方式有MCU,MPU,SPI等.其中MCU方式不需要输入clk,vsync,hsync等信号.完全可以通过异步来驱动,但是这样难以将屏 幕做到很大.MP ...

  8. Intermediate Python for Data Science learning 3 - Customization

    Customization from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotl ...

  9. mysql错误日志与通用日志

    错误日志 MySQL错误日志是记录MySQL 运行过程中较为严重的警告和错误信息,以及MySQL每次启动和关闭的详细信息. 1.错误日志路径查询 show variables like '%log_e ...

  10. html5设置全屏模式--开发游戏必备

    <!-- uc强制竖屏 --> <meta name="screen-orientation" content="portrait"> ...