本文为博主原创,未经允许不得转载:

resultMap标签是为了映射select查询出来结果的集合,其主要作用是将实体类中的字段与

数据库表中的字段进行关联映射。

注意:当实体类中的字段与数据库表中的字段相同时,可以将resultMap标签中的关联关系

忽略不写。当实体类中的字段与数据库表中的字段不相同时,就需要在resultMap标签中将实体类

字段与数据库字段一 一进行关联映射。

举例如下:

1.实体类代码:

public class Test
{
private int id;
private String parentId;
private String name;
private String enName;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getParentId()
{
return parentId;
}
public void setParentId(String parentId)
{
this.parentId = parentId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getEnName()
{
return enName;
}
public void setEnName(String enName)
{
this.enName = enName;
} }

2.实体类字段与数据库表字段一致:

create table test1(
id int() primary key,
parentId int(),
name varchar(),
enName varchar()
)

3.关联查询映射resultMap使用:

 此处有两种写法:

第一种:将字段在resultMap标签中都进行映射。 

<resultMap type="com.test" id="testResultMap">
<!-- property对应实体类的属性名称,column为数据库结果集的列的名称 -->
<id property="id" column="id" />
<result property="parentId" column="parentId"/>
<result property="name" column="name"/>
<result property="enName" column="enName"/>
</resultMap> <select id="selectList"  resultMap="testResultMap">
        select * from test1
 </select>

第二种:由于字段与数据库字段相同,mybatis会自动进行匹配,可以写为一下方式:

<resultMap type="com.test" id="testResultMap">
</resultMap> <select id="selectList"  resultMap="testResultMap">
        select * from test1
</select>

4.实体类字段与数据库字段不一致:

create table test2(
    id int(5) primary key,
    parent_id int(5),
    name varchar(10),
    en_name varchar(10)
)

5.关联查询映射使用resultMap,由于实体类字段与数据库字段不一致,所以要将实体类字段与数据库字段在标签中

进行一一映射。

<!-- type指向你的javabean类,id可以自定义 -->
<resultMap type="Category" id="category">
<!-- property对应实体类的属性名称,column为数据库结果集的列的名称 -->
<id property="id" column="id" />
<result property="parentId" column="parent_id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="enName" column="en_name" jdbcType="VARCHAR"/>
</resultMap>
<select id="selectList"  resultMap="testResultMap">
        select * from test2
    </select>

特别提示:

  <!-- 是否开启自动驼峰命名规则(camel case)映射, -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
mybatis配置文件设置了这项后,查询出来的字段如果带下划线,那么就会去掉下划线,
然后采用java驼峰规则。比如数据库字段Parent_id,那么查询出来后,会转为parentid,
然后去实体类Category匹配对应的字段。 因为你实体类里有下划线,所以匹配不上。
要么采用resultMap 要么禁用掉驼峰规则(不建议禁用)。如果不想该实体类的话,
建议采用resultMap。

mybatis配置文件resultMap标签的使用的更多相关文章

  1. MyBatis之ResultMap标签

    ResultMap标签基本作用:建立SQL查询结果字段与实体属性的映射关系信息 在深入ResultMap标签前,我们需要了解从SQL查询结果集到JavaBean或POJO实体的过程. 1. 通过JDB ...

  2. MyBatis学习 之 四、MyBatis配置文件

    目录(?)[-] 四MyBatis主配置文件 properties属性 settings设置 typeAliases类型别名 typeHandlers类型句柄 ObjectFactory对象工厂 pl ...

  3. MyBatis学习(四)、MyBatis配置文件

    四.MyBatis主配置文件 在定义sqlSessionFactory时需要指定MyBatis主配置文件: <bean id="sqlSessionFactory" clas ...

  4. MyBatis学习 之 五、MyBatis配置文件

    在定义sqlSessionFactory时需要指定MyBatis主配置文件: <bean id="sqlSessionFactory" class="org.myb ...

  5. MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作

    一.getMapper()接口 解析:getMapper()接口 IDept.class定义一个接口, 挂载一个没有实现的方法,特殊之处,借楼任何方法,必须和小配置中id属性是一致的 通过代理:生成接 ...

  6. mybatis学习 十三 resultMap标签 一对一

    1 .<resultMap>标签 写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系. 在写<select>标签中,有一个resultType属性,此时s ...

  7. mybatis resultmap标签type属性什么意思

    mybatis resultmap标签type属性什么意思? :就表示被转换的对象啊,被转换成object的类型啊 <resultMap id="BaseResultMap" ...

  8. MyBatis配置文件中的标签mappers的子标签mapper的url属性

    在浏览器中输入file:/可以打开访达根目录,file:后面至少跟一个杠 MyBatis配置文件中的标签mappers的子标签mapper的url属性中file:后面至少要跟两个杠

  9. MyBatis之ResultMap的association和collection标签详解

    一.前言 MyBatis 创建时的一个思想是:数据库不可能永远是你所想或所需的那个样子. 我们希望每个数据库都具备良好的第三范式或 BCNF 范式,可惜它们并不都是那样. 如果能有一种数据库映射模式, ...

随机推荐

  1. 用django统计代码行数+注释行数

    实现统计代码行数: 1.首先在url.py中配置 from django.conf.urls import url from django.contrib import admin from app0 ...

  2. Autel MaxiSys Elite Diagnostic Tool Common problem solving methods

    1. updating MaxiFlash Elite to firmware 3.21? My maxisys communicate with the MaxiFlash J2534 but Ma ...

  3. The Little Prince-12/09

    The Little Prince-12/09 今天中文书评+自述奥! 也许世界上也有五千朵和你一模一样的花,但只有你是我独一无二的玫瑰. ————喵喵喵,多么美妙的一句表白词呀! 时间会缓和所有的悲 ...

  4. sqlalchemy 和 django 插入操作后自动返回自增ID

    result = session.execute('insert into ***') session.commit() last_insert_id = result.lastrowid 注意:如果 ...

  5. python之auto鼠标/键盘事件

    mouse_key.py import os import time import win32gui import win32api import win32con from ctypes impor ...

  6. curl 异步捉取数据类

    <?php class RequestLib { /** * GET 请求 * @param string $url */ public static function http_get($ur ...

  7. Maven 工程读取resource下的文件

    1:方式1: public static List<String> userList; static { userList = new LinkedList<String>() ...

  8. P4890 Never·island(dp)

    P4890 Never·island 求门开的最小时间,其实也就是求门关的最大时间. 坐标这么大....显然坐标要离散化 离散化排序后,我们发现x轴被这些点划分成若干条线段$(l,r)$,并且有4种情 ...

  9. bzoj1663: [Usaco2006 Open]赶集

    Description Every year, Farmer John loves to attend the county fair. The fair has N booths (1 <= ...

  10. python识别图片生成字符模式

    此python文件来自D7哥, 放在这里备份. 用法 python3 PIL\&argparse.py 1.jpg -o test.txt --width 300 --height 300 p ...