一、单个参数
StudentParamsMapper
packagecn.cnki.ref.mapper;importcn.cnki.ref.pojo.Student;public interfaceStudentParamsMapper {/*** 根据name查询
*@paramname
*@return
*/
publicStudent getByName(String name);
}
View Code
StudentParamsMapper.xml
select * from student where id=#{param1} and name=#{param2}
View Code
StudentParamsController
packagecn.cnki.ref.controller;importcn.cnki.ref.mapper.StudentParamsMapper;importcn.cnki.ref.pojo.Student;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RestController;public interfaceStudentParamsController {
@RestControllerpublic classStudentParamsMapper {
@Autowiredprivatecn.cnki.ref.mapper.StudentParamsMapper StudentParamsMapper;
@GetMapping("/studentparams/{name}")public Student selectCourseById(@PathVariable("name") String name) {
Student student=StudentParamsMapper.getByName(name);returnstudent;
}
}
}
View Code
测试
http://localhost:8080/studentparams/王五
二、多个参数
1.根据参数key值获取,获取规则为param1,param2,param3.........:
StudentParamsMapper
packagecn.cnki.ref.mapper;importcn.cnki.ref.pojo.Student;public interfaceStudentParamsMapper {/*** 根据用户名和id同时查询
*@paramid
*@paramname
*@return
*/
publicStudent getStudentByIdAndName(Integer id,String name);
}
View Code
StudentParamsMapper.xml
select * from student where id=#{0} and name=#{1}
View Code
StudentParamsController
packagecn.cnki.ref.controller;importcn.cnki.ref.mapper.StudentParamsMapper;importcn.cnki.ref.pojo.Student;importorg.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;public interfaceStudentParamsController {
@RestControllerpublic classStudentParamsMapper {
@RequestMapping("/getStudentByIdAndName")public Student getStudentByIdAndName(@RequestParam("id") Integer id, @RequestParam("name") String name) {
Student student=StudentParamsMapper.getStudentByIdAndName(id,name);returnstudent;
}
}
}
View Code
测试
http://localhost:8080/getStudentByIdAndName?id=1&name=张三
2.绑定参数名
StudentParamsMapper
select * from student where name=#{name} and id=#{id}
View Code
StudentParamsMapper.xml
packagecn.cnki.ref.mapper;importcn.cnki.ref.pojo.Student;importorg.apache.ibatis.annotations.Param;public interfaceStudentParamsMapper {/*** 根据用户名和id同时查询
*@paramid
*@paramname
*@return
*/
public Student getStudentByIdAndNameParam(@Param("id")Integer id, @Param("name")String name);
}
View Code
StudentParamsController
packagecn.cnki.ref.controller;importcn.cnki.ref.mapper.StudentParamsMapper;importcn.cnki.ref.pojo.Student;importorg.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;public interfaceStudentParamsController {
@RestControllerpublic classStudentParamsMapper {
@Autowiredprivatecn.cnki.ref.mapper.StudentParamsMapper StudentParamsMapper;
@RequestMapping("/getStudentByIdAndNameParam")public Student getStudentByIdAndNameParam(@RequestParam("id") Integer id, @RequestParam("name") String name) {
Student student=StudentParamsMapper.getStudentByIdAndName(id,name);returnstudent;
}
}
}
View Code
测试
http://localhost:8080/getStudentByIdAndNameParam?id=1&name=张三
3.封装实体参数
StudentParamsMapper
packagecn.cnki.ref.mapper;importcn.cnki.ref.pojo.Student;importorg.apache.ibatis.annotations.Param;public interfaceStudentParamsMapper {/*** 根据用户名和id同时查询
*@paramid
*@paramname
*@return
*/
publicStudent getStudentByIdAndNameByObjectParam(Student student);
}
View Code
StudentParamsMapper.xml
select * from student where name=#{name} and id=#{id}
View Code
StudentParamsController
packagecn.cnki.ref.controller;importcn.cnki.ref.mapper.StudentParamsMapper;importcn.cnki.ref.pojo.Student;importorg.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;public interfaceStudentParamsController {
@RestControllerpublic classStudentParamsMapper {
@Autowiredprivatecn.cnki.ref.mapper.StudentParamsMapper StudentParamsMapper;
@RequestMapping("/getStudentByIdAndNameByObjectParam")public Student getStudentByIdAndNameByObjectParam(@RequestParam("id") Integer id, @RequestParam("name") String name) {
Student student= newStudent();
student.setName(name);
student.setId(id);
Student studentQuery=StudentParamsMapper.getStudentByIdAndNameByObjectParam(student);returnstudent;
}
}
}
View Code
测试
http://localhost:8080/getStudentByIdAndNameByObjectParam?id=1&name=张三