zoukankan      html  css  js  c++  java
  • MyBatis中解决字段名与实体类属性名不相同的冲突

    一: 通过在查询的sql语句中定义字段名的别名,让字段名的别名和实体类的属性名一致,这样就可以表的字段名和实体类的属性名一一对应上了,这种方式是通过在sql语句中定义别名来解决字段名和属性名的映射关系的。

    二: 通过<resultMap>来映射字段名和实体类属性名的一一对应关系。这种方式是使用MyBatis提供的解决方式来解决字段名和属性名的映射关系的。

    列:

    public class User
    {
        private Long id;
        private String userName;
        private String passWord;
        /**
         * ...
         * get,set方法不再列出
         * ...
         * */
    }

    1、xml

    <select id="selectUserById" resultType="User">
            select 
            id,
            user_name as userName,<!--不用在意大小写,Mybatis会先转换成大写再进行匹配  -->
            user_password as userPassword,
            from user
            where id = #{id}
    </select>

    2、xml

    <resultMap type="User" id="UserResultMap">
            <id column="id" property="id"/>
            <result column="user_name" property="userName"/>
            <result column="user_password" property="userPassword"/>
    </resultMap>
        
        
    <select id="selectUserById" resultMap="UserResultMap">
            select 
            id,
            user_name,
            user_password,
            from user
            where id = #{id}
    </select>
  • 相关阅读:
    《图解CSS3》笔记5 媒体与Responsive设计
    理论篇 前端MVC、MVP、MVVM思考1
    AngularJS篇 $resource使用笔记
    《图解CSS3》笔记4 animation动画
    Prim
    邻接矩阵与邻接表
    差分约束
    SPFA
    floyd
    Kosaraju
  • 原文地址:https://www.cnblogs.com/weibanggang/p/9801918.html
Copyright © 2011-2022 走看看