【Mybatis】学生表增删查改练习

在这里插入图片描述
在这里插入图片描述

Student

package com.example.mybatis01.entity;

import lombok.Data;

@Data       //使用注解@Data可以不用再写初始化函数和set,get了
public class Student {
    private int id;
    private String name;
    private int age = 0;
    private int teacherId;
    private int addressId;
}

StudentMapper

package com.example.mybatis01.mapper;

import com.example.mybatis01.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

@Mapper
public interface StudentMapper {



    public int addStudent(Student student);

    public void delStudentById(@Param("id") int id);

    public List<Student> listAll();

    public void updateStudent(Student student);



    public List<Student> findByName(@Param("name") String name);

    public Student findById(@Param("id") int id);

    public Map<String,Object>findMapById(@Param("id") int id);

    public List<Student> findByNameAndAge(@Param("name") String name,@Param("age") int age);

    //根据多个ID查询学生信息
    public List<Student> findbyIds(@Param("ids") List<Integer> ids);


}

StudentMapping

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:命名空间,是用于绑定Dao接口的,即面向接口编程,必须写为被绑定接口的全类名-->
<mapper namespace="com.example.mybatis01.mapper.StudentMapper">

    <resultMap id="StudentResultMap" type="com.example.mybatis01.entity.Student">
        <!--id 中的column 必须是主键字段-->
        <!--
        column : 表的字段,或者可以为查询语句中的别名字段
        jdbcType : 字段类型
        property : 映射pojo对象的主键属性"
        -->
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="age" jdbcType="INTEGER" property="age" />
        <result column="teacher_id" jdbcType="INTEGER" property="teacherId" />
        <result column="address_id" jdbcType="INTEGER" property="addressId" />

    </resultMap>


    <insert id="addStudent" parameterType="com.example.mybatis01.entity.Student" keyColumn="id" useGeneratedKeys="true">
        insert into student(name,age,teacher_id,address_id) values(#{name},#{age},#{teacherId},#{addressId})
    </insert>

    <!--根据ID删除学生-->
    <delete id="delStudentById" parameterType="int">
        delete from student where id = #{id}
    </delete>

    <update id="updateStudent" parameterType="com.example.mybatis01.entity.Student">
        update student set name = #{name},age = #{age},teacher_id = #{teacherId},address_id=#{addressId} where id = #{id}
    </update>

    <select id="listAll" resultType="com.example.mybatis01.entity.Student" >
        select id,name,age,teacher_id,address_id from student
    </select>


    <!--根据name查询学生-->
    <select id="findByName" parameterType="String" resultType="com.example.mybatis01.entity.Student">
        select * from student where name = #{name}
    </select>

    <!--根据id查询学生-->
    <select id="findById" parameterType="int" resultMap="StudentResultMap">
        select * from student where id = #{id};
    </select>

    <select id="findMapById" parameterType="int" resultType="map">
        select * from student where id = #{id};
    </select>


    <!--根据姓名、年龄查询学生-->
    <select id="findByNameAndAge" resultMap="StudentResultMap">
        select * from student
        <where>
            <if test="name != null and name != ''">
                and name like "%" #{name}"%"
            </if>
            <if test="age != null">
                and age = #{age}
            </if>
        </where>

    </select>


    <!--根据多个id查询学生-->
    <select id="findbyIds" parameterType="arraylist" resultMap="StudentResultMap">

        select * from student where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id,jdbcType=INTEGER}
        </foreach>

    </select>



</mapper>

StudentService

package com.example.mybatis01.service;

import com.example.mybatis01.entity.Student;
import com.example.mybatis01.mapper.StudentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class StudentService {

    @Resource
    private StudentMapper studentMapper;

    public int addStudent(Student student){
        int i = studentMapper.addStudent(student);
        return i;
    }

    public void delStudent(int id){
        studentMapper.delStudentById(id);
    }

    public List<Student> findAll(){
        return studentMapper.listAll();
    }

    public void updateStudent(Student student){
        studentMapper.updateStudent(student);
    }


    public List<Student> findByName(String name){
        return studentMapper.findByName(name);
    }

    public Student findById(int id){
        return studentMapper.findById(id);
    }

    public Map<String,Object> findMapById(int id){
        return studentMapper.findMapById(id);
    }

    public List<Student> findByNameAndAge(String name,int age){
        return studentMapper.findByNameAndAge(name,age);
    }

    public List<Student> findByIds(List<Integer> ids){
        return studentMapper.findbyIds(ids);
    }
}

StudentController

package com.example.mybatis01.controller;

import com.example.mybatis01.entity.Student;
import com.example.mybatis01.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j

@RequestMapping(value ="/student")
@Api(value="学生接口",tags = "学生管理相关的接口")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping(value="/add",method = RequestMethod.POST)
    @ApiOperation(value="新增一个学生",notes="好像爱这个世界")
    public boolean addStudent(Student student){
        log.info("开始新增...{}",student);
        studentService.addStudent(student);
        return true;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除一个学生",notes="随着波浪一浮一沉")
    public boolean deleteStudent(@RequestParam(value = "id",required = true) int id){
        log.info("开始删除...id={}",id);
        studentService.delStudent(id);
        return true;
    }

    @RequestMapping(value = "/select",method = RequestMethod.GET)
    @ApiOperation(value="查询所有学生",notes = "在阳光下打盹")
    public List<Student> selectStudent(){
        log.info("开始查询...");
        List<Student> students = studentService.findAll();

        return studentService.findAll();
    }

    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @ApiOperation(value="修改学会信息",notes = "今天就像是昨天")
    public boolean updateStudent(Student student){
        log.info("开始更新...{}",student);
        studentService.updateStudent(student);
        return true;
    }

    @RequestMapping(value = "/findByName",method = RequestMethod.GET)
    @ApiOperation(value = "根据姓名查询学生")
    public List<Student> findByName(@RequestParam("name") String name){
        return studentService.findByName(name);
    }

    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询学生")
    public Student findById(@RequestParam("id") int id){
        return studentService.findById(id);
    }


    @RequestMapping(value="/findMapById",method = RequestMethod.GET)
    @ApiOperation(value="根据id返回一个Map<String,Object>")
    public Map<String,Object> findMapById(@RequestParam("id") int id){
        return studentService.findMapById(id);
    }

    @RequestMapping(value = "/findByNameAndAge",method = RequestMethod.GET)
    @ApiOperation(value = "根据姓名,年龄查询学生")
    public List<Student> findByNameAndAge(@RequestParam("name") String name,@RequestParam("age") int age){
        return studentService.findByNameAndAge(name,age);
    }

    @RequestMapping(value = "/findByIds",method = RequestMethod.GET)
    @ApiOperation(value = "根据多个id查询学生")
    public List<Student> findByIds(@RequestParam("ids") List<Integer> ids){
        return studentService.findByIds(ids);
    }

在这里插入图片描述
创建数据库表:persons

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值