一、知识点
1、springboot之jpa支持
2、Springboot+bootstrap界面版之增删改查及图片上传
二、实践、操作
springboot之jpa支持
2.1 导入相关pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2.2 application.yml文件配置
server:
port: 80
servlet:
context-path: /
spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
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: 123
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: /druid/*
#设置不统计哪些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
#deny: 192.168.1.100
thymeleaf:
cache: false
# 解决图片上传大小限制问题,也可采取配置类
servlet:
multipart:
max-file-size: 20MB
max-request-size: 60MB
2.3 自动建表相关代码
package com.jy.springboot03.entity;
import lombok.Data;
import javax.persistence.*;
/**
* @author 蒋勇
* @site www.jy.com
* @company xxx集团
* @create 2019-12-01 20:23
*/
@Data
@Table(name="t_springboot_book_2019")
@Entity
public class Book {
@Id
@GeneratedValue
private Integer bid;
@Column(length = 100)
private String bname;
@Column
private Float price;
}
2.4 controller层
package com.jy.springboot03.controller;
import com.jy.springboot03.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author 蒋勇
* @site www.jy.com
* @company xxx集团
* @create 2019-12-01 20:23
*/
@RestController
public class BookController {
@Autowired
private JpaRepository 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) {
// 会出现懒加载问题:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
// return jpaDao.getOne(bid);
return (Book)jpaDao.findById(bid).get();
}
@RequestMapping("/getAll")
public List<Book> getAll() {
return jpaDao.findAll();
}
}
2.5 BookRepository
package com.jy.springboot03.repository;
import com.jy.springboot03.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author 蒋勇
* @site www.jy.com
* @company xxx集团
* @create 2019-12-01 20:23
*/
@Repository
public interface BookRepository extends JpaRepository<Book,Integer> {
}