Spring boot 中Hibernate 使用
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; } }
------------------------- A little Progress a day makes you a big success... ----------------------------