SpringBoot版本选择2.7.15
勾选相关的选项,并点击Create
项目创建完成
io.springfox springfox-boot-starter 3.0.0 com.mybatis-flex mybatis-flex-spring-boot-starter 1.6.4 com.zaxxer HikariCP
添加之后并且刷新依赖
然后在mysql的依赖处加入版本号(8.0.33),并刷新依赖
不加的话在运行过程中可能会报错
将Springboot的版本更改为2.5.0,并刷新依赖
实体类的属性有: id name gender garde score
package com.example.entity;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@Table("student")
public class Student {
@Id(keyType=KeyType.Auto)
private long id;
private String name;
private String gender;
private String grade;
private int score;
}
1. 加入相关的注解
2.主键要自增
3.@Id(keyType = KeyType.Auto)书写的时候需要注意第一个k是小写的,第二个k是大写
4. @Table("student")中的student即为我们创建的数据表的表名
创建表之后,并加入了 一条学生信息进去
其中com.example.mapper为mapper包文件所在的路径4
package com.example.service.Impl; import com.example.entity.Student; import com.example.mapper.StudentMapper; import com.example.service.IStudentService; import com.mybatisflex.spring.service.impl.ServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class IStudentServiceImpl extends ServiceImplimplements IStudentService { @Autowired private StudentMapper studentMapper; }
添加@Service注解
package com.example.controller; import com.example.service.IStudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/student") public class StudentController { @Autowired private IStudentService studentService; }
添加RestController和RequestMapping两个注解
package com.example.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; @Data @AllArgsConstructor @NoArgsConstructor public class RespBean { private long code; private String message; private Object object; public RespBean success(String message){ return new RespBean(200,message,null); } public RespBean success(String message,Object object){ return new RespBean(200,message,object); } public RespBean error(String message){ return new RespBean(500,message,null); } public RespBean error(String message,Object object){ return new RespBean(500,message,object); } }
添加注解@Data @AllArgsConstructor @NoArgsConstructor
端口如果不配,则默认为8080
在控制类(studentController)中写调用sevice层的接口的方法
方法名(selectAllStudents) 方法名可自行定义
@GetMapping("/selectAllStudents") public ListselectAllStudents(Student student){ return studentService.selectAllStudents(student); }
注解:
@GetMapping("/selectAllStudents")
在service里写selectAllStudents 方法
ListselectAllStudents(Student student);
在实现类(IStudentServiceImpl)写实现该方法
@Override public ListselectAllStudents(Student student) { return studentMapper.selectAll(); } }
访问地址:http://localhost:8084/swagger-ui/index.html#/
在控制类(studentController)中写调用sevice层的接口的方法
方法名(addStudents)
@PostMapping("addStudents") public RespBean addStudents(Student student ){ return studentService.addStudents(student); }
注解:
@GetMapping("/selectAllStudents")
在service里写addStudents 方法
RespBean addStudents(Student student);
在实现类(IStudentServiceImpl)写实现该方法
@Override public RespBean addStudents(Student student){ studentMapper.insert(student); long id =student.getId(); Student result=studentMapper.selectOneById(id); return RespBean.success("添加成功"+result); } }
点击Try it out 并根据属性输入数据
id可以不用输入
然后去数据库student表查看
删除的话我们可以根据id删除,可以根据name删除
在控制类(studentController)中写调用sevice层的接口的方法
方法名: deleteStudentById
@DeleteMapping("/deleteStudentById") public RespBean deleteStudentById(int id){ return studentService.deleteStudentById(id); }
注解:
@DeleteMapping("/deleteStudentById")
在service里写deleteStudentById 方法
RespBean deleteStudentById(int id);
在实现类(IStudentServiceImpl)写实现该方法
@Override public RespBean deleteStudentById(int id){ QueryWrapper queryWrapper=QueryWrapper.create() .select() .from("student") .where(STUDENT.ID.eq(id)); Student student=studentMapper.selectOneByQuery(queryWrapper); String username=student.getName(); studentMapper.deleteById(id); return RespBean.success(username+"删除成功!"); } }
当然如果我们删除的这个学生他不存在
在实现类添加if语句
例如:
在控制类(studentController)中写调用sevice层的接口的方法
方法名: deleteStudentByName
@DeleteMapping("/ deleteStudentByName") public RespBean deleteStudentByName(String name){ return studentService. deleteStudentByName(name); }
在service里写deleteStudentByIName 方法
在实现类(IStudentServiceImpl)写实现该方法
public RespBean deleteStudentByName(String name){ QueryWrapper queryWrapper= QueryWrapper.create() .select() .from("student") .where(STUDENT.NAME.eq(name)); Student student=studentMapper.selectOneByQuery(queryWrapper); if(student==null) { return RespBean.error("该学生不存在!"); } String username=student.getName(); studentMapper.deleteByQuery(queryWrapper); return RespBean.success(username+"删除成功"); }
此时student表的学生已经被我删除完了
在控制类(studentController)中写调用sevice层的接口的方法
方法名: updateStudents
@PostMapping("/updateStudents") public RespBean updateStudents(Student student){ return studentService.updateStudents(student); }
注解:
@PostMapping("/updateMapping")
在service里写updateStudents 方法
RespBean updateStudents(Student student);
在实现类(IStudentServiceImpl)写实现该方法
@Override public RespBean updateStudents(Student student){ QueryWrapper queryWrapper=QueryWrapper.create() .select() .from("student") .where(STUDENT.ID.eq(student.getId())); studentMapper.update(student); return RespBean.success("修改成功"); }
假如我们把年级更改为高三,分数改为100
这里的id是必填选项,且id=?为你想要更改的对象,其他填选项为你想要更改的内容
我们再去查询该表