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> ...
随机推荐
- p/Invoke工具
开源的工具 下面这个链接来下载这个工具: http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a06085 ...
- UML drawing tools
1. tools based on the script http://www.websequencediagrams.com/ UMLet 12.1 My best two recommendati ...
- PHP框架(如:laravel、yii2、thinkPHP5)中统一异常处理及统一日志打印
背景: 现在写接口服务应用有一个很通用的需求,想通过日志.或者监控的形式监测的接口的运行情况,比如耗时.请求参数.响应结果.和前端联调接口时或者排查线上问题时日志必不可少,特别是现场日志. 应用运行时 ...
- java io之管道流
一.java.io中存在一中流叫管道流,类似管道的功能.PipedOutputStream和PipedInputStream分别是管道输出流和管道输入流.这个两个流必须同时使用. 二.作用:进行两个线 ...
- zoj-3410-Layton's Escape
/* ZOJ Problem Set - 3410Layton's Escape ----------------------------------------------------------- ...
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #12 使用Memory Cgroup限制内存使用量
HACK #12 使用Memory Cgroup限制内存使用量 Memory Cgroup是Cgroup的资源限制功能之一,可以控制特定进程可以使用的内存量.Memory CgroupMemory C ...
- kettle步骤概览--清洗校验
2017年03月22日 11:01:19 阅读数:4755 前边介绍了34个子程序 关于清洗和校验的子系统包含四个: 清洗.错误处理.审计维度.排重 Kettle里没有单一的数据清洗步骤,但有很多 ...
- centos7.3给搭建SVN服务器
centos7.3给搭建SVN服务器 1 安装svnserver yum install subversion 2 查看版本 svnserve --version 3 创建版本库 3.1 运行以下命令 ...
- js 判断空数组,空对象!
var attr1 = [ ]; var obj1 = { }; console.log(isEmpty(attr1)); console.log(isEmpty(obj1)); function i ...
- 通过nginx + lua来统计nginx上的监控网络请求和性能
介绍 以前我们为nginx做统计,都是通过对日志的分析来完成.比较麻烦,现在基于ngx_lua插件,开发了实时统计站点状态的脚本,解放生产力. 项目主页: https://github.com/sky ...