码迷,mamicode.com
首页 > 数据库 > 详细

IBATIS动态SQL--<dynamic>select@@Identity

时间:2014-07-21 10:23:10      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   os   io   

一、<dynamic><isNotNull>

<!-- select 基本语句 -->
    <select id="tbhyScenarioSetting.select" parameterClass="tbhyScenarioSettingDomain" resultClass="tbhyScenarioSettingDomain">
        select id as id,
        gmt_create as gmtCreate,
        gmt_modified as gmtModified,
        scenario_name as scenarioName,
        layout_type as layoutType,
        industry_id as industryId,
        creator as creator,
        modifier as modifier,
        max_item_per_category as maxItemPerCategory,
        max_item_per_seller as maxItemPerSeller,
        max_item_per_brand as maxItemPerBrand,
        max_item_per_spu as maxItemPerSpu,
        scene_id as sceneId
        from tbhy_scenario_setting where 1=1
        <dynamic>
            <isNotNull property="sceneId" prepend="and">
                scene_id=#sceneId#
            </isNotNull>
            <isNotNull property="id" prepend="and">
                id=#id#
            </isNotNull>
        </dynamic>
    </select>
1)如果id字段不為null,就加在isNotNull标签语句前加上“and”。

2)<dynamic>标签会覆盖子标签中的prepend,上例中,<dynamic>中没有prepend,所以子标签中and会保留。

此处的用法是where 1=1,然后是动态sql。

还可以<dynamic prepend = "where">,这样可以覆盖子标签中的and,使其变成where。

二、select@@Identity

<!-- insert into 基本语句 -->
    <insert id="tbhyScenarioSetting.insert" parameterClass="tbhyScenarioSettingDomain">
        insert into tbhy_scenario_setting(scene_id,gmt_create,gmt_modified,scenario_name,layout_type,industry_id,creator,modifier,max_item_per_category,max_item_per_seller,max_item_per_brand,max_item_per_spu)
        values(#sceneId#,NOW(),NOW(),#scenarioName#,#layoutType#,#industryId#,#creator#,#modifier#,#maxItemPerCategory#,#maxItemPerSeller#,#maxItemPerBrand#,#maxItemPerSpu#);
        <selectKey resultClass="Long" keyProperty="id">
            SELECT @@identity AS id
        </selectKey>
    </insert>

利用select@@Identity获取新增记录ID。

使用:

public TbhyScenarioSettingDomain add(final TbhyScenarioSettingDomain domain) {
        return (TbhyScenarioSettingDomain) transactionTemplate.execute(new TransactionCallback() {
            @Override
            public Object doInTransaction(TransactionStatus status) {
                Long id = (Long) getSqlMapClientTemplate().insert("tbhyScenarioSetting.insert", domain);
                domain.setId(id);
                addSceneTopicsAndTabs(domain);
                return domain;
            }
        });
    }

to be continued

IBATIS动态SQL--<dynamic>select@@Identity,布布扣,bubuko.com

IBATIS动态SQL--<dynamic>select@@Identity

标签:style   blog   color   使用   os   io   

原文地址:http://www.cnblogs.com/xianghan/p/3856048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!