ibatis多表关联配置文件配置情况
今天刚进公司,领导让学一下ibatis,以前没有接触到,到网上搜一下,很多内容,基础单表操作很简单,多表关联还是挺麻烦的,网上有几个例子误导人,都是错,没有调试过都发到网上。真是害人啊
我有两个表
create table T_USER
(
id NUMBER(10) not null,
name VARCHAR2(50),
sex VARCHAR2(10)
);
create table T_ROLE
(
roleid NUMBER(10) not null,
rolename VARCHAR2(20),
userid NUMBER(10)
)
通过userid关联
1.通过sql关联查询
<sqlMap namespace="userrole">
<typeAlias alias="userrole" type="ibatis.UserRole" />
<resultMap id="getresultuserrole" class="userrole">
<result property="id" column="id" />
<result property="username" column="username" />
<result property="rolename" column="rolename" />
</resultMap>
<select id="getAllUserRole" resultClass="userrole">
select a.id id,a.name username,b.rolename rolename
from t_user a,t_role b where a.id=b.userid
</select>
//直接写sql关联还是挺简单
2.通过对象映射
<typeAlias alias="User" type="ibatis.User" />
<resultMap id="getresultuser" class="User">
<result property="id" column="id" />
<result property="name" column="name" />
<result property="sex" column="sex" />
<result property="rolelist" column="id" select="getRoleByUserid" />
</resultMap>
<typeAlias alias="role" type="ibatis.Role" />
<resultMap id="getResultRole" class="role">
<result property="roleid" column="roleid" />
<result property="rolename" column="rolename" />
<result property="users" column="userid" select="getUserByUserid"/></resultMap>
<select id="getRoleByUserid" parameterClass="java.lang.Long"
resultMap="getResultRole"> select * from t_role where userid=#value#
</select>
<select id="getUserByUserid" parameterClass="java.lang.Long" resultMap="getresultuser">
select * from t_user where id=#value#
</select>
注意:resultMap映射的对象
------解决方案--------------------呵呵,我前段时间也刚接触ibatis,没有真正的开发过呢..
------解决方案--------------------<resultMap id="getresultuserrole" class="userrole">
<result property="id" column="id" />
<result property="username" column="username" />
<result property="rolename" column="rolename" />
</resultMap>
<select id="getAllUserRole" resultClass="userrole">
select a.id id,a.name username,b.rolename rolename
from t_user a,t_role b where a.id=b.userid
</select>
resultClass="userrole"
LZ你确定不会报错?
-----