4.mybatis属性和表的列名不相同时的处理方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4.mybatis属性和表的列名不相同时的处理方法相关的知识,希望对你有一定的参考价值。

/**
* 属性和表的列名不相同时的处理方法
* 1.sql中给列重新命名:
*    select tid id, tname name from teacher t where tid=#{id}
* 2.在teacherMapper.xml中使用resultMap标签
*    <select id="getTeacher2" parameterType="int" resultMap="getTeacherMap">
*     select * from teacher where tid=#{id}
*    </select>
*    <resultMap type="Teacher" id="getTeacherMap">
*      <id property="id" column="tid"/>
*      <result property="name" column="tname"/>
*    </resultMap>
* @author 魅力_小生
*
*/

 

mysql表:Teacher[tid, tname], Java 实体类:Teacher[id, name]

建表语句:

create table
CREATE TABLE `teacher` (
   `tid` int(11) NOT NULL auto_increment,
   `tname` varchar(20) default NULL,
   PRIMARY KEY  (`tid`)
 ) 

添加2条数据:
1 老师A
2 老师B

Teacher实体类:

public class Teacher {
    private int id;
    private String name;
    
    
    public Teacher() {
        super();
    }
    public Teacher(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Teacher [id=" + id + ", name=" + name + "]";
    }
    
}

一、第一种处理方法:sql中给列重新命名

select tid id, tname name from teacher t where tid=#{id}

1.1 写实体类关联的MapperXML:

<mapper namespace="com.mlxs.mybatis.test3.teacherMapper">
    <!-- 
     * 属性和表的列名不相同时的处理方法
      *         1.sql中给列重新命名:
      *             select tid id, tname name from teacher t where tid=#{id}
     -->
    <select id="getTeacher" parameterType="int" resultType="Teacher">
        select tid id, tname name from teacher t where tid=#{id}
    </select>
</mapper>

1.2 写java测试类

/**
     * 属性和表的列名不相同时的2种处理方法
     *         1.sql中给列重新命名:
     *             select tid id, tname name from teacher t where tid=#{id}
     * @author 魅力_小生
     *
     */
    @Test
    public void getTeacher(){
        //创建session,设置事务为true
        SqlSession session = MyBatisUtil.getSessionFactory().openSession(true);
        String statement = "com.mlxs.mybatis.test3.teacherMapper.getTeacher";
        Teacher teacher = session.selectOne(statement, "1");
        System.out.println("teacher--->"+teacher);
        session.close();
    }

1.3 结果

teacher--->Teacher [id=1, name=老师A]

 

二、第二种处理方法:使用resultMap标签

2.1 写实体类关联的MapperXML:

<mapper namespace="com.mlxs.mybatis.test3.teacherMapper">
    
    <!-- 
        属性和表的列名不相同时的处理方法:
        2.在teacherMapper.xml中使用resultMap标签 
    -->
    <select id="getTeacher2" parameterType="int" resultMap="getTeacherMap">
        select * from teacher where tid=#{id}
    </select>
    <resultMap type="Teacher" id="getTeacherMap">
        <id property="id" column="tid"/>
        <result property="name" column="tname"/>
    </resultMap>
</mapper>

2.2 Java测试类

/**
     * 属性和表的列名不相同时的2种处理方法
     *         2.在teacherMapper.xml中使用resultMap标签
     *         <select id="getTeacher2" parameterType="int" resultMap="getTeacherMap">
     *            select * from teacher where tid=#{id}
     *        </select>
     *        <resultMap type="Teacher" id="getTeacherMap">
     *            <id property="id" column="tid"/>
     *            <result property="name" column="tname"/>
     *        </resultMap>
     * @author 魅力_小生
     *
     */
    @Test
    public void getTeacher2(){
        //创建session,设置事务为true
        SqlSession session = MyBatisUtil.getSessionFactory().openSession(true);
        String statement = "com.mlxs.mybatis.test3.teacherMapper.getTeacher2";
        Teacher teacher = session.selectOne(statement, "1");
        System.out.println("teacher--->"+teacher);
        session.close();
    }

2.3 结果

teacher--->Teacher [id=1, name=老师A]

 

以上是关于4.mybatis属性和表的列名不相同时的处理方法的主要内容,如果未能解决你的问题,请参考以下文章

.sql导入中文显示乱码解决方法

解决表的列名和对象的属性名不匹配

7表的列名与实体类列名不对应,如何将查询结果封装到实体类对象中

MySQL 基础教程-08 查询数据库和表信息

mybatis基于注解的sql语句,bean的属性名和表的字段名不一样怎么办?

实验二 数据库和表的创建与管理