Mybatis基于XML配置SQL映射器(二)

Mybatis之XML注解

之前已经讲到通过 mybatis-generator 生成mapper映射接口和相关的映射配置文件:

Mybatis基于XML配置SQL映射器(二)

下面我们将详细的讲解具体内容

首先我们新建映射接口文档  sysUserExtMapper.java,同时新增相关配置文件sysUserExtMapper.xml。通过操作这两个类我们来讲解具体内容。

映射语句

MyBatis提供了多种元素来配置不同类型的语句,如SELECT,INSERT,UPDATE,DELETE。接下来让我们看看如何具体配置映射语句。

INSERT语句

一个INSERT SQL语句可以在<insert>元素在sysUserExtMapper.xml中配置,如下所示:

     <insert id="insertsysUser" parameterType="com.goku.mybatis.model.sysUser">
insert into sys_user (id, username, password,
name, sex, status, org_id,
email, idcard, is_admin,
sort, mobile, stationid
)
values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{status,jdbcType=CHAR}, #{orgId,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{idcard,jdbcType=VARCHAR}, #{isAdmin,jdbcType=VARCHAR},
#{sort,jdbcType=BIGINT}, #{mobile,jdbcType=VARCHAR}, #{stationid,jdbcType=LONGVARCHAR}
)
</insert>

同时在 sysUserExtMapper.java接口中添加相应的方法

 package com.goku.mybatis.mapper.ext;

 import com.goku.mybatis.model.sysUser;
import org.apache.ibatis.annotations.Param; import java.util.List;
import java.util.Map; /**
* Created by nbfujx on 2017/10/14.
*/
public interface sysUserExtMapper { int insertsysUser(sysUser sysuser));
}

增加相对应单元测试查看相关效果

 package test.com.goku.mybatis.mapper.ext;

 import com.goku.mybatis.WebapiApplication;
import com.goku.mybatis.mapper.ext.sysUserExtMapper;
import com.goku.mybatis.model.sysUser;
import com.goku.mybatis.service.impl.sysUserServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.*; /**
* Created by nbfujx on 2017/10/23.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = WebapiApplication.class)
public class sysUserExtMapperTest { private Logger logger = LoggerFactory.getLogger(sysUserExtMapperTest.class); @Autowired
private sysUserExtMapper sysuserextmapper; @Test
public void insertsysUser() throws Exception {
sysUser sysuser=new sysUser();
sysuser.setId("222");
sysuser.setOrgId("2");
int i=sysuserextmapper.insertsysUser(sysuser);
this.logger.info("执行成功个数:"+i);
} }

执行效果

Mybatis基于XML配置SQL映射器(二)

【自动生成主键】

在上述的INSERT语句中,我们为可以自动生成(auto-generated)主键的列 id 插入值。

我们可以使用useGeneratedKeys 和 keyProperty属性让数据库生成AUTO_INCREMENT列的值,并将生成的值设置到其中一个输入对象属性内,如下所示:

     <insert id="insertsysUser2" parameterType="com.goku.mybatis.model.sysUser" useGeneratedKeys="true"
keyProperty="id">
insert into sys_user ( username, password,
name, sex, status, org_id,
email, idcard, is_admin,
sort, mobile, stationid
)
values ( #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{status,jdbcType=CHAR}, #{orgId,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{idcard,jdbcType=VARCHAR}, #{isAdmin,jdbcType=VARCHAR},
#{sort,jdbcType=BIGINT}, #{mobile,jdbcType=VARCHAR}, #{stationid,jdbcType=LONGVARCHAR}
)
</insert>

有些数据库如Oracle并不支持 AUTO_INCREMENT 列,其使用序列(SEQUENCE),或者其他查询(uuid)来生成主键值,如下所示:

  <insert id="insertsysUser3" parameterType="com.goku.mybatis.model.sysUser">
<selectKey keyProperty="id" resultType="java.lang.String" order="BEFORE">
SELECT replace(uuid(),'-','') AS id
</selectKey>
insert into sys_user (id, username, password,
name, sex, status, org_id,
email, idcard, is_admin,
sort, mobile, stationid
)
values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{status,jdbcType=CHAR}, #{orgId,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{idcard,jdbcType=VARCHAR}, #{isAdmin,jdbcType=VARCHAR},
#{sort,jdbcType=BIGINT}, #{mobile,jdbcType=VARCHAR}, #{stationid,jdbcType=LONGVARCHAR}
)
</insert>
UPDATE 语句

一个UPDATE SQL语句可以在<update>元素在映射器XML配置文件中配置,如下所示:

     <update id="updateUser" parameterType="com.goku.mybatis.model.sysUser">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update sys_user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
status = #{status,jdbcType=CHAR},
org_id = #{orgId,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
idcard = #{idcard,jdbcType=VARCHAR},
is_admin = #{isAdmin,jdbcType=VARCHAR},
sort = #{sort,jdbcType=BIGINT},
mobile = #{mobile,jdbcType=VARCHAR},
stationid = #{stationid,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>

同时在映射器接口中添加相应的方法

     int updateUser(sysUser sysuser);

增加相对应单元测试查看相关效果

  @Test
public void updateUser() throws Exception {
sysUser sysuser=sysuserextmapper.selectByUsername("1");
sysuser.setPassword("3");
sysuser.setOrgId("2");
int i=sysuserextmapper.updateUser(sysuser);
this.logger.info("执行成功个数:"+i);
}

测试效果

Mybatis基于XML配置SQL映射器(二)

DELETE 语句

一个DELETE SQL语句可以在<delete>元素在映射器XML配置文件中配置,如下所示:

     <delete id="deleteByUserName" parameterType="java.lang.String">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
delete from sys_user
where username = #{username,jdbcType=VARCHAR}
</delete>

同时在映射器接口中添加相应的方法

     int deleteByUserName(@Param("username") String username);

增加相对应单元测试查看相关效果

  @Test
public void deleteByUserName() throws Exception {
int i=sysuserextmapper.deleteByUserName("1");
this.logger.info("执行成功个数:"+i);
}

测试效果

Mybatis基于XML配置SQL映射器(二)

SELECT 语句

一个SELECT SQL语句可以在<select>元素在映射器XML配置文件中配置,如下所示:

    <select id="selectByUsername" parameterType="java.lang.String" resultMap="BaseResultExtMap">
select
<include refid="Base_Ext_Column_List" />
from sys_user
where username = #{username,jdbcType=VARCHAR}
</select>

同时在映射器接口中添加相应的方法

     sysUser selectByUsername(String username);

增加相对应单元测试查看相关效果

   @Test
public void selectByUsername() throws Exception {
sysUser user= sysuserextmapper.selectByUsername("444");
System.out.println(user.getId());
}

测试效果

Mybatis基于XML配置SQL映射器(二)

结果集映射ResultMaps

简单ResultMap

在<select>语句中,我们使用了resultMap属性,而不是resultType来引用映射。

当<select>语句中配置了resutlMap属性,MyBatis会使用此数据库列名与对象属性映射关系来填充JavaBean中的属性。

  <select id="selectByUsername" parameterType="java.lang.String" resultMap="BaseResultExtMap">
select
<include refid="Base_Ext_Column_List" />
from sys_user
where username = #{username,jdbcType=VARCHAR}
</select>

让我们来看另外一个<select>映射语句定义的例子,怎样将查询结果填充到HashMap中。如下所示:

     <select id="selectUserByUsername"  resultType="java.util.HashMap">
select username,password from sys_user where username = #{username,jdbcType=VARCHAR}
</select>

在上述的<select>语句中,我们将resultType配置成map,即java.util.HashMap的别名。在这种情况下,结果集的列名将会作为Map中的key值,而列值将作为Map的value值。

映射器接口中添加相应的方法

     Map<String, String> selectUserByUsername(@Param("username") String username);

让我们再看一个 使用resultType=”map”,返回多行结果的例子:

     <select id="selectUserByOrgid"  resultType="java.util.HashMap">
select username,password from sys_user where org_id = #{orgid,jdbcType=VARCHAR}
<if test="orderFiled != null" >
order by ${orderFiled}
<if test="orderSort != null" >
${orderSort}
</if>
</if>
</select>

映射器接口中添加相应的方法

 List<Map<String, String>> selectUserByOrgid(@Param("orgid") String orgid, @Param("orderSort") String orderSort, @Param("orderFiled") String orderFiled);
拓展ResultMap

首先我们要在 sys_User中增加 sys_user_info对象。下面同理

  private sysUserInfo sysuserinfo;

     public sysUserInfo getSysuserinfo() {
return sysuserinfo;
} public void setSysuserinfo(sysUserInfo sysuserinfo) {
this.sysuserinfo = sysuserinfo;
}

我们可以从从另外一个<resultMap>,拓展出一个新的<resultMap>,这样,原先的属性映射可以继承过来,以实现。

   <resultMap id="UserinfoBaseResultExtMap" type="com.goku.mybatis.model.sysUser" extends="BaseResultExtMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<result column="address" jdbcType="VARCHAR" property="sysuserinfo.address" />
<result column="post_code" jdbcType="VARCHAR" property="sysuserinfo.postCode" />
<result column="height" jdbcType="VARCHAR" property="sysuserinfo.height" />
<result column="weight" jdbcType="VARCHAR" property="sysuserinfo.weight" />
<result column="birthday" jdbcType="DATE" property="sysuserinfo.birthday" />
<result column="blood" jdbcType="VARCHAR" property="sysuserinfo.blood" />
<result column="culture" jdbcType="VARCHAR" property="sysuserinfo.culture" />
<result column="finish_school_date" jdbcType="DATE" property="sysuserinfo.finishSchoolDate" />
<result column="folk" jdbcType="VARCHAR" property="sysuserinfo.folk" />
<result column="government" jdbcType="VARCHAR" property="sysuserinfo.government" />
<result column="homepage" jdbcType="VARCHAR" property="sysuserinfo.homepage" />
<result column="householder" jdbcType="VARCHAR" property="sysuserinfo.householder" />
<result column="marriage" jdbcType="VARCHAR" property="sysuserinfo.marriage" />
<result column="msn" jdbcType="VARCHAR" property="sysuserinfo.msn" />
<result column="nativity_address" jdbcType="VARCHAR" property="sysuserinfo.nativityAddress" />
<result column="qq" jdbcType="VARCHAR" property="sysuserinfo.qq" />
<result column="speciality" jdbcType="VARCHAR" property="sysuserinfo.speciality" />
<result column="description" jdbcType="VARCHAR" property="sysuserinfo.description" />
<result column="version" jdbcType="BIGINT" property="sysuserinfo.version" />
</resultMap >

扩展方法来使用它

  <select id="selectextends" parameterType="java.lang.String" resultMap="UserinfoBaseResultExtMap">
select
u.id, username, password, name, sex, status, org_id, email, idcard, is_admin, sort,
mobile,address, post_code, height, weight, birthday, blood, culture, finish_school_date,
folk, government, homepage, householder, marriage, msn, nativity_address, qq, speciality,
description, version
from sys_user u LEFT OUTER JOIN sys_user_info ui ON u.id=ui.id
where username = #{username,jdbcType=VARCHAR}
</select>

一对一映射

使用嵌套结果ResultMap实现一对一关系映射

在我们的域模型样例中,每一个用户都有一个与之关联的用户扩展信息。表sys_user有一个id列,是sys_user_info表的外键。我们定义一个resultMap中,sys_user_info的属性使用了圆点记法被赋上了对应列的值。

  <resultMap id="BaseResultExtMapext" type="com.goku.mybatis.model.sysUser">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="sex" jdbcType="VARCHAR" property="sex" />
<result column="status" jdbcType="CHAR" property="status" />
<result column="org_id" jdbcType="VARCHAR" property="orgId" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="idcard" jdbcType="VARCHAR" property="idcard" />
<result column="is_admin" jdbcType="VARCHAR" property="isAdmin" />
<result column="sort" jdbcType="BIGINT" property="sort" />
<result column="mobile" jdbcType="VARCHAR" property="mobile" />
<result column="address" jdbcType="VARCHAR" property="sysuserinfo.address" />
<result column="height" jdbcType="VARCHAR" property="sysuserinfo.height" />
<result column="weight" jdbcType="VARCHAR" property="sysuserinfo.weight" />
<result column="birthday" jdbcType="DATE" property="sysuserinfo.birthday" />
<result column="blood" jdbcType="VARCHAR" property="sysuserinfo.blood" />
<result column="culture" jdbcType="VARCHAR" property="sysuserinfo.culture" />
<result column="finish_school_date" jdbcType="DATE" property="sysuserinfo.finishSchoolDate" />
<result column="folk" jdbcType="VARCHAR" property="sysuserinfo.folk" />
<result column="government" jdbcType="VARCHAR" property="sysuserinfo.government" />
<result column="homepage" jdbcType="VARCHAR" property="sysuserinfo.homepage" />
<result column="householder" jdbcType="VARCHAR" property="sysuserinfo.householder" />
<result column="marriage" jdbcType="VARCHAR" property="sysuserinfo.marriage" />
<result column="msn" jdbcType="VARCHAR" property="sysuserinfo.msn" />
<result column="nativity_address" jdbcType="VARCHAR" property="sysuserinfo.nativityAddress" />
<result column="qq" jdbcType="VARCHAR" property="sysuserinfo.qq" />
<result column="speciality" jdbcType="VARCHAR" property="sysuserinfo.speciality" />
<result column="description" jdbcType="VARCHAR" property="sysuserinfo.description" />
<result column="version" jdbcType="BIGINT" property="sysuserinfo.version" />
</resultMap >

扩展方法来使用它

     <select id="selectextends2" parameterType="java.lang.String" resultMap="BaseResultExtMapext">
select
u.id, username, password, name, sex, status, org_id, email, idcard, is_admin, sort,
mobile,address, post_code, height, weight, birthday, blood, culture, finish_school_date,
folk, government, homepage, householder, marriage, msn, nativity_address, qq, speciality,
description, version
from sys_user u LEFT OUTER JOIN sys_user_info ui ON u.id=ui.id
where username = #{username,jdbcType=VARCHAR}
</select>
使用嵌套Select语句实现一对一关系映射

上述样例展示了一对一关联映射的一种方法。然而,使用这种方式映射,如果sysuserinfo结果需要在其他的SELECT映射语句中映射成Address对象,我们需要为每一个语句重复这种映射关系。MyBatis提供了更好地实现一对一关联映射的方法:嵌套结果ResultMap和嵌套select查询语句。

嵌套结果ResultMap

       <resultMap id="userinfoBaseResultMap" type="com.goku.mybatis.model.sysUserInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="post_code" jdbcType="VARCHAR" property="postCode" />
<result column="height" jdbcType="VARCHAR" property="height" />
<result column="weight" jdbcType="VARCHAR" property="weight" />
<result column="birthday" jdbcType="DATE" property="birthday" />
<result column="blood" jdbcType="VARCHAR" property="blood" />
<result column="culture" jdbcType="VARCHAR" property="culture" />
<result column="finish_school_date" jdbcType="DATE" property="finishSchoolDate" />
<result column="folk" jdbcType="VARCHAR" property="folk" />
<result column="government" jdbcType="VARCHAR" property="government" />
<result column="homepage" jdbcType="VARCHAR" property="homepage" />
<result column="householder" jdbcType="VARCHAR" property="householder" />
<result column="marriage" jdbcType="VARCHAR" property="marriage" />
<result column="msn" jdbcType="VARCHAR" property="msn" />
<result column="nativity_address" jdbcType="VARCHAR" property="nativityAddress" />
<result column="qq" jdbcType="VARCHAR" property="qq" />
<result column="speciality" jdbcType="VARCHAR" property="speciality" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="version" jdbcType="BIGINT" property="version" />
</resultMap>
<resultMap type="com.goku.mybatis.model.sysUser" id="BaseResultExtMapext3">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="sex" jdbcType="VARCHAR" property="sex" />
<result column="status" jdbcType="CHAR" property="status" />
<result column="org_id" jdbcType="VARCHAR" property="orgId" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="idcard" jdbcType="VARCHAR" property="idcard" />
<result column="is_admin" jdbcType="VARCHAR" property="isAdmin" />
<result column="sort" jdbcType="BIGINT" property="sort" />
<result column="mobile" jdbcType="VARCHAR" property="mobile" />
<association property="sysuserinfo" resultMap="userinfoBaseResultMap" />
</resultMap>

相对应的扩展方法来使用它

     <select id="selectextends5" parameterType="java.lang.String" resultMap="BaseResultExtMapext3">
select
u.id, username, password, name, sex, status, org_id, email, idcard, is_admin, sort,
mobile,address, post_code, height, weight, birthday, blood, culture, finish_school_date,
folk, government, homepage, householder, marriage, msn, nativity_address, qq, speciality,
description, version
from sys_user u LEFT OUTER JOIN sys_user_info ui ON u.id=ui.id
where username = #{username,jdbcType=VARCHAR}
</select>

元素<association>被用来导入“有一个”(has-one)类型的关联。在上述的例子中,我们使用了<association>元素引用了另外的在同一个XML文件中定义的<resultMap>。也可以使用<association>定义内联的resultMap。

 <resultMap type="com.goku.mybatis.model.sysUser" id="BaseResultExtMapext4">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="sex" jdbcType="VARCHAR" property="sex" />
<result column="status" jdbcType="CHAR" property="status" />
<result column="org_id" jdbcType="VARCHAR" property="orgId" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="idcard" jdbcType="VARCHAR" property="idcard" />
<result column="is_admin" jdbcType="VARCHAR" property="isAdmin" />
<result column="sort" jdbcType="BIGINT" property="sort" />
<result column="mobile" jdbcType="VARCHAR" property="mobile" />
<association property="sysuserinfo" javaType="com.goku.mybatis.model.sysUserInfo">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="post_code" jdbcType="VARCHAR" property="postCode" />
<result column="height" jdbcType="VARCHAR" property="height" />
<result column="weight" jdbcType="VARCHAR" property="weight" />
<result column="birthday" jdbcType="DATE" property="birthday" />
<result column="blood" jdbcType="VARCHAR" property="blood" />
<result column="culture" jdbcType="VARCHAR" property="culture" />
<result column="finish_school_date" jdbcType="DATE" property="finishSchoolDate" />
<result column="folk" jdbcType="VARCHAR" property="folk" />
<result column="government" jdbcType="VARCHAR" property="government" />
<result column="homepage" jdbcType="VARCHAR" property="homepage" />
<result column="householder" jdbcType="VARCHAR" property="householder" />
<result column="marriage" jdbcType="VARCHAR" property="marriage" />
<result column="msn" jdbcType="VARCHAR" property="msn" />
<result column="nativity_address" jdbcType="VARCHAR" property="nativityAddress" />
<result column="qq" jdbcType="VARCHAR" property="qq" />
<result column="speciality" jdbcType="VARCHAR" property="speciality" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="version" jdbcType="BIGINT" property="version" />
</association>
</resultMap>

嵌套select查询语句

     <resultMap type="com.goku.mybatis.model.sysUser" id="BaseResultExtMapext2">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="sex" jdbcType="VARCHAR" property="sex" />
<result column="status" jdbcType="CHAR" property="status" />
<result column="org_id" jdbcType="VARCHAR" property="orgId" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="idcard" jdbcType="VARCHAR" property="idcard" />
<result column="is_admin" jdbcType="VARCHAR" property="isAdmin" />
<result column="sort" jdbcType="BIGINT" property="sort" />
<result column="mobile" jdbcType="VARCHAR" property="mobile" />
<association property="sysuserinfo" column="id" select="findsysUserInfoById"/>
</resultMap>

相对应的扩展方法来使用它

     <select id="findsysUserInfoById" parameterType="String"
resultMap="userinfoBaseResultMap">
SELECT * FROM sys_user_info where id = #{id,jdbcType=VARCHAR}
</select>
<select id="selectextends4" parameterType="java.lang.String" resultMap="BaseResultExtMapext2">
select * from sys_user where username = #{username,jdbcType=VARCHAR}
</select>

一对多映射

我们创建一张机构表一个机构包含多个用户。在sys_org中增加sys_user对象。

     public List<sysUser> getSysuser() {
return sysuser;
} public void setSysuser(List<sysUser> sysuser) {
this.sysuser = sysuser;
} private List<sysUser> sysuser;
使用内嵌结果ResultMap实现一对多映射
     <resultMap id="OrgBaseResultMapext1" type="com.goku.mybatis.model.sysOrg">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="version" jdbcType="BIGINT" property="version" />
<result column="level" jdbcType="BIGINT" property="level" />
<result column="type" jdbcType="CHAR" property="type" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="image" jdbcType="VARCHAR" property="image" />
<result column="isparent" jdbcType="VARCHAR" property="isparent" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="sort" jdbcType="BIGINT" property="sort" />
<result column="url" jdbcType="VARCHAR" property="url" />
<result column="parent_id" jdbcType="VARCHAR" property="parentId" />
<result column="operate" jdbcType="VARCHAR" property="operate" />
<collection property="sysuser" resultMap="userBaseResultMap" />
</resultMap>

相对应的扩展方法来使用它

     <select id="selectextend1" parameterType="java.lang.String" resultMap="OrgBaseResultMapext1">
select o.id, version, level, type, description, image, isparent, o.name, o.sort, url, parent_id,
u.id as uid, username, password, u.name, sex, status, org_id, email, idcard, is_admin, u.sort,
mobile from sys_org o LEFT OUTER JOIN sys_user u
on o.id=u.org_id where o.id = #{id,jdbcType=VARCHAR}
</select>

级联查询的时候,主表和从表有一样的字段名的时候,在mysql上命令查询是没问题的。但在mybatis中主从表需要为相同字段名设置别名。

<collection>元素被用来将多行结果映射成一个对象的一个集合。

使用嵌套Select语句实现一对多映射
  <resultMap id="OrgBaseResultMapext2" type="com.goku.mybatis.model.sysOrg">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="version" jdbcType="BIGINT" property="version" />
<result column="level" jdbcType="BIGINT" property="level" />
<result column="type" jdbcType="CHAR" property="type" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="image" jdbcType="VARCHAR" property="image" />
<result column="isparent" jdbcType="VARCHAR" property="isparent" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="sort" jdbcType="BIGINT" property="sort" />
<result column="url" jdbcType="VARCHAR" property="url" />
<result column="parent_id" jdbcType="VARCHAR" property="parentId" />
<result column="operate" jdbcType="VARCHAR" property="operate" />
<collection property="sysuser" column="id" select="findsysUserByOrgId" />
</resultMap>

相对应的扩展方法来使用它

     <select id="findsysUserByOrgId" parameterType="String"
resultMap="userBaseResultMap">
SELECT * FROM sys_user where org_id = #{id,jdbcType=VARCHAR}
</select>
<select id="selectextend2" parameterType="java.lang.String" resultMap="OrgBaseResultMapext2">
select * from sys_org where id = #{id,jdbcType=VARCHAR}
</select>

GITHUB

github :  https://github.com/nbfujx/learn-java-demo/tree/master/Goku.MybatisDemo.XML

上一篇:小峰mybatis(4)mybatis使用注解配置sql映射器


下一篇:[iOS]浅谈NSRunloop工作原理和相关应用