Java MyBatis3(6)参数传递
一、单个参数
StudentParamsMapper
package cn.cnki.ref.mapper; import cn.cnki.ref.pojo.Student; public interface StudentParamsMapper { /** * 根据name查询 * @param name * @return */ public Student getByName(String name); }
StudentParamsMapper.xml
<?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" > <mapper namespace="cn.cnki.ref.mapper.StudentParamsMapper"> <!-- 根据用户名和id同时查询 --> <select id="getStudentByIdAndName" resultType="cn.cnki.ref.pojo.Student"> select * from student where id=#{param1} and name=#{param2} </select> </mapper>
StudentParamsController
package cn.cnki.ref.controller; import cn.cnki.ref.mapper.StudentParamsMapper; import cn.cnki.ref.pojo.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; public interface StudentParamsController { @RestController public class StudentParamsMapper { @Autowired private cn.cnki.ref.mapper.StudentParamsMapper StudentParamsMapper; @GetMapping("/studentparams/{name}") public Student selectCourseById(@PathVariable("name") String name) { Student student = StudentParamsMapper.getByName(name); return student; } } }
测试
http://localhost:8080/studentparams/王五
二、多个参数
1.根据参数key值获取,获取规则为param1,param2,param3.........:
StudentParamsMapper
package cn.cnki.ref.mapper; import cn.cnki.ref.pojo.Student; public interface StudentParamsMapper { /** * 根据用户名和id同时查询 * @param id * @param name * @return */ public Student getStudentByIdAndName(Integer id,String name); }
StudentParamsMapper.xml
<?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" > <mapper namespace="cn.cnki.ref.mapper.StudentParamsMapper"> <!-- 根据用户名和id同时查询 --> <select id="getStudentByIdAndName" resultType="cn.cnki.ref.pojo.Student"> select * from student where id=#{0} and name=#{1} </select> </mapper>
StudentParamsController
package cn.cnki.ref.controller; import cn.cnki.ref.mapper.StudentParamsMapper; import cn.cnki.ref.pojo.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; public interface StudentParamsController { @RestController public class StudentParamsMapper { @RequestMapping("/getStudentByIdAndName") public Student getStudentByIdAndName(@RequestParam("id") Integer id, @RequestParam("name") String name) { Student student = StudentParamsMapper.getStudentByIdAndName(id,name); return student; } } }
测试
http://localhost:8080/getStudentByIdAndName?id=1&name=张三
2.绑定参数名
StudentParamsMapper
<?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" > <mapper namespace="cn.cnki.ref.mapper.StudentParamsMapper"> <!-- 根据用户名和id同时查询 --> <select id="getStudentByIdAndNameParam" resultType="cn.cnki.ref.pojo.Student"> select * from student where name=#{name} and id=#{id} </select> </mapper>
StudentParamsMapper.xml
package cn.cnki.ref.mapper; import cn.cnki.ref.pojo.Student; import org.apache.ibatis.annotations.Param; public interface StudentParamsMapper { /** * 根据用户名和id同时查询 * @param id * @param name * @return */ public Student getStudentByIdAndNameParam(@Param("id")Integer id, @Param("name")String name); }
StudentParamsController
package cn.cnki.ref.controller; import cn.cnki.ref.mapper.StudentParamsMapper; import cn.cnki.ref.pojo.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; public interface StudentParamsController { @RestController public class StudentParamsMapper { @Autowired private cn.cnki.ref.mapper.StudentParamsMapper StudentParamsMapper; @RequestMapping("/getStudentByIdAndNameParam") public Student getStudentByIdAndNameParam(@RequestParam("id") Integer id, @RequestParam("name") String name) { Student student = StudentParamsMapper.getStudentByIdAndName(id,name); return student; } } }
测试
http://localhost:8080/getStudentByIdAndNameParam?id=1&name=张三
3.封装实体参数
StudentParamsMapper
package cn.cnki.ref.mapper; import cn.cnki.ref.pojo.Student; import org.apache.ibatis.annotations.Param; public interface StudentParamsMapper { /** * 根据用户名和id同时查询 * @param id * @param name * @return */ public Student getStudentByIdAndNameByObjectParam(Student student); }
StudentParamsMapper.xml
<?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" > <mapper namespace="cn.cnki.ref.mapper.StudentParamsMapper"> <!-- 根据用户名和id同时查询 --> <select id="getStudentByIdAndNameByObjectParam" resultType="cn.cnki.ref.pojo.Student"> select * from student where name=#{name} and id=#{id} </select> </mapper>
StudentParamsController
package cn.cnki.ref.controller; import cn.cnki.ref.mapper.StudentParamsMapper; import cn.cnki.ref.pojo.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; public interface StudentParamsController { @RestController public class StudentParamsMapper { @Autowired private cn.cnki.ref.mapper.StudentParamsMapper StudentParamsMapper; @RequestMapping("/getStudentByIdAndNameByObjectParam") public Student getStudentByIdAndNameByObjectParam(@RequestParam("id") Integer id, @RequestParam("name") String name) { Student student = new Student(); student.setName(name); student.setId(id); Student studentQuery = StudentParamsMapper.getStudentByIdAndNameByObjectParam(student); return student; } } }
测试
http://localhost:8080/getStudentByIdAndNameByObjectParam?id=1&name=张三