zoukankan      html  css  js  c++  java
  • Mybatis中,当实体类中的属性名和表中的字段名不一样 ,怎么办 ?

    方法一:写SQL语句时起别名

    	<select id="getEmployeeById" resultType="com.atguigu.mybatis.entities.Employee">
    		select id,first_name firstName,email,salary,dept_id deptID from employees where id = #{id}
    	</select>
    

    方法二:在MyBatis的全局配置文件中开启驼峰命名规则

    mapUnderscoreToCamelCase:true/false 
    <!--是否启用下划线与驼峰式命名规则的映射(如first_name => firstName)-->
    
    <configuration>  
        <settings>  
            <setting name="mapUnderscoreToCamelCase" value="true" />  
        </settings>  
    </configuration>
    
    

    方法三:在Mapper映射文件中使用resultMap来自定义映射规则

    	<select id="getEmployeeById" resultMap="myMap">
    		select * from employees where id = #{id}
    	</select>
    	
    	<!-- 自定义高级映射 -->
        <resultMap type="com.atguigu.mybatis.entities.Employee" id="myMap">
        	<!-- 映射主键 -->
        	<id column="id" property="id"/>
        	<!-- 映射其他列 -->
        	<result column="last_name" property="lastName"/>
        	<result column="email" property="email"/>
        	<result column="salary" property="salary"/>
        	<result column="dept_id" property="deptId"/>
        </resultMap>
    
  • 相关阅读:
    游LeetCode一月之闲谈
    新年计划与企盼
    F#周报2019年第51&52期
    F#周报2019年第50期
    F#周报2019年第49期
    F#周报2019年第48期
    F#周报2019年第47期
    F#周报2019年第46期
    F#周报2019年第45期
    关于我的随笔
  • 原文地址:https://www.cnblogs.com/shaoyu/p/12467273.html
Copyright © 2011-2022 走看看