当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. Dubbo 基础教程

    原文地址:Dubbo 基础教程 博客地址:http://www.extlight.com 一.前言 当服务越来越多时,容量的评估,小服务资源的浪费等问题逐渐显现,此时需要增加一个调度中心基于访问压力实 ...

  2. pgsql的同步须知

    pgsql的同步模式是根据master上的日志来做的同步,有两种同步方式,参考http://www.chinaxing.org/articles/Postgres/2012/12/14/2012-12 ...

  3. java 输出helloword

    1,安装jdk;2,配置环境变量;3,新建D:/Test.java文件;4,文件内容如下:public class Test{ public static void main(String[] arg ...

  4. VS解决方案的目录结构设置和管理

    一个中等规模的解决方案通常都会包含多个项目,其中一些项目产出静态库,一些产出动态库,一些用于单元测试,还有的产出最终的应用程序执行档.除此以外,根据项目的需求,还会使用一些第三方的库.   所以为解决 ...

  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">的说明

    今天在做适配手机版时,chrome调到手机版,但是还是显示PC端的样式,无法展现出手机端的样式: 开始的时候还以为是chrome版本的问题,最新版本的chrome62.0是有很多变化的,而之前工作中使 ...

  6. mysql如何处理外码约束

    http://hi.baidu.com/wangzhiqing999/item/e74b2be28be445a3c00d7528 MySQL 外键约束 -- 创建测试主表. ID 是主键.CREATE ...

  7. 前端之promise

    Promise是一个非常重要的方法,它可以是一些异步操作最后归为有序的进行. url: from django.contrib import admin from django.urls import ...

  8. angularjs中的$http详解

    语法: 要将区别先弄清$http服务,它是对原生XMLHttpRequest对象的简单封装,是只能接受一个参数的方法, 这个方法会返回一个promise对象,具有sccess和error两个方法.当然 ...

  9. MS SQL 流程控制语句

    Declare   myCursor   cursor   For     Select   *   from   table1         open   myCursor         Fet ...

  10. ManagedProperty not injected in @FacesConverter

    I'm trying to inject a ManagedBean in my FacesConverted the following way: @ManagedBean @RequestScop ...