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);
}