一、单个参数

 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. postman 断点

  2. proe工程图输出dwg/dxf文件设置

    网上看到不少人分享proe转转dxf/dwg配置文件的,但是看了一圈,几乎都没有涉及到转化线型的,所以自己整理自己的配置文件,写在这里分享出来. 以Pro/engineer WF5.0为例: 1.复制 ...

  3. Linux内核分析第一周总结

    冯诺依曼体系结构 储存程序计算机工作模型 硬件 程序员 CPU当作for循环: IP: 16位计算机:IP 32位计算机:eIP 64位计算机:rIP X86汇编基础 X86的CPU寄存器 X86的C ...

  4. team330团队铁大兼职网站使用说明

    项目名称:铁大兼职网站 项目形式:网站 网站链接:http://39.106.30.16:8080/zhaopinweb/mainpage.jsp 开发团队:team330 网站上线时间:2018年1 ...

  5. Leetcode题库——40.组合总和II

    @author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:给定一个数组 candidat ...

  6. 一篇关于spring ioc 依赖注入3种方式的文章引用

    今天看到一篇spring ioc 容器依赖注入3种方式的文章,为了方便后面的复习,在此引用别人的文章,查看请戳我.

  7. Objective-C 语言特点/特性

    Objective-C中 1.所有的类都必须继承自NSObject. 2.所有对象都是指针的形式. 3.用self代替this. 4.使用id代替void*. 5.使用nil表示NULL, 6.只支持 ...

  8. bash基本功能 -命令的别名和快捷键

    命令的别名 == 人的小名 如何查看和设定别名 alias 查看系统中的所有别名 ls --color=auto alias ll = 'ls - l --color=auto' touch abc ...

  9. Docker(四)-Dcoker镜像

    Docker 运行容器前需要本地存在对应的镜像,如果镜像不存在本地, Docker 会从镜像仓库下载(默认是Docker Hub公共注册服务器中的仓库). Docker Hub:https://hub ...

  10. 【转】Mysql事务,并发问题,锁机制

    转自:http://www.cnblogs.com/fidelQuan/p/4549068.html 1.什么是事务 事务是一条或多条数据库操作语句的组合,具备ACID,4个特点. 原子性:要不全部成 ...