一、ibatis的关键字like查询

select * from t_student where s_name '%张%';

这种like语句在ibatis中怎么写,他们现在的项目是用ibatis作为持久层的框架。

我的第一反应是这样写:
<select id="showOneStudentByName" parameterClass="String" resultMap="studentORM">
     select * from t_student where s_name like #name#
</select>

但是在调用中需要在参数的前后加上%,比如这样:
return sqlMapper.queryForList("showOneStudentByName", "%"+name+"%");

虽然这样可行,但总显得有些不协调。仔细看了ibaitis的文档后发现最好的写法是这样
<select id="showOneStudentByName" parameterClass="String" resultMap="studentORM">
   select * from t_student where s_name like '%'||#name#||'%' 
</select>

在调用的时候就不用去前后加%了。

注意:sql语句不要写成select * from t_student where s_name like '%$name$%',这样极易受到注入攻击。
网上搜了一下ibatis的关于like的使用,撇开 '%$xxx$%' 不讲。网上的解决方法如下:

select *
from user
where username like '%'||#username#||'%'

其实上面的语句是针对Oracle 的,对于不同数据库字符串连接符不一样。 现列举mysql

select *
from user
where username like concat('%', #username#, '%')

            

二、ibatis操作陷阱
在使用iBatis操作数据的时候,容易跌入陷阱。
1、保存insert方法
在保存数据方面,iBatis的insert方法返回的是新增记录的主键,类型为Object,但实为整型,有时候会让人误解为实体类型,也并非任何表的insert操作都会返回主键----这是一个陷阱。
要返回这个新增记录的主键,前提是表的主键是自增型的,或者是Sequence的。否则获取新增记录主键的值为0或者null。
对于oracle这样来写:
<selectKey resultClass="int" keyProperty="id">
<![CDATA[
select hibernate_seq.nextval as id from dual
]]>
</selectKey>
对比Hibernate返回的Serializable类型,实际上也是主键。
2、更新删除update/delete方法
返回影响的记录行数。
3、对DAO/Service设计时候的影响
1)、对于自增主键类型表,DAO的save方法可以返回主键、或者实体对象(该对象的标识符域会填充上主键值)。
2)、对于非自增主键类型表,DAO的save方法,在保存之前就指导整个实体对象的所有值(包括主键),因此返回什么 都可以,也可以不返回值。
3)、对于Service的,不管表如何,一般都返回保存的实体对象。

来自:http://www.cnblogs.com/linjiqin/archive/2012/06/01/2529829.html

ibatis注意要点的更多相关文章

  1. 值得注意的ibatis动态sql语法格式

    一.Ibatis常用动态sql语法,简单粗暴用一例子 <select id="iBatisSelectList" parameterClass="java.util ...

  2. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): da.huying.usermanag ...

  3. [入门级] visual studio 2010 mvc4开发,用ibatis作为数据库访问媒介(一)

    [入门级] visual studio 2010 mvc4开发,用ibatis作为数据库访问媒介(一) Date  周二 06 一月 2015 By 钟谢伟 Tags mvc4 / asp.net 示 ...

  4. Spring+ibatis动态管理数据源

    Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性.而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时 ...

  5. iBatis.net 循环iterate,没有foreach

    3.9.4. Iterate Element This tag will iterate over a collection and repeat the body content for each ...

  6. ibatis 轻松入门

    1.总中的配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig ...

  7. IBatis.Net使用总结(四)-- IBatis 调用存储过程

    IBatis 调用存储过程 http://www.cnblogs.com/jeffwongishandsome/archive/2010/01/10/1543219.html http://www.c ...

  8. MyBatis-Exception:org.apache.ibatis.exceptions.PersistenceException

    错误信息如下: HTTP Status 500 - org.mybatis.spring.MyBatisSystemException: nested exception is org.apache. ...

  9. Exception:HTTP Status 500 - org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    主要错误信息如下: HTTP Status 500 - org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...

随机推荐

  1. 【centos】centos安装g++

    gcc在Centos下的安装:使用的是以下语句:yum install gcc 以为安装g++,类似的应该使用:yum install g++ 可是提示:command is not found 查询 ...

  2. Django1.0和2.0中的rest_framework的序列化组件之超链接字段的处理

    大家看到这个标题是不是有点懵逼,其实我就是想要一个这样的效果 比如我get一条书籍的数据,在一对多的字段中我们显示一个url,看起来是不是很绚! 下面我们就来实现这么一个东西 首先我们一对多字段中的一 ...

  3. StringBuffer 和 StringBuilder 类

    当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类. 和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够 ...

  4. errror:[test_rig3.launch] is neither a launch file in package [svo_ros] nor is [svo_ros] a launch file name The traceback for the exception was written to the log file

    1. 打开一个终端,运行roscore 2. 打开另一个终端,运行 roslaunch svo_ros test_rig3.launch 出现errror: 忘记关键步骤了 $ cd <path ...

  5. js中被调用的函数获取调用者对象

    通过event.srcElement即可.一直用了很久的传参,终于解决了.

  6. 发现一个animate的小应用

    <script src="jquery-1.11.1.js"></script> <script> //animate() : //第一个参数 ...

  7. JAVA软件安装

    Java配置----JDK开发环境搭建及环境变量配置 文章来源:http://www.cnblogs.com/smyhvae/p/3788534.html Tomcat安装.配置和部署笔记 文章来源: ...

  8. mysql 8小时timeout问题

    MySQL> show variables like '%timeout%'; +--------------------------+-------+ | Variable_name      ...

  9. delphi程序设计改进可读性一法

    Delphi,Lazarus程序设计改进一法 作者:steven QQ:1565498246 Delphi/Lazarus有一个思想就是方法.函数名调用,后边可以不使用括号(),比如调用函数Now,这 ...

  10. How to change and add some params to request in Laravel controller?

    $request->merge([ 'aae_id' => request('id', 0), 'foo' => 'bar', ]);