现在的位置: 首页 > 综合 > 正文

ibatis技巧收集(不断更新中)

2014年01月13日 ⁄ 综合 ⁄ 共 2317字 ⁄ 字号 评论关闭

1、ibatis保存后返回主键

<insert id="saveAlarmInfo" parameterClass="alarminfo">
		INSERT INTO its.alarm_info 
			(place, 
			isSolve, 
			sn, 
			starttime, 
			kind
			)
		VALUES
			((SELECT NAME FROM xserver.t_dc WHERE sn = #sn#), 
			'0', 
			#sn#, 
			#starttime#, 
			'拥堵报警'
			);
		<selectKey resultClass="int" keyProperty="id" >  
		          SELECT @@IDENTITY AS id  
		</selectKey> 
</insert>

2、ibatis的xml中如果想支持中文插入查询等,要将xml的编码改为gbk

<?xml version="1.0" encoding="GBK" ?>

3、使用like模糊查询时要这样写

 (card_info.name like '%$personName$%')

4、动态拼接条件语句,如传入的某一字段不为空时才加入下面的一句条件,详细的dynamic的用法可自行查找

where              
    record_log.recordtime>= #begintime# 
            ]]>
<dynamic>
            <isNotEmpty prepend="AND" property="personName">
            (card_info.name like '%$personName$%')
            </isNotEmpty>

</dynamic> 

5、ibatis中的blob和java中的byte[]对应的写法应为"[B",不过一般不需要指定

<parameterMap id="securityPara" class="security">
	    <parameter property="pictureID"  jdbcType="VARCHAR" javaType="java.lang.Long"></parameter>
	    <parameter property="picture"  jdbcType="BLOB" javaType="[B"></parameter>
</parameterMap>

6、映射实体中还包含另一个对象

public class TGisMenu {
	private Integer id;
	private String notification;
	private String label;
	private String imageiconurl;
	private String tooltip;
	private String layerlabel;
	private String layerurl;
	private String reserve;
	private Integer parentid;
	private Integer application;
	private Module module;// 为另一个自定义对象

那么,在xml文件中这样定义

<resultMap id="gisMenu" class="com.wynlink.bean.TGisMenu">
		<result property="id" column="id" />
		<result property="notification" column="notification" />
		<result property="label" column="label" />
		<result property="imageiconurl" column="imageiconurl" />
		<result property="tooltip" column="tooltip" />
		<result property="layerlabel" column="layerlabel" />
		<result property="layerurl" column="layerurl" />
		<result property="reserve" column="reserve" />
		<result property="parentid" column="parentid" />
		<result property="application" column="application" />
		<result property="module.id" column="tm_id" />
		<result property="module.name" column="tm_name" />
		<result property="module.url" column="tm_url" />
		<result property="module.type" column="tm_type" />
		<result property="module.group" column="tm_group" />
		<result property="module.param" column="tm_param" />
		<result property="module.events" column="tm_events" />
		<result property="module.jar" column="tm_jar" />
		<result property="module.depth" column="tm_depth" />
	</resultMap>
<select id="feachByUserIdAndApplication" parameterClass="java.util.Map" resultMap="gisMenu">

这里的sql语句查询返回的字段名一定要和resultMap中的column对应上,这样就可以实现所需的要求了

抱歉!评论已关闭.