|
在使用mybatis中的collection的时候遇到了很多问题,下面记录一下:
resultMap配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.benben.dao.ModuleDao">
<resultMap id="BaseResultMap" type="Module" >
<id column="module_id" property="id" />
<result column="module_name" property="name" />
<collection property="authUsers" ofType="Auth" resultMap="AuthUserMap"/>
</resultMap>
<resultMap id="AuthUserMap" type="Auth">
<id column="auth_id" property="id"/>
<result column="username" property="userName"/>
<result column="password" property="password"/>
</resultMap>
<select id="selectModule" resultMap="BaseResultMap">
select mo.id module_id ,mo.name module_name,au.id auth_id ,au.username username,au.password password from td_module mo left outer join td_auth au on au.module_id=mo.id
</select>
</mapper>
1、别名的使用,如
select mo.id module_id ,mo.name module_name,au.id auth_id ,au.username username,au.password password from td_module mo left outer join td_auth au on au.module_id=mo.id
这里千万要记住命名成别名,如果不用的话,可能两个表的字段名称一致,造成了匹配错误,如两张表中都叫id,就会造成匹配错误
2、ofType,这里是javabeen类 |
|
|