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> ...
随机推荐
- CentOS 6.6下 BCM4312 802.11b/g无线网卡驱动安装
1.目前www.broadcom.com网站上最新版本为hybrid-v35,但此版本与2.6.32不匹配,无法识别验证密码,搜索网上说是要求升级内核,后根据http://www.dadclab.co ...
- Autocad 2010+ObjectArx 2010 +Vs2010 的.net 开发设置(转)
Autocad 2010+ObjectArx 2010 +Vs2010 的.net 开发设置 分类: ObjectArx.net2010-09-14 16:52 4203人阅读 评论(7) 收藏 举报 ...
- IntelliJ IDEA 基础设置
原文地址:IntelliJ IDEA 基础设置 博客地址:http://www.extlight.com 一.前言 IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,Intel ...
- bzoj2505: tickets
Description 有一位售票员给乘客售票,对于每位乘客,他会卖出多张连续的票,直到已卖出的编号的所有位置上的数的和不小于给定的正数k.然后他会按照相同的规则给下一位乘客售票.初 ...
- 学习笔记之Tips for Macbook
写给Mac新手的入门指南 - 威锋网 https://mp.weixin.qq.com/s/pqmqGZhNwevx57KeLnzZmg https://bbs.feng.com/read-htm-t ...
- word2vec相关
word '\xe8\xb6\x85\xe8\x87\xaa\xe7\x84\xb6\xe7\x8e\xb0\xe8\xb1\xa1' not in vocabulary 分词后的样本格式:英雄联盟, ...
- oracle 11g RAC 的一些基本概念(四)
RAC 在Grid Infrastructure安装完以后,我们把注意力转移到集群上的Oracle软件的安装上来.我们看到,Grid Infrasctructure提供了运行RAC的框架,包括集群 ...
- linux记录每个用户执行的命令
1.在/etc/profile中添加如下代码: #history USER_IP=`>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'` HISTD ...
- 基础html和css知识
No1.HTML 1.网页结构 网页结构一般都包含文档声明DOCTYPE,并且在head中的meta应该包含编码格式.关键字.网页描述信息.简单格式如下: <!DOCTYPE html> ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习
转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...