导入pom相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
application.yml文件配置
server:
port: 80
servlet:
context-path: /
spring:
datasource:
#1.JDBC
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: root
password: 123456
druid:
#2.连接池配置
#初始化连接池的连接数量 大小,最小,最大
initial-size: 5
min-idle: 5
max-active: 20
#配置获取连接等待超时的时间
max-wait: 60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 30000
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: true
test-on-return: false
# 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filter:
stat:
merge-sql: true
slow-sql-millis: 5000
#3.基础监控配置
web-stat-filter:
enabled: true
url-pattern: /*
#设置不统计哪些URL
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-enable: true
session-stat-max-count: 100
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: true
#设置监控页面的登录名和密码
login-username: admin
login-password: admin
allow: 127.0.0.1
#jpa支持
jpa:
hibernate:
ddl-auto: update
show-sql: true
thymeleaf:
cache: false
#显示日志
logging:
level:
com.javaxl.springboot02.mapper: debug
自动建表相关代码
package com.javachz.springboot03.entity;
import lombok.Data;
import javax.persistence.*;
/**
* @author 52hz
* @site www.javachz.com
* @company xxx公司
* @create 2020-01-02 16:24
*/
@Data
@Entity
@Table(name = "t_springboot_book")
public class Book {
@Id
@GeneratedValue
private Integer bid;
@Column(length = 100)
private String banme;
@Column
private Float price;
}
运行测试
数据库自动建表截图
会创建一个序列以及t_springboot_book表
jpa值增删改查
package com.javachz.springboot03.dao;
import com.javachz.springboot03.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* @author 52hz
* @site www.javachz.com
* @company xxx公司
* @create 2020-01-02 16:47
*
* 只要继承JpaRepository,通常所用的增删查改方法都有
* * 第一个参数:操作的实体类
* * 第二个参数:实体类对应数据表的主键
*
* 要使用高级查询必须继承
* * org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>接口
*/
public interface JpaDao extends JpaRepository<Book,Integer>, JpaSpecificationExecutor<Book> {
}
controller层
package com.javachz.springboot03.controller;
import com.javachz.springboot03.dao.JpaDao;
import com.javachz.springboot03.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
/**
* @author 52hz
* @site www.javachz.com
* @company xxx公司
* @create 2020-01-02 16:52
*/
@RestController
@RequestMapping("/BookController")
public class BookController {
@Autowired
private JpaDao jpaDao;
@RequestMapping("/add")
public String add(Book book){
jpaDao.save(book);
return "success";
}
@RequestMapping("/edit")
public String edit(Book book){
jpaDao.save(book);
return "success";
}
@RequestMapping("/del")
public String del(Book book){
jpaDao.delete(book);
return "success";
}
@RequestMapping("/getOne")
public Book getOne(Integer bid){
return jpaDao.findById(bid).get();
}
@RequestMapping("getAll")
public List<Book> getAll(){
return jpaDao.findAll();
}
@RequestMapping("/jpa")
public List<Book> getCondition(Book book){
return jpaDao.findAll(new Specification<Book>() {
@Override
public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Predicate predicate=criteriaBuilder.conjunction();
if(book!=null){
if(null!=book.getBanme()&&!"".equals(book.getBanme())){
predicate.getExpressions().add(criteriaBuilder.like(root.get("bname"),"%"+book.getBanme()+"%"));
}
}
return predicate;
}
});
}
}
浏览器访问测试结果
getCondition结果
add结果
edit结果
del结果
getOne结果
getAll结果