MyBatis的标签属性

<!--column不做限制,可以为任意表的字段,而property必须type定义的pojo属性-->
<resultMap id="唯一的标识" type="映射的pojo对象">
<id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型"
property="映射pojo对象的主键属性"/>
<result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型"
property="映射pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>
<association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">
<id column="关联pojo对象对应表的主键字段" javaType="字段类型"
property="关联pojo对象的主键属性"/>
<result column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
</association>
<!--集合中的property须为oftype定义的pojo对象的属性-->
<!--<collection property="pojo的集合属性" ofType="集合中的pojo对象">
<id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型"
property="集合中pojo对象主键属性"/>
<result column="可以为任意表的一个字段" jdbcType="字段类型"
property="集合中的pojo对象的属性"/>
</collection>-->
<collection property="pojo的集合属性" ofType="集合中的pojo对象"
select="pojo对象的Dao层方法"
column="集合中pojo对象对应的表的字段">
</collection>
</resultMap>

例:
Dao层
List<Menu> findId(Integer roleId);
<!--根据用户ID查询关联的角色-->
<select id="findId" parameterType="int" resultMap="findUser">
SELECT m.* FROM t_menu m,t_role_menu rm WHERE m.id = rm.menu_id AND role_id = #{roleId} and level = 1
</select>
<resultMap id="findUser" type="com.itheima.pojo.Menu">
<!-- <id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="linkUrl" property="linkUrl"></result>
<result column="path" property="path"></result>
<result column="priority" property="priority"></result>
<result column="description" property="description"></result>
<result column="icon" property="icon"></result>-->
<collection property="children" ofType="com.itheima.pojo.Menu"
select="com.itheima.dao.MenuDao.getMenuByParentId"
column="id">
</collection>
</resultMap>

<select id="getMenuByParentId" parameterType="int" resultType="com.itheima.pojo.Menu">
SELECT * FROM `t_menu` t WHERE t.`level`=2 AND t.`parentMenuId`=#{id}
</select>

 

 
上一篇:java开发规范《选自AliJava开发手册》(更新ing)


下一篇:mybatis: 参数处理