Parameter
 
1.   传入简单类型

JAVA代码:

  1. public User get(Long id) {
  2. return (User) getSqlSession().selectOne("com.liulanghan.get" , id);
  3. }

MAPPER :

  1. <select id="findUserListByIdList" parameterType="java.lang.Long" resultType="User">
  2. select * from user where  id = #{id};
  3. </select>

2.   传入List

JAVA代码:

  1. public List<Area> findUserListByIdList(List<Long> idList) {
  2. return getSqlSession().selectList("com.liulanghan.findUserListByIdList", idList);
  3. }

MAPPER :

  1. <select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">
  2. select * from user user
  3. <where>
  4. user.ID in (
  5. <foreach item="guard" index="index" collection="list"
  6. separator=","> #{guard} </foreach>
  7. )
  8. </where>
  9. </select>

单独传入list时,foreach中的collection必须是list,不不管变量的具体名称是什么。比如这里变量名为idList,
 collection却是是list。
 
3.  传入数组

JAVA代码:

  1. public List<Area> findUserListByIdList(int[] ids) {
  2. return getSqlSession().selectList("com.liulanghan.findUserListByIdList", ids);
  3. }

MAPPER :

  1. <select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User">
  2. select * from user user
  3. <where>
  4. user.ID in (
  5. <foreach item="guard" index="index" collection="array"
  6. separator=","> #{guard} </foreach>
  7. )
  8. </where>
  9. </select>

单独传入数组时,foreach中的collection必须是array,不不管变量的具体名称是什么。比如这里变量名为ids,
 collection却是是array

4.  传入map
 
 JAVA代码:

  1. public boolean exists(Map<String, Object> map){
  2. Object count = getSqlSession().selectOne("com.liulanghan.exists", map);
  3. int totalCount = Integer.parseInt(count.toString());
  4. return totalCount > 0 ? true : false;
  5. }

MAPPER :

  1. <select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">
  2. SELECT COUNT(*) FROM USER user
  3. <where>
  4. <if test="code != null">
  5. and user.CODE = #{code}
  6. </if>
  7. <if test="id != null">
  8. and user.ID = #{id}
  9. </if>
  10. <if test="idList !=null ">
  11. and user.ID in (
  12. <foreach item="guard" index="index" collection="idList"
  13. separator=","> #{guard} </foreach>
  14. )
  15. </if>
  16. </where>
  17. </select>

MAP中有list或array时,foreach中的collection必须是具体list或array的变量名。比如这里MAP含有一个
    名为idList的list,所以MAP中用idList取值,这点和单独传list或array时不太一样。
 
 
5. 传入JAVA对象
 
 JAVA代码:

  1. public boolean findUserListByDTO(UserDTO userDTO){
  2. Object count = getSqlSession().selectOne("com.liulanghan.exists", userDTO);
  3. int totalCount = Integer.parseInt(count.toString());
  4. return totalCount > 0 ? true : false;
  5. }

MAPPER :

  1. <select id="findUserListByDTO" parameterType="UserDTO" resultType="java.lang.Integer">
  2. SELECT COUNT(*) FROM USER user
  3. <where>
  4. <if test="code != null">
  5. and user.CODE = #{code}
  6. </if>
  7. <if test="id != null">
  8. and user.ID = #{id}
  9. </if>
  10. <if test="idList !=null ">
  11. and user.ID in (
  12. <foreach item="guard" index="index" collection="idList"
  13. separator=","> #{guard} </foreach>
  14. )
  15. </if>
  16. </where>
  17. </select>

JAVA对象中有list或array时,foreach中的collection必须是具体list或array的变量名。比如这里UserDTO含有一个
    名为idList的list,所以UserDTO中用idList取值,这点和单独传list或array时不太一样。

6.取值

由上面可以看出,取值的时候用的是#{}。它具体的意思是告诉MyBatis创建一个预处理语句参数。
 使用JDBC,这样的一个参数在SQL中会由一个“?”来标识,并被传递到一个新的预处理语句中,就像这样:

  1. // Similar JDBC code, NOT MyBatis…
  2. String selectPerson = “SELECT * FROM PERSON WHERE ID=?”;
  3. PreparedStatement ps = conn.prepareStatement(selectPerson);
  4. ps.setInt(1,id);

可以看到这个写法比较简单,MyBatis为我们做了很多默认的事情,具体的写法应该如下:

  1. #{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注入攻击,因此你
 不应该允许用户输入这些字段,或者通常自行转义并检查。

最新文章

  1. Mysql基础(二)
  2. PHP面向对象学习二
  3. [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
  4. Linux下安装配置MongoDB 3.0.x 版本数据库
  5. try...except 错误记录添加logging
  6. CasperJS基于PhantomJS抓取页面
  7. raspberry pi 3 截图及查看
  8. Delphi下URL汉字编码解码的两个函数
  9. 性能是全新的 SEO
  10. [code]判断周期串
  11. Jdk1.6 JUC源码解析(7)-locks-ReentrantLock
  12. int ,long long等范围
  13. javascript day 02
  14. Spark 以及 spark streaming 核心原理及实践
  15. Gradle基本使用(1):安装、IDEA使用
  16. android 量产软件改动信息(持续更新)
  17. [翻译] GCDiscreetNotificationView
  18. React-Native StyleSheet属性支持
  19. 关于 WinScp 的一点使用经验
  20. c# lock的误解

热门文章

  1. POJ 1651 Multiplication Puzzle 区间dp(水
  2. 一起talk C栗子吧(第九十回:C语言实例--使用管道进行进程间通信三)
  3. zabbix 监控zookeeper
  4. Linux微信web开发者工具
  5. 4.8 Using Ambiguous Grammars
  6. python-----查看显卡gpu信息
  7. 第十一周 Leetcode 576. Out of Boundary Paths (HARD) 计数dp
  8. JavaScript代码优化新工具UglifyJS
  9. 【174】C#添加非默认字体
  10. varnish的架构和日志