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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM