mybatis解决实体类字段名和表列名不一样的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/swjtu2014112194/article/details/85227448

Book类有成员变量:bookName,price
book表有列名:book_name,price

解决方法一:在BookMapper.xml文件的sql语句中使用别名
例如:

<select id="getAllBooks" resultType="Book">
	select book_name bookName, price from book
</select>

解决方法二:通过<resultMap>映射实体类属性名和表的字段名对应关系

<resultMap type="me.gacl.domain.Order" id="orderResultMap">
	<!-- 用id属性来映射主键字段 -->
	<id property="bookName" column="book_name"/>
	<!-- 用result属性来映射非主键字段 -->
	<result property="test" column="test1"/>
</resultMap>
<select id="getAllBooks" resultType="Book">
	select * from book
</select>

猜你喜欢

转载自blog.csdn.net/swjtu2014112194/article/details/85227448
今日推荐