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 ...
随机推荐
- JQ_下雪特效
这是一个jQuery下雪特效.特效的代码如下: <style>body{background:black;color:white}</style><script>/ ...
- 机器学习之线性回归使用Python和tensorflow实现
导入依赖包 import tensorflow as tf import numpy as np import matplotlib.pylab as plt from pylab import mp ...
- C. Smallest Word
链接 [http://codeforces.com/contest/1043/problem/C] 题意 给你一个只包含a,b的字符串,有一种操作把第1个字符到第i个字符的子串进行反转,问要使最后字符 ...
- 《Linux内核》课本读书笔记 第三章
- 读书笔记(chapter5)
系统调用 5.1与内核通信 1.系统调用在用户空间进程和硬件设备之间添加一个中间层.作用有三个:它为用户空间提供了一种硬件的抽象接口:系统调用保证了系统的稳定和安全:系统调用是用户空间访问内核的唯一手 ...
- rabbitmq windows 安装,更改配置文件路径的问题(管理页面不显示的问题)
路径中的advanced和rabbitmq是advanced.config和rabbitmq.config的文件名而不是文件夹名 并将这两个环境变量加到path里. 完成后,执行命令:rabbitmq ...
- Java NIO 详解(二)
异步IO 异步 I/O 是一种没有阻塞地读写数据的方法.通常,在代码进行 read() 调用时,代码会阻塞直至有可供读取的数据.同样, write()调用将会阻塞直至数据能够写入,关于同步的IO请参考 ...
- MySQL 5.7 GA 新特性
转载自: http://www.chinaxing.org/articles/Database/2015/10/23/2015-10-22-mysql-5.7.html sys-schema http ...
- 【题解】 [ZJOI2006]书架 (Splay)
懒得复制,戳我戳我 Solution: 还是一个\(Splay\),我们只用多存一个值\(rad\)来维护二叉树,然后用数组存下每个书对应的值是多少 \(Top\)操作,我是把\(s\)旋转到根节点, ...
- 【小记】FreeRTOS任务创建后但任务中为空时运行错误
FreeRTOS任务创建后但任务中无语句为空时运行错误 会死在文件<port.c>中下边函数处 static void prvTaskExitError( void ){ /* A fun ...
