Mybatis的map用法
当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用法的更多相关文章
- Collection List Set和Map用法与区别
labels:Collection List Set和Map用法与区别 java 散列表 集合 Collection 接 口的接口 对 象的集合 ├ List ...
- mybatis循环、mybatis传map
mybatis中使用循环.mybatis传入map案例 <!-- 根据id修改商户提成配置--> <update id="editStopAll" paramet ...
- mybatis 遍历map;
mybatis 遍历map; 参考http://blog.csdn.net/hj7jay/article/details/78652050 ps: ${m[key]}这是显示 打印的key读value ...
- ES6中Set 和 Map用法
JS中Set与Map用法 一.Set 1.基本用法 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. ...
- 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 ...
- C++中的STL中map用法详解(转)
原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解 Map是STL的一个关联容器,它提供 ...
- JSONObject和JSONArray 以及Mybatis传入Map类型参数
import org.json.JSONArray;import org.json.JSONObject; 将字符串转化为JSONArray JSONArray jsonArray = new ...
- Mybatis sql map 小于号配置
Mybatis SQL map配置中出现小于号转义时,通过<![CDATA[查询条件]]>解决. EXCEMPLE: <select id="getComments&quo ...
- C++:map用法及元素的默认值
C++:map用法 一.map基本用法 键值对 第一个参数为键的类型,第二个参数为值的类型. 源代码 #include <iostream> #include <string> ...
随机推荐
- macOS -- Mac系统如何编辑hosts文件
Hosts是一个没有扩展名的系统文件,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联"数据库",当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文 ...
- 设置pip镜像源
修改镜像源 vim ~/.pip/pip.conf windows 当前用户目录\pip\pip.ini 内容 [global] index-url=https://pypi.tuna.tsinghu ...
- 黄聪:将自己开发的插件发布到WordPress官方插件站(转)
原文作者:我爱水煮鱼 把你的插件发布到 WordPress 官方插件目录是让更多人用到插件的好方法,这样可以让你的插件不仅仅给我们中国人民使用,而且还可以让你的插件给全世界人民使用,想想全世界人民都在 ...
- java 查询solr时间格式
solr时间格式是2015-07-06T00:00:00.0Z,所以下面是把当前时间做转换 SimpleDateFormat format = new SimpleDateFormat("y ...
- 20181123_SQL Server 2008_找出以逗号分隔的字符串中最大的数字
--select [dbo].[Fun_GetMaxNum]('棉 20%,麻 190%,涤纶60%') CREATE FUNCTION [dbo].[Fun_GetMaxNum] ( @StrAll ...
- CFGym 101490E 题解
一.题目链接 http://codeforces.com/gym/101490 二.题面 三.题意 给你一个图,n个点,m条边,一个x,从顶点1走到顶点n.假设从顶点1走到顶点n的最短路为d,x代表你 ...
- 关于python-生成HTMLTestRunner测试报告
一.HTMLTestRunner 是 Python 标准库的 unittest 模块的一个扩展,它可以生成 HTML的 测试报告. 1.首先要下 HTMLTestRunner.py 文件,将下载的文件 ...
- 在C#里面获得应用程序的当前路径
Environment.CurrentDirectory——获取应用程序的当前工作目录.System.IO.Directory.GetCurrentDirectory()AppDomain.Curre ...
- delphi TEncoding
#include <tchar.h> #include <memory> //For STL auto_ptr class //------------------------ ...
- 两种定时器 setInterval(一直执行) setTimeout(只执行一次)
//第一种 var obj = setInterval(function () { console.log(123); clearInterval(obj) }, 1000); //第二种 var t ...