mybatis自己容易忘记的东西2(仅适自己)



if和where:xxx.yy表示从vo中读取属性值

<where>可以自动取出第一个and

<where>

<iftest="xxx!=null">

<iftest="xxx.yy!=null and xxx.yy!=''">

and····

</if>

<iftest="xxx.zz!=null and xxx.zz!=''">

and name=${xxx.zz}

</if>

</if>

</where>

建议对单表的东西进行单独抽取sql

<sqlid="">

列名

</sql>

引用:如果不在同一个文件就是namespace+id

<includerefid="">

</include>

foreach的使用

collection:集合的属性

open:开始循坏拼接的串

close:结束循坏拼接的串

item:每次循环所取到的对象

separator:循环中间拼接的串

and (id=16,id=22,id=15)

<foreachcollection="ids"open="and ("close=")"item="id"seporator=",">

#{id}

</foreach>

resultMap一对一Orders类中加入一个User类属性

<resultMapid="aaaaaaaaaaaa"type="orders">

<idcolumn=""property="">

<resultcolumn=""property="">

····

association:用户对关联的单条记录映射

property:要将关联信息映射到orders的哪个属性中

javaType:关联信息映射到orders的属性的类型,是user的类型

<association property="属性名" javaType="属性类型名">

<id column="" property=""/>

user表的唯一标识

<result column="" property=""/>

</association

></resultMap>

resultMap一对多orders类中加入属性List<OrderDetail> orderDetails

可以用extend继承只写没有的,也可以不继承就需要全部写ofType:集合中pojo的类型

<resultMapid=""type=""extend="aaaaaaaaaaaa">

<collectionproperty="属性名"ofType="集合中pojo类型">

<idcolumn=""property=""/>

<resultcolumn=""property="">

</collection></resultMap>

resultMap一对多复杂的查询:

user类中加入List<Order> orderlist(list)

Order中有List<OrderDetail> orderDetails(list)

OrderDetail中有item属性(pojo)

class user{ int id; ···· List<Order> orderlist}

class Order{ ···· List<OrderDetail> orderDetails;}

class OrderDetail{ ···· private Item item;}

resultMap:实现延迟加载用这个,为了方便

<resultMapid=""type="user">

<idcolumn=""property="">

<resultcolumn=""property="">

····

<!--List<Order> orderlist(list)-->

<collectionproperty="orderlist"javaType="">

<idcolumn=""property="">

<resultcolumn=""property="">

`````

<collectionproperty="orderDetails"ofType="">

<idcolumn=""property="">

<resultcolumn=""property="">

`````

<assoctionproperty="item"javaType="">

<idcolumn=""property="">

<resultcolumn=""property="">

`````

</assoction>

</collection>

</collection>

</resultMap>

延迟加载的配置:在sqlMapConfig中配置

<settings>

<settingname="lazyLoadingEnabled"value="true"/>打开延迟加载总开关(默认为false)

<settingname="aggressiveLazyLoading"value="false">要设为false才是按需加载(可实现延迟加载)(默认为true) 缺一不可

</settings>

区别:resultMap和resultType有简单的现成的用resultType方便获取关联信息用resultMap

if和where: xxx.yy表示从vo中读取属性值 <where>可以自动取出第一个and <where> <iftest="xxx!=null"> <iftest="xxx.yy!=null and xxx.yy!=''"> and···· </if> <iftest="xxx.zz!=null and xxx.zz!=''"> and name=${xxx.zz} </if> </if> </where> 建议对单表的东西进行单独抽取sql <sqlid=""> 列名 </sql> 引用:如果不在同一个文件就是namespace+id <includerefid=""> </include> foreach的使用 collection:集合的属性 open:开始循坏拼接的串 close:结束循坏拼接的串 item:每次循环所取到的对象 separator:循环中间拼接的串 and (id=16,id=22,id=15) <foreachcollection="ids"open="and ("close=")"item="id"seporator=","> #{id} </foreach> resultMap一对一 Orders类中加入一个User类属性 <resultMapid="aaaaaaaaaaaa"type="orders"> <idcolumn=""property=""> <resultcolumn=""property=""> ···· association:用户对关联的单条记录映射 property:要将关联信息映射到orders的哪个属性中 javaType:关联信息映射到orders的属性的类型,是user的类型 <association property="属性名" javaType="属性类型名"> <id column="" property=""/>user表的唯一标识 <result column="" property=""/> </association> </resultMap> resultMap一对多 orders类中加入属性List<OrderDetail> orderDetails 可以用extend继承只写没有的,也可以不继承就需要全部写 ofType:集合中pojo的类型 <resultMapid=""type=""extend="aaaaaaaaaaaa"> <collectionproperty="属性名"ofType="集合中pojo类型"> <idcolumn=""property=""/> <resultcolumn=""property=""> </collection> </resultMap> resultMap一对多复杂的查询: user类中加入List<Order> orderlist(list) Order中有List<OrderDetail> orderDetails(list) OrderDetail中有item属性(pojo) class user{ int id; ···· List<Order> orderlist } class Order{ ···· List<OrderDetail> orderDetails; } class OrderDetail{ ···· private Item item; } resultMap:实现延迟加载用这个,为了方便 <resultMapid=""type="user"> <idcolumn=""property=""> <resultcolumn=""property=""> ···· <!--List<Order> orderlist(list)--> <collectionproperty="orderlist"javaType=""> <idcolumn=""property=""> <resultcolumn=""property=""> ````` <collectionproperty="orderDetails"ofType=""> <idcolumn=""property=""> <resultcolumn=""property=""> ````` <assoctionproperty="item"javaType=""> <idcolumn=""property=""> <resultcolumn=""property=""> ````` </assoction> </collection> </collection> </resultMap> 延迟加载的配置: 在sqlMapConfig中配置 <settings> <settingname="lazyLoadingEnabled"value="true"/>打开延迟加载总开关(默认为false) <settingname="aggressiveLazyLoading"value="false">要设为false才是按需加载(可实现延迟加载)(默认为true) 缺一不可 </settings> 区别:resultMap和resultType 有简单的现成的用resultType 方便获取关联信息用resultMap

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值