Java SSM框架之MyBatis3(六)MyBatis之参数传递
一、单个参数
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=张三
Java SSM框架之MyBatis3(六)MyBatis之参数传递的更多相关文章
- Java SSM框架之MyBatis3(一)MyBatis入门
MyBatis3介绍 mybatis就是一个封装来jdbc的持久层框架,它和hibernate都属于ORM框架,但是具体的说,hibernate是一个完全的orm框架,而mybatis是一个不完全的o ...
- Java SSM框架之MyBatis3(二)MyBatis之Mapper代理的开发方式
Mapper代理的开发规范 1. mapper接口的全限定名要和mapper映射文件的namespace值一致. 2. mapper接口的方法名称要和mapper映射文件的statement的id一致 ...
- Java SSM框架之MyBatis3(八)MyBatis之动态SQL
前言: mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活. 创建User表 /*Table structure for table `user` */ D ...
- Java SSM框架之MyBatis3(四)MyBatis之一对一、一对多、多对多
项目搭建Springboot 1.5 pom.xml <?xml version="1.0" encoding="UTF-8"?> <pro ...
- Java SSM框架之MyBatis3(七)MyBatis之参数取值
在mybatis中,参数取值方式有两种:#{ } 和 ${ } 一.#{ } select * from student where name=#{name} 编译后执行的sql语句: select ...
- Java SSM框架之MyBatis3(五)MyBatis之ResultMap详解
resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...
- Java SSM框架之MyBatis3(三)Mybatis分页插件PageHelper
引言 对于使用Mybatis时,最头痛的就是写分页,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真不想花双倍的时间写count和select ...
- Java SSM框架之MyBatis3(十)MyBatis批量插入数据(MySql)
插入成功后返回自增主键 <insert id="insertRole" parameterType="role" useGeneratedKeys=&qu ...
- (转)MyBatis框架的学习(六)——MyBatis整合Spring
http://blog.csdn.net/yerenyuan_pku/article/details/71904315 本文将手把手教你如何使用MyBatis整合Spring,这儿,我本人使用的MyB ...
随机推荐
- Js_特效II
字号缩放 让文字大点,让更多的用户看的更清楚.(也可以把字体变为百分比来实现)<script type="text/javascript"> function doZ ...
- 【阿里巴巴】CBU技术部招聘
如果你偏爱技术挑战,希望成就不一样的自己,欢迎投递简历至 yangyang.xiayy@alibaba-inc.com [业务简介] B2B内贸www.1688.com:1688.com是最大的内贸B ...
- IE=edge 让浏览器使用最新的渲染模式
Bootstrap不支持IE的兼容模式.为了让IE浏览器运行最新的渲染模式,建议将此 <meta> 标签加入到你的页面中: <metahttp-equiv="X-UA-Co ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- 《linux内核分析》chapter3读书笔记
- 调研ios开发环境的演变
一:ios的发展演变: 以下两句为百度百科IOS,可自行查阅,不多赘述,就Ctrl+c,Ctrl+v两句表示一下. 2007年1月9日苹果公司在Macworld展览会上公布,随后于同年的6月发布第一版 ...
- order by null 的作用
在SQL中order by null有什么用吗?这是我在一次面试时面试官问我的问题,当时我是懵的.他让我猜一下,我说不排序?没想到被我猜对了 不排序你就别用order by啊!为什么要用order b ...
- 【Leetcode】378. Kth Smallest Element in a Sorted Matrix
Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...
- TCP/IP之大明内阁 转
原创: 刘欣 码农翻身 2016-11-02 本文是<TCP/IP之大明王朝邮差>的前传, 讲一讲大明内阁的各位大人是怎么设计TCP/IP网络的.大明天启年间, 明熹宗朱由校醉心于木工 ...
- 正则js
匹配中文字符的正则表达式: [\u4e00-\u9fa5] 匹配双字节字符(包括汉字在内):[^\x00-\xff] 匹配空行的正则表达式:\n[\s| ]*\r 匹配HTML标记的正则表达式:/&l ...