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=张三
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2016-07-20 Python之Flask Web开发
2016-07-20 时光流年(2)美景