一、springboot之JPA支持
- 添加Pom依赖(记得降版本mysql)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</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: 123
druid:
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
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
filter:
stat:
merge-sql: true
slow-sql-millis: 5000
web-stat-filter:
enabled: true
url-pattern: /*
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:
hibernate:
ddl-auto: update
show-sql: true
- 创建entity包以及类进行自动建表
Student.java
package com.xfz.springboot03.entity;
import lombok.Data;
import javax.persistence.*;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2020-01-03 14:56
*/
@Data
@Entity
@Table(name = "t_springboot_student_2020")
public class Student {
@Id
@GeneratedValue
private Integer sid;
@Column
private String sname;
@Column
private String sex;
}
运行启动类
- 建表完毕后开始写后台代码
StudentDao (jpa值增删改查)
package com.xfz.springboot03.dao;
import com.xfz.springboot03.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2020-01-03 15:22
*
* 只要继承JpaRepository,通常所用的增删改查方法都有
* 第一个参数:操作的实体类
* 第二个参数:实体类对应的数据表的主键
*/
public interface StudentDao extends JpaRepository<Student,Integer> {
}
StudentController
package com.xfz.springboot03.controller;
import com.xfz.springboot03.dao.StudentDao;
import com.xfz.springboot03.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2020-01-03 15:25
*/
@RestController
public class StudentController {
@Autowired
private StudentDao studentDao;
/**
* 删除方法
* @param student
* @return
*/
@RequestMapping("/del")
public String del(Student student){
studentDao.delete(student);
return "success";
}
/**
* 增加方法
* @param student
* @return
*/
@RequestMapping("/add")
public String add(Student student){
studentDao.save(student);
return "success";
}
/**
* 修改方法
* @param student
* @return
*/
@RequestMapping("/edit")
public String edit(Student student){
studentDao.save(student);
return "success";
}
/**
* 获取单个方法
* @param
* @return
*/
@RequestMapping("/getOne")
public Student getOne(Integer sid){
return studentDao.findById(sid).get();
}
/**
* 获取全部方法
* @param
* @return
*/
@RequestMapping("/getAll")
public List<Student> getAll(){
return studentDao.findAll();
}
}
显示效果:
查询全部
增加,修改
新增修改用的是同一个方法,当id在表列表里没有时调用save方则会新增,但是当有这个id,改动其他数据时调用save方法则会保存修改
删除
二、springboot+bootstrap界面版之增删改查及图片上传
- 添加相关的pom依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
- 添加配置文件中的配置(层级关系要对齐)
thymeleaf:
cache: false
# 解决图片上传大小限制问题,也可采取配置类
servlet:
multipart:
max-file-size: 20MB
max-request-size: 60MB
- 在启动类中加注解Springboot05Application.java
package com.xfz.springboot03;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@SpringBootApp