SpringBoot-MyBatis基础用法

前提一:SpringBoot整合MyBatis
前提二:SpringBoot返回值封装
1、Controller层

/**
 * 根据 ID 获取学生信息(参数与取值名称相同)
 * @param id
 * @return
 */
@GetMapping("/getById/{id}")
public Result getStudentById(@PathVariable int id) {
    return Result.success(service.getById(id));
}

/**
 * 根据姓名模糊查询学生信息(参数与取值名称不同)
 * @param name
 * @return
 */
@GetMapping("/getByName/{names}")
public Result getStudentByName(@PathVariable(value = "names", required = false) String name) {
    return Result.success(service.getByName(name));
}

/**
 * 添加学生信息(多个参数)
 * @param name
 * @return
 */
@GetMapping("/add/{name}/{age}")
public Result add(@PathVariable String name, @PathVariable int age) {
    int code = service.add(name, age);
    if (code == 1) {
        return Result.success("你已经成功添加了"+ name +"同学!");
    }
    return Result.error("添加错误!");
}

/**
 * 添加学生信息(地址相同无参数)
 * @return
 */
@GetMapping("/add")
public Result addStudent() {
    StudentPo po = new StudentPo("张伟", 18);
    int code = service.addStudent(po);
    if (code == 1) {
        return Result.success("你已经成功添加了"+ po.getName() +"同学!");
    }
    return Result.error("添加错误!");
}

/**
 * 根据 ID 删除学生
 * @param id
 * @return
 */
@GetMapping("/delete/{id}")
public Result deleteStudentById(@PathVariable int id) {
    int code = service.deleteById(id);
    if (code == 1) {
        return Result.success("你已经成功删除了ID为"+ id +"的学生!");
    }
   	return Result.error("删除失败!");
}

/**
 * 修改学生信息
 * @param po
 * @return
 */
@GetMapping("/update")
public Result update(StudentPo po) {
    int code = service.update(po);
    if (code == 1) {
        return Result.success("你已经成功修改了"+ po.getName() +"的信息!");
    }
    return Result.error("修改失败!");
}

2、Service层

/**
 * 根据 ID 获取学生信息
 * @param id
 * @return
 */
public StudentPo getById(int id) {
    return dao.getById(id);
}

/**
 * 根据 NAME 获取学生信息
 * @param name
 * @return
 */
public List<StudentPo> getByName(String name) {
    return dao.getByName(name);
}

/**
 * 添加学生
 * @param name
 * @param age
 * @return
 */
public int add(String name, int age) {
    return dao.add(name, age);
}

/**
 * 根据 ID 删除
 * @param id
 * @return
 */
public int deleteById(int id) {
    return dao.deleteById(id);
}

/**
 * 添加学生(传入对象)
 * @param po
 * @return
 */
public int addStudent(StudentPo po) {
    return dao.addStudent(po);
}

/**
 * 修改学生信息
 * @param po
 * @return
 */
public int update(StudentPo po) {
    StudentPo studentPo = dao.getById(po.getId());
    studentPo.setName(po.getName());
    studentPo.setAge(po.getAge());
    return dao.update(studentPo);
}

3、Dao层

// 根据 ID 获取学生信息
@Select("select * from student where id = #{id}")
StudentPo getById(int id);

// 根据 NAME 获取学生信息
@Select("select * from student where name like \"%\"#{name}\"%\"")
List<StudentPo> getByName(String name);

// 添加学生
@Insert("insert into student (name, age) values (#{data1}, #{data2})")
int add(@Param("data1") String name, @Param("data2") int age);

// 添加学生(传入对象)
@Insert("insert into student (name, age) values (#{name}, #{age})")
int addStudent(StudentPo po);

// 根据 ID 删除
@Delete("delete from student where id = #{id}")
int deleteById(int id);

// 修改学生信息
@Update("update student set name = #{name}, age = #{age} where id = #{id}")
int update(StudentPo studentPo);

4、测试:
http://localhost:8080/student/getById/1
请添加图片描述
http://localhost:8080/student/getByName/李四
请添加图片描述
http://localhost:8080/student/add/王五/22
请添加图片描述
http://localhost:8080/student/add
请添加图片描述
请添加图片描述
http://localhost:8080/student/delete/8
请添加图片描述
请添加图片描述
http://localhost:8080/student/update?id=1&name=灰太狼&age=22
请添加图片描述
请添加图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值