一、单个参数

 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之参数传递的更多相关文章

  1. Java SSM框架之MyBatis3(一)MyBatis入门

    MyBatis3介绍 mybatis就是一个封装来jdbc的持久层框架,它和hibernate都属于ORM框架,但是具体的说,hibernate是一个完全的orm框架,而mybatis是一个不完全的o ...

  2. Java SSM框架之MyBatis3(二)MyBatis之Mapper代理的开发方式

    Mapper代理的开发规范 1. mapper接口的全限定名要和mapper映射文件的namespace值一致. 2. mapper接口的方法名称要和mapper映射文件的statement的id一致 ...

  3. Java SSM框架之MyBatis3(八)MyBatis之动态SQL

    前言: mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活. 创建User表 /*Table structure for table `user` */ D ...

  4. Java SSM框架之MyBatis3(四)MyBatis之一对一、一对多、多对多

    项目搭建Springboot 1.5  pom.xml <?xml version="1.0" encoding="UTF-8"?> <pro ...

  5. Java SSM框架之MyBatis3(七)MyBatis之参数取值

    在mybatis中,参数取值方式有两种:#{ } 和 ${ } 一.#{ } select * from student where name=#{name} 编译后执行的sql语句: select ...

  6. Java SSM框架之MyBatis3(五)MyBatis之ResultMap详解

    resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...

  7. Java SSM框架之MyBatis3(三)Mybatis分页插件PageHelper

    引言 对于使用Mybatis时,最头痛的就是写分页,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真不想花双倍的时间写count和select ...

  8. Java SSM框架之MyBatis3(十)MyBatis批量插入数据(MySql)

    插入成功后返回自增主键 <insert id="insertRole" parameterType="role" useGeneratedKeys=&qu ...

  9. (转)MyBatis框架的学习(六)——MyBatis整合Spring

    http://blog.csdn.net/yerenyuan_pku/article/details/71904315 本文将手把手教你如何使用MyBatis整合Spring,这儿,我本人使用的MyB ...

随机推荐

  1. Beyond Compare文本对比中提示编辑禁止的解决方法

    Beyond Compare是一款拥有文本比较功能的智能化软件,它支持在文本比较的同时,直接对差异文本进行修改.删除.编辑等一系列操作,这样一来,节约了文本对比的时间.但是在使用Beyond Comp ...

  2. Python+opencv 图像拼接

    1.http://www.cnblogs.com/skyfsm/p/7411961.html ,给出了很好地拼接算法实现 2.由于不是Python的,所以简单做了一些翻译转成Python+opencv ...

  3. 【阿里巴巴】CBU技术部招聘

    如果你偏爱技术挑战,希望成就不一样的自己,欢迎投递简历至 yangyang.xiayy@alibaba-inc.com [业务简介] B2B内贸www.1688.com:1688.com是最大的内贸B ...

  4. SpringMVC 之 @ResponseBody 和 @RequestBody

    前后端进行数据交互的时候,规定数据交互的格式,使数据交互规范而统一,是极为重要的事.一般而言,我们会采用 JSON 进行数据交互.本文暂不讨论如何 JSON 的格式规范,而是解析一下如何在 Sprin ...

  5. 团队week9

    1. Bug bash ▪ How many bugs is found in your bug bash? Bug很多,就前端的用户管理部分发现的bug就有14个. 2. Write a blog ...

  6. 《Linux内核分析》第三周笔记 构造一个简单的Linux系统MenuOS

    构造一个简单的Linux系统MenuOS 一.linux内核源代码简介 三大法宝(存储程序计算机.函数调用堆栈.中断)和两把宝剑(中断上下文的切换:保存现场和恢复现场.进程上下文的切换) 1.在lin ...

  7. String基础

    一: String,StringBuffer与StringBuilder的区别??String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程 ...

  8. A02-java学习-classpath配置-标识符-java变量类型

    学习 1, classpath的配置和使用 2, java的标识符命名规则和命名规范 3, 字符编码 4, java的变量类型 5, 程序的入口main方法解释

  9. Alpha冲刺——day7

    Alpha冲刺--day7 作业链接 Alpha冲刺随笔集 github地址 团队成员 031602636 许舒玲(队长) 031602237 吴杰婷 031602220 雷博浩 031602634 ...

  10. 部分机器进入bios 的 方法