公司用ibaties 刚开始还以为一样。后来拿mybaties标签放进去 ibaties完全不认啊。这样一对多怎么办啊 没有connections啊 找了一些http://blog.csdn.net/zzcv_/article/details/1966869
提供的方法还是很满意的。
但是有一个地方致命啊
若是数据量稍微大点 5w再网上 这跟for循环有啥区别啊。是吧数据库往死里整啊。
<sqlMap namespace="test">
<typeAlias alias="Key" type="zzcv.dao.domain.Key"/>
<typeAlias alias="Lock" type="zzcv.dao.domain.Lock"/>
<resultMap id="KeyResult" class="Key">
<result property="id" column="id"/>
<result property="keyName" column="keyName"/>
<result property="lock" column="lockId" select="getLockById"/>
</resultMap>
<!-- 通过key表中保存的lock的id实现的一对一关联,ibatis会使用getLockById(lockId)的结果填充lock属性 -->
<resultMap id="LockResult" class="Lock">
<result property="id" column="id"/>
<result property="lockName" column="lockName"/>
<result property="keys" column="id" select="getKeysByLockId"/>
</resultMap>
<!-- 通过lock的id实现的一对多关联,ibatis会使用getKeysByLockId(id)得到的List填充keys属性 -->
<!-- 多对多可以通过嵌套实现,这里就不列出了 -->
<select id="selectAllkeys" resultMap="KeyResult">
<![CDATA[
select id,lockId,keyName from key
]]>
</select>
<select id="getLockById" parameterClass="int" resultClass="Lock">
<![CDATA[
select id,lockName from lock where id = #value#
]]>
</select>
<select id="selectAllLocks" resultMap="LockResult">
<![CDATA[
select id,lockName from lock
]]>
</select>
<select id="getKeyByLockId" parameterClass="int" resultClass="key">
<![CDATA[
select id,lockId,keyName from lock where lockId = #value#
]]>
</select>
</sqlMap>
后来又看到一个封装办法 http://cuishen.iteye.com/blog/544207 这种效率要高很多。
这样只用查询一条即可 注意数据量不能过多
- <sqlMap namespace="sys">
- <typeAlias alias="menu" type="com.cuishen.sqlmaptest.vo.Menu" />
- <resultMap id="sub-menu-map" class="menu">
- <result property="id" column="subid" />
- <result property="name" column="subname" />
- <result property="url" column="suburl" />
- <result property="layer" column="sublayer" />
- <result property="grade" column="subgrade" />
- <result property="position" column="subposition" />
- <result property="parentId" column="subparentid" />
- </resultMap>
- <resultMap id="menu-map" class="menu" groupBy="id">
- <result property="id" column="id" />
- <result property="name" column="name" />
- <result property="url" column="url" />
- <result property="layer" column="layer" />
- <result property="grade" column="grade" />
- <result property="position" column="position" />
- <result property="parentId" column="parentid" />
- <result property="subMenus" resultMap="sys.sub-menu-map" />
- </resultMap>
- <select id="getTopMenu" parameterClass="java.util.HashMap" resultMap="sys.menu-map">
- select
- t.id as id,t.name as name,t.url as url,t.layer as layer,t.grade as grade,t.position as position,
- t.parentid as parentid,s.id as subid,s.name as subname,s.url as suburl,s.layer as sublayer,
- s.grade as subgrade,s.position as subposition,s.parentid as subparentid
- from menu t left join menu s
- on t.id = s.parentid
- where t.layer=1 order by t.position, s.position
- </select>
- </sqlMap>