server:
port: 8881
servlet:
context-path: /ex02
spring:
datasource:
name: druidDataSource
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
#自己的数据库Ip地址和数据库名,账号及密码
url: jdbc:mysql://localhost:3306/college?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC
username: root
password: 99150704
druid:
#监控统计拦截的过滤器 不加监控界面sql无法统计 wall用于防火墙
filters: stat,wall,log4j,config
#最大连接数
max-active: 100
#初始化大小
initial-size: 1
#获取连接最大等待时间
max-wait: 60000
min-idle: 1
#间隔多久检测一次需要关闭的空闲连接 毫秒
time-between-eviction-runs-millis: 60000
#连接在连接池中最小生存的时间,毫秒
min-evictable-idle-time-millis: 300000
validation-query: select ‘x‘
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-open-prepared-statements: 50
max-pool-prepared-statement-per-connection-size: 20
web-stat-filter:
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
url-pattern: /*
stat-view-servlet:
#白名单IP
allow: 127.0.0.1
#黑名单IP
deny: 192.168.0.106
#登录账号和密码
login-username: ex02
login-password: springboot
#启用重置数据功能
reset-enable: true
jpa:
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
show-sql: true
thymeleaf:
cache: false
suffix: .html
encoding: UTF-8
servlet:
content-type: text/html
mode: HTML5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=firestorm.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
package com.example.sw_ex02.entity;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "college", schema = "college", catalog = "")
public class CollegeEntity {
private String collegeCode;
private String collegeName;
private String deanName;
private String deanNumber;
@Id
@Column(name = "college_code")
public String getCollegeCode() {
return collegeCode;
}
//springboot解析器在解析json过程中出现问题,因字段名驼峰命名无法匹配字段名导致,进行注解添加
@JsonProperty(value = "college_code")
public void setCollegeCode(String collegeCode) {
this.collegeCode = collegeCode;
}
@Basic
@Column(name = "college_name")
public String getCollegeName() {
return collegeName;
}
@JsonProperty(value = "college_name")
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
@Basic
@Column(name = "dean_name")
public String getDeanName() {
return deanName;
}
@JsonProperty(value = "dean_name")
public void setDeanName(String deanName) {
this.deanName = deanName;
}
@Basic
@Column(name = "dean_number")
public String getDeanNumber() {
return deanNumber;
}
@JsonProperty(value = "dean_number")
public void setDeanNumber(String deanNumber) {
this.deanNumber = deanNumber;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CollegeEntity that = (CollegeEntity) o;
return Objects.equals(collegeCode, that.collegeCode) &&
Objects.equals(collegeName, that.collegeName) &&
Objects.equals(deanName, that.deanName) &&
Objects.equals(deanNumber, that.deanNumber);
}
@Override
public int hashCode() {
return Objects.hash(collegeCode, collegeName, deanName, deanNumber);
}
}
package com.example.sw_ex02.repository;
import com.example.sw_ex02.entity.CollegeEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CollegeRepository extends JpaRepository<CollegeEntity,String> {
}
package com.example.sw_ex02.service;
import com.example.sw_ex02.entity.CollegeEntity;
import com.example.sw_ex02.repository.CollegeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CollegeServiceImpl {
@Autowired
private CollegeRepository collegeRepository;
/***
* @description 查询所有用户
*/
public List<CollegeEntity> getCollegeList(){
return collegeRepository.findAll();
}
/***
* @description 查询单个用户
*/
public CollegeEntity getCollege(String code){
return collegeRepository.findById(code).get();
}
/***
* @description 创建用户
*/
public CollegeEntity insertCollege(CollegeEntity collegeEntity){
collegeEntity.setCollegeCode("");
collegeEntity.setCollegeName("");
collegeEntity.setDeanName("");
collegeEntity.setDeanNumber("");
return collegeRepository.save(collegeEntity);
}
/***
* @description 修改用户
*/
public CollegeEntity updateCollege(CollegeEntity collegeEntity){
CollegeEntity collegeEntity1=collegeRepository.findById(collegeEntity.getCollegeCode()).get();
if(collegeEntity1==null){
return null;
}
collegeEntity1.setCollegeName(collegeEntity.getCollegeName());
collegeEntity1.setDeanNumber(collegeEntity.getDeanNumber());
collegeEntity1.setDeanName(collegeEntity.getDeanName());
return collegeRepository.save(collegeEntity1);
}
/***
* @description 删除用户
*/
public boolean deleteCollege(String code){
try {
collegeRepository.deleteById(code);
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
}
package com.example.sw_ex02.controller;
import com.example.sw_ex02.entity.CollegeEntity;
import com.example.sw_ex02.service.CollegeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("college")
public class CollegeController {
@Autowired
private CollegeServiceImpl collegeService;
/***
* @description 查询所有用户
*/
@GetMapping
public List<CollegeEntity> getAllCollege(){
return collegeService.getCollegeList();
}
/***
* @description 查询单个用户
*/
@GetMapping("{code}")
public CollegeEntity getCollege(@PathVariable("code")String code){
return collegeService.getCollege(code);
}
/***
* @description 创建用户
*/
@PostMapping
public CollegeEntity insertCollege(@RequestBody CollegeEntity collegeEntity){
return collegeService.insertCollege(collegeEntity);
}
/***
* @description 修改用户
*/
@PatchMapping
public CollegeEntity updateCollege(@RequestBody CollegeEntity collegeEntity){
return collegeService.updateCollege(collegeEntity);
}
/***
* @description 删除用户
*/
@DeleteMapping("{code}")
public boolean deleteCollege(@PathVariable("code")String code){
collegeService.deleteCollege(code);
return true;
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>userList</title>
<!--thymeleaf表达式,th:href="@{/css/bootstrap.css}"@表示后面的是一个链接-->
<link rel="stylesheet" th:href="@{/css/bootstrap.css}"/>
</head>
<body class="container">
<br/>
<h1>用户列表</h1>
<br/><br/>
<div class="with:80%">
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>college_code</th>
<th>college_name</th>
<th>dean_name</th>
<th>dean_number</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<!--each来进行for循环求值-->
<tr th:each="collegeEntity : ${collegeEntites}">
<th scope="row" th:text="${collegeEntity.collegeCode}">1</th>
<td th:text="${collegeEntity.collegeCode}">neo</td>
<td th:text="${collegeEntity.collegeName}">neo</td>
<td th:text="${collegeEntity.deanName}">Otto</td>
<td th:text="${collegeEntity.deanNumber}">6</td>
<td><a th:href="‘/ex02/toEdit/‘+${collegeEntity.collegeCode}">edit</a></td>
<td><a th:href="‘/ex02/delete/‘+${collegeEntity.collegeCode}">delete</a></td>
</tr>
</tbody>
</table>
</div>
<div class="form-group">
<div class="col-sm-2 control-label">
<a href="/toAdd" th:href="@{/toAdd}" class="btn btn-info">add</a>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>college</title>
<!--利用thymeleaf表达式获取css路径,bootstrap给button提供样式-->
<link rel="stylesheet" th:href="@{/css/bootstrap.css}"/>
</head>
<body class="container">
<br/>
<h1>增加数据</h1>
<br/><br/>
<div class="with:80%">
<form class="form-horizontal" th:action="@{/edit}" method="post">
<div class="form-group">
<label for="collegeCode" class="col-sm-2 control-label">collegeCode</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="collegeCode" id="collegeCode" placeholder="collegeCode"/>
</div>
</div>
<div class="form-group">
<label for="collegeName" class="col-sm-2 control-label" >collegeName</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="collegeName" id="collegeName" placeholder="collegeName"/>
</div>
</div>
<div class="form-group">
<label for="deanName" class="col-sm-2 control-label">deanName</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="deanName" id="deanName" placeholder="deanName"/>
</div>
</div>
<div class="form-group">
<label for="deanNumber" class="col-sm-2 control-label">deanNumber</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="deanNumber" id="deanNumber" placeholder="deanNumber"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="Submit" class="btn btn-info" />
<input type="reset" value="Reset" class="btn btn-info" />
</div>
</div>
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>college</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}"/>
</head>
<body class="container">
<br/>
<h1>修改用户</h1>
<br/><br/>
<div class="with:80%">
<form class="form-horizontal" th:action="@{/edit}" th:object="${college}" method="post">
<input type="hidden" name="collegeCode" th:value="*{collegeCode}" />
<div class="form-group">
<label for="collegeName" class="col-sm-2 control-label" >collegeName</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="collegeName" id="collegeName" th:value="*{collegeName}" placeholder="collegeName"/>
</div>
</div>
<div class="form-group">
<label for="deanName" class="col-sm-2 control-label">deanName</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="deanName" id="deanName" th:value="*{deanName}" placeholder="deanName"/>
</div>
</div>
<div class="form-group">
<label for="deanNumber" class="col-sm-2 control-label">deanNumber</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="deanNumber" id="deanNumber" th:value="*{deanNumber}" placeholder="deanNumber"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="Submit" class="btn btn-info" />
<a href="/list" th:href="@{/list}" class="btn btn-info">Back</a>
</div>
</div>
</form>
</div>
</body>
</html>
package com.example.sw_ex02.controller;
import com.example.sw_ex02.entity.CollegeEntity;
import com.example.sw_ex02.service.CollegeServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class CollegeControllerThymeleaf {
@Resource
CollegeServiceImpl collegeService;
/***
* @description 将"/"自动重定向到"/list"
* @return 跳转到‘/list’页面
*/
@RequestMapping("/thymeleaf")
public String index(){
return "redirect:/list";
}
/***
* @description 将"/"自动重定向到"/list"
* @return 跳转到‘/list’页面
*/
@RequestMapping("/list")
public String list(Model model){
List<CollegeEntity> collegeEntities=collegeService.getCollegeList();
model.addAttribute("collegeEntites",collegeEntities);
return "college/list";
}
/*
* @Discription:跳转到增加用户页面
*/
@RequestMapping(value = "/toAdd")
public String toAdd(){
return "college/collegeAdd";
}
/*
* @param "/add"链接
* @return 重定向到/list页面
* @Discription: 保存实体到数据库,然后返回到list界面
*/
@RequestMapping(value = "/add")
public String add(CollegeEntity collegeEntity){
collegeService.save(collegeEntity);
return "redirect:/list";
}
/*
* @param /toEdit链接进入该程序
* @return 查询出数据以后,跳转到编辑界面
* @Discription:跳转到编辑页面对数据进行编辑
*/
@RequestMapping(value="/toEdit/{code}")
public String toEdit(Model model,@PathVariable("code")String code) {
CollegeEntity collegeEntity = collegeService.findCollegeById(code);
model.addAttribute("college", collegeEntity);
return "college/collegeEdit";
}
/*
* @param "/edit"链接进入该程序
* @return 重定向到"/list"链接
* @Discription:更新数据库中的数据以后跳转到list
*/
@RequestMapping(value = "/edit")
public String edit(CollegeEntity collegeEntity){
collegeService.edit(collegeEntity);
return "redirect:/list";
}
/*
* @param "/delete"链接进入该方法
* @return 重定向到"/list"
* @Discription: 删除某一个数据后定向到"/list"
*/
@RequestMapping(value="/delete/{code}")
public String delete(@PathVariable("code")String code){
collegeService.delete(code);
return "redirect:/list";
}
}
原文:https://www.cnblogs.com/fangzhiyou/p/14596709.html