1.插入
<insert id="insTable" resultClass="int"> INSERT INTO [dbo].[table] ([Id] ,[FId],[IsDel],[CreateTime] ) VALUES <iterate conjunction="," open="" close=""> (#[].Id# ,#[].FId# ,0 ,GETDATE() ) </iterate> </insert>
2.读取
<select id="queryTable" resultMap="Table" parameterClass="List"> select Id, FId, CreateTime from Table (nolock) where IsDel=1 and [Id] in <iterate open="(" close=")" conjunction=","> #[]# </iterate> </select>
3.修改
<update id="updateTable" parameterClass="list"> begin <iterate conjunction=""> update TABLE set Fid=#[].FId# where id = #[].Id#; </iterate> end; </update>
var tables = new List<Table>() { {new Table() {Id = 1, FId = "100"}}, {new Table() {Id = 2, FId = "102"}}, }; this.mapper.Update("updateTable", tables);
4.删除
<delete id="delTable" parameterClass="List"> delete from TABLE where id in <iterate conjunction="," open="(" close=")"> #[]# </iterate> </delete>
var ids=new List<int>(){2,3,4}; this.mapper.Delete("delTable", ids);
5.类里字段列表插入(注意iterate 里多了一个property )
<insert id="insSaleOrderDetails" parameterClass="SaleOrderDetailModel" resultClass="int"> INSERT INTO [dbo].[SaleDetail] ( [OrderId] ,[PId] ,[Price] ,[Quantity] ,[Amount] ,[CreateDate] ,[CreateTime] ,[Creator] ,[IsDel] ) VALUES <iterate conjunction="," open="" close="" property="Details"> (#SaleId# ,#Details[].PId# ,#Details[].Price# ,#Details[].Quantity# ,#Details[].Amount# ,getdate() ,getdate() ,#Creator# ,0 ) </iterate> </insert>
6.ibatis的比较运算
<isEqual> 相等。 <isNotEqual> 不等。 <isGreaterThan> 大于 <isGreaterEqual> 大于等于 <isLessThan> 小于 <isLessEqual> 小于等于 <isEqual property="Status" compareValue="1"> and Status>0 </isEqual> <isEqual property="sort_onlinetime" compareValue="asc"> order by u.online_time asc </isEqual>
7.直接传入一个List
<!--列表查询需显示内容--> <select id ="qryDataList" parameterClass="list" resultClass="DataModel"> select * from DataConfig a (NOLOCK) join data b (NOLOCK) on a.dataid=b.id where a.isdel=0 and b.isdel=0 <isNotNull prepend="AND"> ModuleId in <iterate conjunction="," open="(" close=")"> #[]# </iterate> </isNotNull> </select>