Mybatis基于XML配置SQL映射器(二)
Mybatis之XML注解
之前已经讲到通过 mybatis-generator 生成mapper映射接口和相关的映射配置文件:
下面我们将详细的讲解具体内容
首先我们新建映射接口文档 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);
} }
执行效果
【自动生成主键】
在上述的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);
}
测试效果
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);
}
测试效果
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());
}
测试效果
结果集映射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基于XML配置SQL映射器(二)的更多相关文章
- Mybatis基于XML配置SQL映射器(一)
Durid和Mybatis开发环境搭建 SpringBoot搭建基于Spring+SpringMvc+Mybatis的REST服务(http://www.cnblogs.com/nbfujx/p/76 ...
- Mybatis基于XML配置SQL映射器(三)
Mybatis之动态SQL mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: if choo ...
- mybatis 基于xml 配置的映射器
cache 给命名空间的缓存配置 cache-ref 其他命名空间缓存配置的引用 resultMap 描述如何从数据库结果集中来加载对象 <!--column不做限制,可以为任意表的字段,而p ...
- Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器
关系型数据库和SQL是经受时间考验和验证的数据存储机制.和其他的ORM 框架如Hibernate不同,MyBatis鼓励开发者可以直接使用数据库,而不是将其对开发者隐藏,因为这样可以充分发挥数据库服务 ...
- MyBatis学习笔记3--使用XML配置SQL映射器
<resultMap type="Student" id="StudentResult"> <id property="id&quo ...
- 小峰mybatis(5)mybatis使用注解配置sql映射器--动态sql
一.使用注解配置映射器 动态sql: 用的并不是很多,了解下: Student.java 实体bean: package com.cy.model; public class Student{ pri ...
- Mybatis基于接口注解配置SQL映射器(二)
Mybatis之增强型注解 MyBatis提供了简单的Java注解,使得我们可以不配置XML格式的Mapper文件,也能方便的编写简单的数据库操作代码.但是注解对动态SQL的支持一直差强人意,即使My ...
- 小峰mybatis(4)mybatis使用注解配置sql映射器
主流开发还是使用xml来配置:使用注解配置比较快,但是不支持所有功能:有些功能还是得用配置文件: 一.基本映射语句: @Inert @Update @Delete @Select 二.结果集映射语句 ...
- (三)使用XML配置SQL映射器
SqlSessionFactoryUtil.java package com.javaxk.util; import java.io.IOException; import java.io.Input ...
随机推荐
- 本地develop往远端develop上推代码步骤
- InputStream接口的常见实现类
一. FileInputStream FileInputStream可以从系统文件中获取输入字节,也从可以从诸从图象数据的的原始字节流中读取. 如果是读取字符串流,推荐使用FileReader. 感觉 ...
- MySQL执行计划之EXPLAIN基本解释说明
一.EXPLAIN使用潜规则 explain + sql语句 例如: EXPLAIN SELECT * FROM `t_user`; 二. 表头字段详解 (1) id-----> 表的读取顺序 ...
- python学习笔记(五)文件操作和集合
文件基本操作: 现有文件file.txt f=open('file.txt','r')#以只读方式打开一个文件,获取文件的句柄,如果是读的话,r可以不写,默认就是只读:文件不存在时,会报错 first ...
- python学习笔记(一)python简介和基础
1.什么是python? python是一种面向对象的,解释型的计算机语言,它的特点是语法简介,优雅,简单易学.1989年诞生,Guido(龟叔)开发. 编译型语言:代码在编译之后,编译成2进制的文件 ...
- python3安装pdfminer并使用
1.python3不同与2版本不能使用pdfminer pip install pdfminer3k 2.使用pdfminer解析相应文档并保存到相应的文件夹中 # encoding : udf-8 ...
- scrapy工作流程
整个scrapy流程,我们可以用去超市取货的过程来比喻一下 两个采购员小王和小李开着采购车,来到一个大型商场采购公司月饼.到了商场之后,小李(spider)来到商场前台,找到服务台小花(引擎)并对她说 ...
- c++实现哈夫曼树,哈夫曼编码,哈夫曼解码(字符串去重,并统计频率)
#include <iostream> #include <iomanip> #include <string> #include <cstdlib> ...
- 电脑同时安装python2和python3 ,默认使用python3
python3环境变量放在python2前面: 此电脑--属性--高级系统设置--高级--环境变量--系统变量--Path--编辑 D:\Python36\Scripts;D:\Python36;C: ...
- spring cloud学习--feign
简单示例 增加feign maven依赖 <dependency> <groupId>org.springframework.cloud</groupId> < ...