zoukankan      html  css  js  c++  java
  • mybatis配置文件resultMap标签的使用

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

           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(5) primary key,
        parentId int(5),
        name varchar(10),
        enName varchar(10)
    )

    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

  • 相关阅读:
    负载均衡算法
    .NET Core 如何上传文件及处理大文件上传
    ado.net 批量添加 更新 删除
    asp.net mvc session锁问题
    C# Cache 设定过期时间的方法
    C# 开源项目
    Linux上 发布.Net Core
    Roslyn
    .net core 基础知识
    神经网络基础
  • 原文地址:https://www.cnblogs.com/zjdxr-up/p/8675194.html
Copyright © 2011-2022 走看看