如何实现通过前端请求访问的方式实现增删改呢?数据自备
1.创建项目
2.基础代码(因数据库信息而变化)
实体类Member.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Member {
private Integer id;
private String term;
private String username;
private String student_number;
private String gender;
private String qq;
private String phone;
private String mail;
private String join_time;
private String grade_and_class;
private String graduate_time;
private String current_location;
private String current_company;
private String post;
private String role;
private LocalDateTime update_time;
private Integer status;
private LocalDateTime create_time;
private Integer is_deleted = 0;
}
Result.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
private Object data;
public static Result success(Object data){
return new Result(data);
}
}
MemberMapper.java接口
@Mapper
public interface MemberMapper {
//代码
}
MemberContrller.java
@Slf4j
@RestController
@RequestMapping("/member")
public class MemberController {
//代码
}
MemberService.java(接口)
@Service
public interface MemberService {
//代码部分
}
MemberServiceImpl.java
@Service
public class MemberServcieImpl implements MemberService {
//代码部分
}
3.通过Apifox(相当于前端请求访)实现增删改查
1.查询所有成员的信息
mapper层:
@Select("select *from ds_user")
List<Member> list();
controller层:
@GetMapping /*此处的路径为公共路径,
所以在公共类上标上注解即可:@RequestMapping("/member")*/
public Result list(){
log.info("查询所有成员信息");
List<Member> memberList=memberService.list();
return Result.success(memberList);
}
service接口层:
/*
查询全部信息
*/
List<Member>list();
impl层:
//根据ID查询成员信息
@Override
public List<Member> list(){
return memberMapper.list();
}
Apifox GET请求:
终端日志输出:
2. 根据ID删除成员信息
mapper层:
/*
DeleteById
*/
@Delete("delete from ds_user where id=#{id}")
void deleteById(Integer id);
controller层:
/*
根据ID成员删除信息
*/
@DeleteMapping("/{id}")//请求时在路径后加上要删除的id即可
public Result delete(@PathVariable Integer id){
log.info("根据ID删除成员信息:{}",id);
memberService.delete(id);
return null;
}
service接口层:
/*
根据ID删除成员信息
*/
void delete(Integer id);
impl层:
//根据ID删除成员信息
@Override
public void delete(Integer id) {
memberMapper.deleteById(id);
}
Apifox DELETE请求:
终端日志输出:(数据库中id=141的成员信息被删除)
3.添加成员信息
mapper层:
@Insert("insert into ds_user(term, username, student_number, gender, qq, phone, mail, join_time, grade_and_class, graduate_time, current_location, current_company, post, role, update_time, status, create_time, is_deleted)values " +
"(#{term},#{username},#{student_number},#{gender},#{qq},#{phone},#{mail},#{join_time},#{grade_and_class},#{graduate_time},#{current_location},#{current_company},#{post},#{role},#{update_time},#{status},#{create_time},#{is_deleted})")
void insert(Member member);
controller层:
@PostMapping
public Result add(@RequestBody Member member){
log.info("添加成员信息:{}",member);
memberService.add(member);
return Result.success(member);
}
service接口层:
/*
新增成员信息
*/
void add(Member member);
impl层:
//添加成员信息
@Override
public void add(Member member) {
memberMapper.insert(member);
}
Apifox POST请求:(通过json格式传入参数值)
终端输出:
添加成功:
4.更新成员信息
mapper层:
@Update("update ds_user" +
" set term=#{term},username=#{username},student_number=#{student_number},gender=#{gender}," +
"qq=#{qq},phone=#{phone},mail=#{mail},join_time=#{join_time},grade_and_class=#{grade_and_class}," +
"graduate_time=#{graduate_time},current_location=#{current_location},current_company=#{current_company}," +
"post=#{post},role=#{role},update_time=#{update_time},status=#{status},create_time=#{create_time}," +
"is_deleted=#{is_deleted} " +
"where id=#{id}")
void update(Member member);
controller层:
@PutMapping
public Result update(@RequestBody Member member){
log.info("修改成员信息:{}",member);
memberService.update(member);
return Result.success(member);
}
service接口层:
/*
修改成员信息
*/
void update(Member member);
impl层:
/*
修改成员信息
*/
@Override
public void update(Member member) {
memberMapper.update(member);
}
Apifox PUT请求:
终端日志输出: