当Mybatis传过来的值是map类型的时候,有两种处理方法

1、将数值装入类封装起来

public interface IStudentDao {

    // 根据姓名和年龄查询
List<Student> selectStudentsByCondition(Map<String, Object> map); // 根据姓名和年龄查询
List<Student> selectStudentsByCondition2(String name,int age);
}

2、map有动态加载,所以不用impl,只需dao 的抽象方法和id名称一样就可以

<?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.xml相同的id名所以namespace改为对应的到的包路径-->
<mapper namespace="com.liuya.demo.mybatis.dysnamic.dao.IStudentDao">
<!-- 配置数据库和实体类的字段 -->
<resultMap id="studentMapper" type="Student">
<id column="T_ID" property="id"/>
<result column="T_NAME" property="name"/>
<result column="T_AGE" property="age"/>
<result column="T_SCORE" property="score"/>
</resultMap> <!-- 根据姓氏模糊查询 -->
<select id="selectStudentsByCondition" resultMap="studentMapper">
select T_NAME,T_AGE,T_SCORE
from STUDENT
where T_NAME like '%' #{nameCon} '%'
AND T_AGE > #{ageCon}
AND T_SCORE > #{student1.score }
</select> <!-- 根据姓氏模糊查询,#{}大括号里是索引
#{}中可以放什么内容?
(1)参数对象的属性
(2)随意内容,此时的#{}是占位符
(3)参数为map时的key
(4)参数为map时,若key所对应的value为对象,则可将对象的属性放入
(5)参数的索引号
-->
<select id="selectStudentsByCondition2" resultMap="studentMapper">
select T_NAME,T_AGE,T_SCORE
from STUDENT
where T_NAME like '%' #{0} '%'
AND T_AGE > #{1}
</select> </mapper>

3、对其测试

public class MyTest {

    private IStudentDao idao;
private SqlSession sqlSession; @Before
public void before() {
sqlSession = MybatisUtil.getSqlSession();
idao = sqlSession.getMapper(IStudentDao.class);
} @After
public void after() {
if (sqlSession != null) {
sqlSession.close();
}
} // 查询一个姓张的,年龄大于24,并且成绩比田七高的学生
@Test
public void testSelectStudentsByCondition() {
System.out.println("开始查询学生");
Student student1 = new Student("",21,66); Map<String, Object> map = new HashMap<String, Object>();
map.put("nameCon","张");
map.put("ageCon",24);
map.put("student1",student1); List<Student> students = idao.selectStudentsByCondition(map);
for (Student student : students) {
System.out.println(student);
}
System.out.println("查询学生成功");
} // 查询一个姓张的,年龄大于24,并且成绩比田七高的学生
@Test
public void testSelectStudentsByCondition2() {
System.out.println("开始查询学生"); List<Student> students = idao.selectStudentsByCondition2("张",24);
for (Student student : students) {
System.out.println(student);
}
System.out.println("查询学生成功");
}
}

Mybatis的map用法的更多相关文章

  1. Collection List Set和Map用法与区别

    labels:Collection List Set和Map用法与区别 java 散列表 集合 Collection           接 口的接口      对 象的集合   ├   List   ...

  2. mybatis循环、mybatis传map

    mybatis中使用循环.mybatis传入map案例 <!-- 根据id修改商户提成配置--> <update id="editStopAll" paramet ...

  3. mybatis 遍历map;

    mybatis 遍历map; 参考http://blog.csdn.net/hj7jay/article/details/78652050 ps: ${m[key]}这是显示 打印的key读value ...

  4. ES6中Set 和 Map用法

    JS中Set与Map用法 一.Set 1.基本用法 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. ...

  5. sort函数(cmp)、map用法---------------Tju_Oj_2312Help Me with the Game

    这道题里主要学习了sort函数.sort的cmp函数写法.C++的map用法(其实和数组一样) Your task is to read a picture of a chessboard posit ...

  6. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  7. JSONObject和JSONArray 以及Mybatis传入Map类型参数

    import org.json.JSONArray;import org.json.JSONObject; 将字符串转化为JSONArray   JSONArray  jsonArray = new ...

  8. Mybatis sql map 小于号配置

    Mybatis SQL map配置中出现小于号转义时,通过<![CDATA[查询条件]]>解决. EXCEMPLE: <select id="getComments&quo ...

  9. C++:map用法及元素的默认值

    C++:map用法 一.map基本用法 键值对 第一个参数为键的类型,第二个参数为值的类型. 源代码 #include <iostream> #include <string> ...

随机推荐

  1. An internal error occurred during: "Android Library Update".

    打开 proj.android下的.cproject文件,把多余的CConfiguration配置节删掉,只留第一个就行了.

  2. 洛谷2473(SCOI2008)奖励关

    题目:https://www.luogu.org/problemnew/show/P2473 因为可不可选此物与之前选过什么物品有关,所以状态可以记录成前面已经选过什么物品. 因为选不选此物与它带来的 ...

  3. 显示列表控件(引用SourceGrid)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...

  4. Winfrom 开源组件Control.FirefoxDialog使用

    1. 如果窗体是以模式窗体方式打开的,会出现点了应用,窗体就立马关闭.此时可能别的设置需要一块设置,这种就存在问题. var form1 = new Form1(); form1.ShowDialog ...

  5. springboot 整合 elasticsearch

    1引入jar包 <!--elasticsearch--> <dependency> <groupId>org.springframework.boot</gr ...

  6. Bootstrap-Other:HTML编码规范

    ylbtech-Bootstrap-Other:HTML编码规范 1.返回顶部 1. Bootstrap HTML编码规范 语法 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得 ...

  7. pythonNet day03

    TCP应用之 httpserver 1.接收http请求 2.查看http请求 3.返回一个网页给客户端 # 做的是一个本地服务端,接收来自浏览器客户端的请求 from socket import * ...

  8. jemalloc内存分配器详解

    前言 C 中动态内存分配malloc 函数的背后实现有诸派:dlmalloc 之于 bionic:ptmalloc 之于 glibc:allocation zones 之于 mac os x/ios: ...

  9. dd命令的conv=fsync,oflag=sync/dsync

    conv的参数有 1.sync Pad every input block to size of 'ibs' with trailing zero bytes. When used with 'blo ...

  10. Shell脚本的调试方法

    Shell脚本的调试方法 Shell提供了一些用于调试脚本的选项,如下所示: -n    读一遍脚本中的命令但不执行,用于检查脚本中的语法错误 -v    一边执行脚本,一边将执行过的脚本命令打印到标 ...