mybatis 使用参数
Parameter
1. 传入简单类型
JAVA代码:
- public User get(Long id) {
- return (User) getSqlSession().selectOne("com.liulanghan.get" , id);
- }
MAPPER :
- <select id="findUserListByIdList" parameterType="java.lang.Long" resultType="User">
- select * from user where id = #{id};
- </select>
2. 传入List
JAVA代码:
- public List<Area> findUserListByIdList(List<Long> idList) {
- return getSqlSession().selectList("com.liulanghan.findUserListByIdList", idList);
- }
MAPPER :
- <select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">
- select * from user user
- <where>
- user.ID in (
- <foreach item="guard" index="index" collection="list"
- separator=","> #{guard} </foreach>
- )
- </where>
- </select>
单独传入list时,foreach中的collection必须是list,不不管变量的具体名称是什么。比如这里变量名为idList,
collection却是是list。
3. 传入数组
JAVA代码:
- public List<Area> findUserListByIdList(int[] ids) {
- return getSqlSession().selectList("com.liulanghan.findUserListByIdList", ids);
- }
MAPPER :
- <select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User">
- select * from user user
- <where>
- user.ID in (
- <foreach item="guard" index="index" collection="array"
- separator=","> #{guard} </foreach>
- )
- </where>
- </select>
单独传入数组时,foreach中的collection必须是array,不不管变量的具体名称是什么。比如这里变量名为ids,
collection却是是array
4. 传入map
JAVA代码:
- public boolean exists(Map<String, Object> map){
- Object count = getSqlSession().selectOne("com.liulanghan.exists", map);
- int totalCount = Integer.parseInt(count.toString());
- return totalCount > 0 ? true : false;
- }
MAPPER :
- <select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">
- SELECT COUNT(*) FROM USER user
- <where>
- <if test="code != null">
- and user.CODE = #{code}
- </if>
- <if test="id != null">
- and user.ID = #{id}
- </if>
- <if test="idList !=null ">
- and user.ID in (
- <foreach item="guard" index="index" collection="idList"
- separator=","> #{guard} </foreach>
- )
- </if>
- </where>
- </select>
MAP中有list或array时,foreach中的collection必须是具体list或array的变量名。比如这里MAP含有一个
名为idList的list,所以MAP中用idList取值,这点和单独传list或array时不太一样。
5. 传入JAVA对象
JAVA代码:
- public boolean findUserListByDTO(UserDTO userDTO){
- Object count = getSqlSession().selectOne("com.liulanghan.exists", userDTO);
- int totalCount = Integer.parseInt(count.toString());
- return totalCount > 0 ? true : false;
- }
MAPPER :
- <select id="findUserListByDTO" parameterType="UserDTO" resultType="java.lang.Integer">
- SELECT COUNT(*) FROM USER user
- <where>
- <if test="code != null">
- and user.CODE = #{code}
- </if>
- <if test="id != null">
- and user.ID = #{id}
- </if>
- <if test="idList !=null ">
- and user.ID in (
- <foreach item="guard" index="index" collection="idList"
- separator=","> #{guard} </foreach>
- )
- </if>
- </where>
- </select>
JAVA对象中有list或array时,foreach中的collection必须是具体list或array的变量名。比如这里UserDTO含有一个
名为idList的list,所以UserDTO中用idList取值,这点和单独传list或array时不太一样。
6.取值
由上面可以看出,取值的时候用的是#{}。它具体的意思是告诉MyBatis创建一个预处理语句参数。
使用JDBC,这样的一个参数在SQL中会由一个“?”来标识,并被传递到一个新的预处理语句中,就像这样:
- // Similar JDBC code, NOT MyBatis…
- String selectPerson = “SELECT * FROM PERSON WHERE ID=?”;
- PreparedStatement ps = conn.prepareStatement(selectPerson);
- ps.setInt(1,id);
可以看到这个写法比较简单,MyBatis为我们做了很多默认的事情,具体的写法应该如下:
- #{property,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler,mode=OUT,resultMap=User}
property:属性名,即代码传入的变量名。
javaType:该字段在JAVA中的类型,比如int。
jdbcType:该字段在JDBC中的类型,比如NUMERIC。
typeHandler:类型处理器
mode:参数类型为IN,OUT或INOUT参数
resultMap:结果。
还好,MyBatis比较体谅我们,一般我们只需写一个属性名即可,如#{id},其他的如javaType和typeHandlerMybatis
会自动帮我们填好。可是这样有时也会出问题,比如出现CLOB字段时。
由于JAVA代码中的String类型对应的默认typeHandler为StringTypeHandler,当用String类型处理时,如果String长度超过一定长度,就会报如下错误:
setString can only process strings of less than 32766 chararacters
解决办法是指定该属性的typeHandler,如下:
#{message,typeHandler=org.apache.ibatis.type.ClobTypeHandler}
我们也可以自定义typeHandler来处理需要的数据,具体这里详述。
JDBC类型是仅仅需要对插入,更新和删除操作可能为空的列进行处理。这是JDBC的需要,而不是MyBatis的。一般不需要配置
mode、resultMap一般不需要,在写存储过程时会用到,这里不详述。
7.字符串替换
一般情况下,我们采用#{}取值,产生预处理语句,但是有时我们可能不希望Mybatis来帮我们预处理,比如ORDER BY时,可以
采用如下写法:
ORDER BY ${columnName}
这里MyBatis不会修改或转义字符串。而是直接拼接到SQL字符串后面。
重要:接受从用户输出的内容并提供给语句中不变的字符串,这样做是不安全的。这会导致潜在的SQL注入攻击,因此你
不应该允许用户输入这些字段,或者通常自行转义并检查。
mybatis 使用参数的更多相关文章
- mybatis 传递参数的方法总结
有三种mybatis传递参数的方式: 第一种 mybatis传入参数是有序号的,可以直接用序号取得参数 User selectUser(String name,String area); 可以在xml ...
- 【转载】Mybatis多参数查询映射
转载地址:http://www.07net01.com/zhishi/402787.html 最近在做一个Mybatis的项目,由于是接触不久,虽然看了一下资料,但在实际开发中还是暴 露了很多问题,其 ...
- MyBatis传递参数
MyBatis传递参数 一.使用 map 接口传递参数 在 MyBatis 中允许 map 接口通过键值对传递多个参数,把接口方法定义为 : public List<Role> findR ...
- MyBatis传入参数为list、数组、map写法(转载)
MyBatis传入参数为list.数组.map写法 1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item ...
- Mybatis基于代理Dao实现CRUD操作 及 Mybatis的参数深入
Mybatis基于代理Dao实现CRUD操作 使用要求: 1.持久层接口和持久层接口的映射配置必须在相同的包下 2.持久层映射配置中mapper标签的namespace属性取值必须是持久层接口的全限定 ...
- MyBatis的参数,不能传入null
今天在调试的过程中发现一个bug,把传入的参数写到查询分析器中执行没有问题,但是在程序中执行就报错:org.springframework.jdbc.UncategorizedSQLException ...
- MyBatis传入参数为list、数组、map写法
1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item,index,collection,open,sep ...
- mybatis中参数为list集合时使用 mybatis in查询
mybatis中参数为list集合时使用 mybatis in查询 一.问题描述mybatis sql查询时,若遇到多个条件匹配一个字段,sql 如: select * from user where ...
- 【mybatis源码学习】mybatis的参数处理
一.mybatis的参数处理以及参数取值 1.单个参数 mybatis不做任何处理 取值方式: #{参数名/任意名} <!-- Employee getEmpById(Integer id) ...
- Mybatis传入参数类型为Map
mybatis更新sql语句: <update id="publishT00_notice" parameterType="Map"> update ...
随机推荐
- Zend Studio如何调试?
1.安装Zend Studio之前,本机已安装Apache2.如果使用Apache2作为服务器 Window-Preferences-Php-Php Servers 配置好 URL和Server Ro ...
- javascript总结02
1 如何打开和关闭一个新的窗口? 2 Window对象的哪个属性能返回上一个浏览页面? 3 一次或多次执行一段程序的函数是什么? 定时函数 4 如何查找并访问节点? 5 给表格新增行和单元格的方法分别 ...
- PHP博客项目-gai
XX科技还是米有电话过来,看样子真的是黄了.这段时间都没有好好学习,经历了两次稀里糊涂的面试,特别是第二次,让我感觉自己之前学的东西都已经忘了,本来就学的不多,也不扎实,还一忘...看了是真的要开始着 ...
- C# 函数的传值与传址(转)
http://www.cnblogs.com/mdnx/archive/2012/09/04/2671060.html using System; using System.Collections.G ...
- Centos7 配置防火墙 firewall
一.firewall 1.从CentOS7开始,默认使用firewall来配置防火墙,没有安装iptables(旧版默认安装). 2.firewall的配置文件是以xml的格式,存储在 /usr/li ...
- EasyUI Datagrid 分页显示(客户端)
转自:https://blog.csdn.net/metal1/article/details/17536185 EasyUI Datagrid 分页显示(客户端) By ZYZ 在使用JQuery ...
- bzoj 1628: [Usaco2007 Demo]City skyline【贪心+单调栈】
还以为是dp呢 首先默认答案是n 对于一个影子,如果前边的影子比它高则可以归进前面的影子,高处的一段单算: 和他一样高的话就不用单算了,ans--: 否则入栈 #include<iostream ...
- react hooks 全面转换攻略(一) react本篇之useState,useEffect
useState 经典案例: import { useState } from 'react'; function Example() { const [count, setCount] = useS ...
- Js 使用小技巧总结(1)
1.Js 的时间控制,小于初始时间,大于截止时间 <script type="text/javascript"> window.onload = Game ...
- G - And Then There Was One (约瑟夫环变形)
Description Let’s play a stone removing game. Initially, n stones are arranged on a circle and numbe ...