1、SpringBoot项目创建好了之后,首先引入mysql驱动,mybatis plus,thymeleaf所需的驱动
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent2.5.5 com.example demo0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-devtoolstrue org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-webmysql mysql-connector-javaruntime com.alibaba druid1.1.20 com.baomidou mybatis-plus-boot-starter3.4.0 org.springframework.boot spring-boot-starter-thymeleafsrc/main/java *** org.springframework.boot spring-boot-maven-plugintrue
2、配置连接数据库属性,及mybatis plus配置
application.yml
server:
port: 8000
spring:
thymeleaf:
prefix: classpath:/templates/
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql:///test?characterEncoding=utf-8&serverTimezone=UTC
username: root //数据库账号
password: lichenxi2 //数据库密码
mybatis-plus:
mapper-locations: mapper
@PostMapping("/addAction")
public String addAction(@RequestParam(name = "name2") String name,
@RequestParam(name = "age2") String age,
@RequestParam(name = "address2") String address){
Student stu1 = new Student(0,name,address,age);
studentService.add(stu1);
return "redirect:/list";
}
@PostMapping("/addAction2")
@ResponseBody
public String addAction2(@RequestBody Map req){
Student stu1 = new Student(0, (String)req.get("name"),
(String)req.get("address"),
(String)req.get("age"));
studentService.add(stu1);
return "{"result":0,"msg":"ok"}";
}
@GetMapping("/update")
public String update(@RequestParam(name = "id") Integer id, Model model){
Student stu1 = studentService.findByID(id.intValue());
model.addAttribute("student",stu1);
return "update";
}
@PostMapping("/updateAction")
public String updateAction(Student student){
studentService.update(student);
return "redirect:/list";
}
@GetMapping("/delete")
public String delete(@RequestParam(name = "id") Integer id){
studentService.delete(id.intValue());
return "redirect:/list";
}
index.html(主页面)
Title
新增
| ID | 姓名 | 年龄 | 地址 | 操作 | |
|---|---|---|---|---|---|
| 更新 | 删除 |
add.html
Title
update.html
Title
演示
点击更新后的页面:
点击新增后的页面:
就这样了,做的比较简陋。



