一、<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。
二、[email protected]@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>
利用[email protected]@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>[email protected]@Identity
时间: 2024-10-16 10:17:29