首页 > 代码库 > IBATIS动态SQL--<dynamic>select@@Identity
IBATIS动态SQL--<dynamic>select@@Identity
一、<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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。