Springboot 整合Mybatis 实现增删改查(二)

续上篇:Springboot整合Mybatis的详细案例+图解+分析-CSDN博客

mapper层(StudentMapper)

    //通过id查询student方法
    Student searchStudentById(int id);
    //通过id删除student方法
    int deleteStudentById(int id);
    //通过id增加student方法
    int insertStudent(Student student);
    //通过id修改student方法
    int updateStudent(Student student);

service层(StudentService)

    //通过id查询student方法
    Student searchStudentById(int id);
    //通过id删除student方法
    int deleteStudentById(int id);
    //通过id增加student方法
    String insertStudent(Student student);
    //通过id修改student方法
    String updateStudent(Student student);

service接口层(StudentServiceImpl)

    //通过id查找
    @Override
    public Student searchStudentById(int id) {
        return studentMapper.searchStudentById(id);
    }
    //删除
    @Override
    public int deleteStudentById(int id) {
        int result1 = studentMapper.deleteStudentById(id);
        if (result1 == 1) {
            return 1;
        } else {
            return 0;
        }
    }
    //添加
    @Override
    public String insertStudent(Student student)
    {
        int result2=studentMapper.insertStudent(student);
        if(result2==1)
        {
            return "添加成功!";
        }
        else
        {
            return "添加失败!";
        }
    }
    //修改
    @Override
    public String updateStudent(Student student)
    {
        int result3=studentMapper.updateStudent(student);
        if(result3==1)
        {
            return "修改成功!";
        }
        else
        {
            return "修改失败!";
        }
    }

controller层(StudentController)

    @RequestMapping("/query/{id}")
    public String searchStudentById(@PathVariable("id") int id)
    {
        Student student=studentService.searchStudentById(id);
        return student.toString();
    }
    @RequestMapping("/delete/{id}")
    public int deleteStudentById(@PathVariable("id") int id)
    {
        return studentService.deleteStudentById(id);
    }
    @RequestMapping("/insert")
    public String insertStudent(Student student){
        return studentService.insertStudent(student);
    }
    @RequestMapping("/update")
    public String updateStudent(Student student)
    {
        return studentService.updateStudent(student);
    }

*mapper.xml(StudentMapper.xml)

    <resultMap id="BaseResultMap" type="com.example.demo.entity.Student">
        <result column="id" jdbcType="INTEGER" property="id"/>
        <result column="userName" jdbcType="VARCHAR" property="username"/>
        <result column="passWord" jdbcType="VARCHAR" property="password"/>
        <result column="sex" jdbcType="VARCHAR" property="sex"/>
        <result column="age" jdbcType="INTEGER" property="age"/>
    </resultMap>
    <select id="queryStudentList" resultType="Student">
        select * from student
    </select>
    <select id="searchStudentById" parameterType="java.lang.Integer" resultType="Student">
        select * from student where id=#{id}
    </select>
    <delete id="deleteStudentById" parameterType="java.lang.Integer">
        delete from student where id=#{id}
    </delete>
    <insert id="insertStudent">
        insert into student (id,username,password,sex,age) values (#{id},#{username},#{password},#{sex},#{age})
    </insert>
    <update id="updateStudent">
        update student set username=#{username},password=#{password},sex=#{sex},age=#{age} where id=#{id}
    </update>

运行结果

查询:http://localhost:8081/query/2

==>  Preparing: select * from student
==> Parameters: 
<==    Columns: id, username, password, sex, age
<==        Row: 1, tom, 123, nan, 22
<==        Row: 2, qq, 123456, nu, 23
<==        Row: 3, aa, 11111, nan, 20
<==        Row: 4, cd, 123456, nan, 21
<==      Total: 4

添加:http://localhost:8081/insert?id=5&username=dd&password=123456&sex=nan&age=25

==>  Preparing: select * from student
==> Parameters: 
<==    Columns: id, username, password, sex, age
<==        Row: 1, tom, 123, nan, 22
<==        Row: 2, qq, 123456, nu, 23
<==        Row: 3, aa, 11111, nan, 20
<==        Row: 4, cd, 123456, nan, 21
<==        Row: 5, dd, 123456, nan, 25
<==      Total: 5

删除:http://localhost:8081/delete/4

==> Parameters: 
<==    Columns: id, username, password, sex, age
<==        Row: 1, tom, 123, nan, 22
<==        Row: 2, qq, 123456, nu, 23
<==        Row: 3, aa, 11111, nan, 20
<==        Row: 5, dd, 123456, nan, 25
<==      Total: 4

修改:http://localhost:8081/update?id=5&username=LL&password=1256&sex=nu&age=20

 

==> Parameters: 
<==    Columns: id, username, password, sex, age
<==        Row: 1, tom, 123, nan, 22
<==        Row: 2, qq, 123456, nu, 23
<==        Row: 3, aa, 11111, nan, 20
<==        Row: 5, LL, 1256, nu, 20
<==      Total: 4

操作后的student表数据:

相关推荐

  1. SpringBoot实现增删

    2024-03-19 15:10:05       14 阅读
  2. MyBaties-增删

    2024-03-19 15:10:05       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-19 15:10:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-19 15:10:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-19 15:10:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-19 15:10:05       20 阅读

热门阅读

  1. Leetcode 389. Find the Difference

    2024-03-19 15:10:05       19 阅读
  2. Linux 常用指令

    2024-03-19 15:10:05       23 阅读
  3. 密码学——数字签名

    2024-03-19 15:10:05       20 阅读
  4. 基于ubuntu搭建qemu+risc-v虚拟机流程详细说明

    2024-03-19 15:10:05       17 阅读
  5. Python教程:Python安装目录说明

    2024-03-19 15:10:05       19 阅读
  6. Pillow教程:翻转图像

    2024-03-19 15:10:05       21 阅读
  7. 【无标题】

    2024-03-19 15:10:05       19 阅读