ibatis 使用学习入门

本文介绍了Ibatis框架的SQLMap XML映射文件配置方法,包括不同类型的Statement及其属性使用,ResultMap标签的应用,以及如何利用API进行数据库操作。同时,还探讨了编写SQL时的一些注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ibatis使用学习总结

一.SQL Map XML映射文件

1.  Statement类型

<statement>元素是个通用声明,可以用于任何类型的SQL语句。但是通常我们使用具体的statement类型。具体类型以及所具有属性如下图1.1所示:

statement类型

属性

<insert>

Id  parameterClass  parameterMap

<update>

Id  parameterClass  parameterMap

<select>

Id  parameterClass  parameterMap resultClass  resultMap  cacheModel

<delete>

Id  parameterClass  parameterMap

<procedure>

Id  parameterClass  parameterMap resultClass  resultMap  cacheModel

xmlResultName

图1.1 statement类型及对应属性

2.  属性的使用说明

parameterClass:属性的值是Java类的全限定名(即包括类的包名,也可使用<typeAlias>进行重命名),限制输入参数的类型为指定的Java类。

使用例子如下:

<typeAlias alias="livePushInfoDO"  type="com.taobao.trip.triplive.common.dal.dataobj.LivePushInfoDO"/>

<insert id="insert" parameterClass="livePushInfoDO">
REPLACE INTO live_push_info (id,  gmt_create,  gmt_modified, user_id,title, url) VALUES (#id#, now(), now(),  #userId#, #title#, #url#)
</insert>

resultClass:属性的值是Java类的全限定名(即包括类的包名),只要Java Bean的属性名称和ResultSet的列名匹配,属性自动赋值给列值。

使用例子如下:

<select id="getPushInfo" parameterClass="int" resultClass="livePushInfoDO">
SELECT PER_ID as id,
PER_FIRST_NAME as firstName,
PER_LAST_NAME as lastName,
PER_BIRTH_DATE as birthDate,
PER_WEIGHT_KG as weightInKilograms,
PER_HEIGHT_M as heightInMeters
FROM PERSON
WHERE PER_ID = #value#
</select>

resultMap: 最常用的属性之一,使用时首先定义<resultMap>标签,其次通过resultMap的定义,查询语句得到的ResultSet被映射成livePushInfoDO对象。如下例子中,resultMap定义的userId属性值将被赋予user_id字段值。

使用resultMap属性

<select id="getPushInfo" resultMap="livePushInfoDOResultMap">
select * from live_push_info
</select>
3.  resultMap标签

在SQL Map框架中,Result Map是极其重要的组件。resultMap负责将结果集的列值映射成Java Bean的属性值。

<resultMap>将数据库表的列映射为对应class的属性值

<resultMap id="livePushInfoDOResultMap" class="livePushInfoDO">
    <result property="id" column="id"/>
    <result property="gmtCreate" column="gmt_create"/>
    <result property="gmtModified" column="gmt_modified"/>
    <result property="userId" column="user_id"/>
    <result property="title" column="title"/>
    <result property="url" column="url"/>
    <result property="triggerTime" column="trigger_time"/>
    <result property="liveId" column="live_id"/>
    <result property="hostId" column="host_id"/>
    <result property="status" column="status"/>
</resultMap>

 

property 指的是返回结果对象的Java bean的属性名称,

column的值是resultSet中字段的名称,该字段赋值给指定的Java Bean属性

隐式resultMap:可以不用定义resultMap,只需将resultSet中的字段名称和resultClass对应Java bean类型属性名称相匹配即可。

二.SQL Map API

常用的函数如下:

insert()
@Override
public Boolean insertLiveInfo(LiveVideoDO liveVideoDO) {
    try {
        getSqlMapClientTemplate().insert("LiveVideo.insertLiveInfo",
                liveVideoDO);
    }catch (Exception e) {
        LoggerUtil.error(e, logger, "LiveVideoListDAOImpl insertLiveInfo Error liveVideoDO = {0}",
                JSON.toJSONString(liveVideoDO));
        return false;
    }
    return true;
}

 

update()
@Override
public Boolean updatePreLiveInfo(LiveVideoDO liveVideoDO) {
    try{
        getSqlMapClientTemplate().update("LiveVideo.updatePreLiveInfo", liveVideoDO);
    }catch (Exception e){
        LoggerUtil.error(e, logger, "LiveVideoListDAOImpl updatePreLiveInfo Error liveVideoDO = {0}");
        return false;
    }
    return true;
}

 

queryForList()
@Override
public List<LivePushInfoDO> queryByUserIdAndLiveId(Long userId, Long liveId) {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("userId", userId);
    params.put("liveId", liveId);
    params.put("status", 1);
    return getSqlMapClientTemplate().queryForList(
            "LivePushInfo.queryByUniqueKey", params);
}

 

三.Sql注意事项

1.  <![CDATA[]]>

Sql中的特殊符号,如(、)等,需要用<![CDATA[]]>包裹

2.  <dynamic>

dynamic可以去除第一个prepend="**"中的字符,从而可以帮助你实现一些很实用的功能。

3.  <isNotNull>
<update id="updateVideoByHostId" parameterClass="liveVideoDO">
     UPDATE live_video set gmt_modified = now()
     <dynamic>
         <isNotNull property="status" prepend=",">
             <![CDATA[
   status = #status#
]]>
         </isNotNull>
         <isNotNull property="feedType" prepend=",">
             <![CDATA[
   feed_type = #feedType#
]]>
         </isNotNull>
         <isNotNull property="showLevel" prepend=",">
             <![CDATA[
   show_level = #showLevel#
]]>
         </isNotNull>
     </dynamic>
     WHERE host_id = #hostId# AND env = #env#
 </update>

当某个属性为null时,则不进行更新,使用灵活,对于insert语句同样适用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值