博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot--整合JPA
阅读量:2442 次
发布时间:2019-05-10

本文共 7608 字,大约阅读时间需要 25 分钟。

SpringBoot整合JPA

创建工程并导入依赖

4.0.0
pers.zhang
springboot_jpa
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.1.9

配置文件

spring:  datasource:    type: com.alibaba.druid.pool.DruidDataSource    url: jdbc:mysql:///chapter05    username: root    password: 123456  jpa:    show-sql: true #是否打印sql    database: mysql #JPA对应的数据库    hibernate:      ddl-auto: update #无表创建表

实体类

package pers.zhang.entity;import javax.persistence.*;/** * @author zhang * @date 2019/12/18 - 21:59 *//** * @Entity 表示该类是一个实体类,在项目启动时会根据该类自动生成一张表,表的名称即@Entity注解中name的值,如果不配置name,默认表名为类名。 * * 所有的实体类都要有主键,@Id注解表示该属性是一个主键,@GeneratedValue注解表示主键自动生成,strategy表示主键的生成策略。 * * 默认情况下,生成的表中字段名称就是实体类中的属性名称,通过@Column注解可以定制生成的字段名,name可以表示该属性对应的数据表中字段的名称, * nullable表示该字段是否非空 * * @Transient 注解表示在生成数据库中的表时,该属性被忽略,即不生成对应的字段。 */@Entity(name = "t_book")public class Book {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "book_name", nullable = false)//nullable是否非空 private String name; private String author; private Float price; @Transient private String description; public Integer getId() {
return id; } public void setId(Integer id) {
this.id = id; } public String getName() {
return name; } public void setName(String name) {
this.name = name; } public String getAuthor() {
return author; } public void setAuthor(String author) {
this.author = author; } public Float getPrice() {
return price; } public void setPrice(Float price) {
this.price = price; } public String getDescription() {
return description; } public void setDescription(String description) {
this.description = description; } @Override public String toString() {
return "Book{" + "id=" + id + ", name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", description='" + description + '\'' + '}'; }}

Service

package pers.zhang.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import org.springframework.stereotype.Service;import pers.zhang.dao.BookDao;import pers.zhang.entity.Book;import java.util.List;/** * @author zhang * @date 2019/12/18 - 22:18 */@Servicepublic class BookService {
@Autowired BookDao bookDao; public void addBook(Book book){
bookDao.save(book); } public Page
getBooksByPage(Pageable pageable){
return bookDao.findAll(pageable); } public List
getBooksByAuthorStartingWith(String author){
return bookDao.getBooksByAuthorStartingWith(author); } public List
getBooksByPriceGreaterThan(Float price){
return bookDao.getBooksByPriceGreaterThan(price); } public Book getMaxIdBook(){
return bookDao.getMaxIdBook(); } public List
getBookByIdAndAuthor(String author, Integer id){
return bookDao.getBookByIdAndAuthor(author, id); } public List
getBooksByIdandName(String name, Integer id){
return bookDao.getBooksByIdAndName(name, id); }}

Controller

package pers.zhang.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import pers.zhang.entity.Book;import pers.zhang.service.BookService;import java.util.List;/** * @author zhang * @date 2019/12/18 - 22:25 */@Controllerpublic class BookController {
@Autowired BookService bookService; @GetMapping("/findAll") public void findAll(){
PageRequest pageRequest = new PageRequest(2, 3); Page
page = bookService.getBooksByPage(pageRequest); System.out.println("总页数:" + page.getTotalPages()); System.out.println("总记录数:" + page.getTotalElements()); System.out.println("查询记录:" + page.getContent()); System.out.println("当前页数:" + (page.getNumber() + 1)); System.out.println("当前页记录数:" + page.getNumberOfElements()); System.out.println("每页记录数:" + page.getSize()); } @GetMapping("/search") public void search(){
List
bs1 = bookService.getBookByIdAndAuthor("鲁迅", 7); List
bs2 = bookService.getBooksByAuthorStartingWith("吴"); List
bs3 = bookService.getBooksByIdandName("西", 8); List
bs4 = bookService.getBooksByPriceGreaterThan(30f); Book b = bookService.getMaxIdBook(); System.out.println("bs1:" + bs1); System.out.println("bs2:" + bs2); System.out.println("bs3:" + bs3); System.out.println("bs4:" + bs4); System.out.println("b:" + b); } @GetMapping("/save") public void save(){
Book book = new Book(); book.setAuthor("鲁迅"); book.setName("呐喊"); book.setPrice(23f); bookService.addBook(book); }}

测试

访问前表数据如下

在这里插入图片描述

访问http://localhost:8080/findAll

控制台打印:

Hibernate: select book0_.id as id1_0_, book0_.author as author2_0_, book0_.book_name as book_nam3_0_, book0_.price as price4_0_ from t_book book0_ limit ?, ?总页数:3总记录数:7查询记录:[Book{id=7, name='故事新编', author='鲁迅', price=22.0, description='null'}]当前页数:3当前页记录数:1每页记录数:3

访问http://localhost:8080/save

后台打印:

Hibernate: insert into t_book (author, book_name, price) values (?, ?, ?)

数据表变为:

在这里插入图片描述

访问http://localhost:8080/search

控制台打印:

Hibernate: select book0_.id as id1_0_, book0_.author as author2_0_, book0_.book_name as book_nam3_0_, book0_.price as price4_0_ from t_book book0_ where book0_.id>? and book0_.author=?Hibernate: select book0_.id as id1_0_, book0_.author as author2_0_, book0_.book_name as book_nam3_0_, book0_.price as price4_0_ from t_book book0_ where book0_.author like ?Hibernate: select book0_.id as id1_0_, book0_.author as author2_0_, book0_.book_name as book_nam3_0_, book0_.price as price4_0_ from t_book book0_ where book0_.id
?Hibernate: select * from t_book where id = (select max(id) from t_book)bs1:[Book{id=8, name='呐喊', author='鲁迅', price=23.0, description='null'}]bs2:[Book{id=3, name='西游记', author='吴承恩', price=29.0, description='null'}]bs3:[Book{id=3, name='西游记', author='吴承恩', price=29.0, description='null'}]bs4:[Book{id=2, name='红楼梦', author='曹雪芹', price=35.0, description='null'}, Book{id=5, name='宋词选注', author='钱钟书', price=33.0, description='null'}]b:Book{id=8, name='呐喊', author='鲁迅', price=23.0, description='null'}

转载地址:http://xzpqb.baihongyu.com/

你可能感兴趣的文章
react hooks使用_使用React Hooks将React类组件转换为功能组件的5种方法
查看>>
使用Node.JS,React,Redux和Redux-saga Part3:身份验证构建Retrogames存档
查看>>
使用Node.JS,React,Redux和Redux-Saga Part2:Redux集成构建Retrogames存档。
查看>>
使用Selenium WebDriver测试Flask应用程序-第1部分
查看>>
python circle_与Python和Circle CI的持续集成
查看>>
react apollo_React with Apollo中的实时GraphQL UI更新。
查看>>
express react_在30分钟内使用Express.js构建博客并进行React
查看>>
构建express_15分钟内在Express中构建简单身份验证
查看>>
覆盖vue.js样式_使用Vue.js和Cloudinary在化身上覆盖眼镜/面罩
查看>>
js 迭代器 异步_异步迭代器和生成器入门
查看>>
vs 部署 azure_使用VS Code的Azure函数入门:零部署
查看>>
graphql-go_GraphQL-好与坏
查看>>
bitcoin_使用Node,Coinbase,Bitcoin和Okta构建自己的发票服务
查看>>
filestorm超级节点_带有节点的超级简单GraphQL
查看>>
vue避免使用mixins_用Mixins扩展Vue组件
查看>>
api curl_具有cURL和JavaScript的Airtable API教程
查看>>
代码打字速度_使用VueJS创建打字速度效果
查看>>
casl库控制用户权限_使用CASL在Vue中管理用户权限
查看>>
css 汉堡菜单_使用CSS构建变形汉堡包菜单
查看>>
javascript运算符_JavaScript一元运算符:简单而有用
查看>>