- 0. 代码下载
- 1.项目所需的依赖
- 2.配置文件
- 3.实体类
- 4. mapper
- 5.配置mapper.xml文件
- 6.service
- 7.serviceImpl
- 8.controller
代码下载链接:mybatis-plus-demo的代码
里面有数据库表的生成文件
注意可能需要新建一个blog-demo的数据库
2.配置文件org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.2 com.baomidou mybatis-plus-boot-starter 3.5.1 com.alibaba fastjson 1.2.70 org.springframework.boot spring-boot-devtools runtime true mysql mysql-connector-java runtime org.projectlombok lombok true
server:
port: 8082
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: mysql
url: jdbc:mysql://localhost:3306/blog-demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
mvc:
view:
prefix: /
suffix: .html
#logging:
# file:
# name: log/log.log
# level:
# root: info
# hhf:
# mybatisplusdemo : debug
mybatis-plus:
type-aliases-package: hhf.mybatisplusdemo.entity
mapper-locations: classpath:/hhf/mybatisplusdemo/mapper
@GetMapping("/save")
public void save(){
Student student = new Student(44, 232323, "小芳", 19, "女", LocalDate.now());
boolean save = iStudentService.save(student);
}
@GetMapping("/deleteById")
public void deleteById(@PathParam("id") Integer id){
boolean b = iStudentService.removeById(id);
}
@GetMapping("/updateById")
public void updateById(){
Student student = new Student(44, 3453454, "小明", 19, "女", LocalDate.now());
boolean b = iStudentService.updateById(student);
}
@GetMapping("/getById")
public String getById(@PathParam("id") Integer id){
Student student = iStudentService.getById(id);
System.out.println(student);
return student.toString();
}
@GetMapping("/getOneById")
public Student getOneById(@PathParam("id") Integer id){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id",id);
Student student = iStudentService.getOne(queryWrapper);
return student;
}
@GetMapping("/getOneByStuName")
public Student getOneByStuName(@PathParam("stuName") String stuName){
System.out.println("------------------------------------------");
// 设置查询条件
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("stu_name",stuName);
Student student = iStudentService.getOne(queryWrapper);
System.out.println("-----------------------------------------");
System.out.println(student.toString());
return student;
}
@GetMapping("/getStudentListBySex")
public List list(@PathParam("gender") String gender){
//
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("gender", gender);
List list = iStudentService.list(queryWrapper);
return list;
}
@GetMapping("/listStudentListByAge")
public List listStudentListByAge(@PathParam("age") Integer age){
//
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", age);
List list = iStudentService.list(queryWrapper);
return list;
}
}



