java mapper 参数_Java MyBatis3(6)参数传递

一、单个参数

StudentParamsMapper

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecn.cnki.ref.mapper;importcn.cnki.ref.pojo.Student;public interfaceStudentParamsMapper {/*** 根据name查询

*@paramname

*@return

*/

publicStudent getByName(String name);

}

View Code

StudentParamsMapper.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

select * from student where id=#{param1} and name=#{param2}

View Code

StudentParamsController

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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/王五

24c9206865819091c75d5af32d9fa354.png

二、多个参数

1.根据参数key值获取,获取规则为param1,param2,param3.........:

StudentParamsMapper

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

select * from student where id=#{0} and name=#{1}

View Code

StudentParamsController

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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=张三

aea57406ab13407bd71ad4f3201dc78e.png

2.绑定参数名

StudentParamsMapper

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

select * from student where name=#{name} and id=#{id}

View Code

StudentParamsMapper.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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=张三

ba872b0752e33395079cde07076fed7d.png

3.封装实体参数

StudentParamsMapper

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

select * from student where name=#{name} and id=#{id}

View Code

StudentParamsController

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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=张三

0784b1e0e17956c2e659620ee5a2ffea.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值