Mybatis使用resultMap返回一个null

今天实现一个功能的时候需要进行多表查询,由于数据库之中使用了下划线进行命名,所以用resultMap进行映射,但是返回来的值是一个[null],查阅了很多文档也没找到为什么。

<resultMap id="CartVOResultMap" type="com.vx.entity.vo.CartVO">
  <id column="t1.cart_id" jdbcType="BIGINT" property="cartId" />
  <result column="t1.user_id" jdbcType="BIGINT" property="userId" />
  <result column="t1.pid" jdbcType="BIGINT" property="pid" />
  <result column="t2.pname" jdbcType="BIGINT" property="pname" />
  <result column="t2.price" jdbcType="NUMERIC" property="price" />
  <result column="t1.pcount" jdbcType="BIGINT" property="pcount" />
</resultMap>

最后干脆用第二种开启驼峰命名转换的方法解决了,在mybatis-config.xml文件中进行如下配置:

<configuration>
<settings>
    <setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>

MyBatis中,ResultMap用于解决数据库表中字段与实体类中属性不一致的情况。通过建立一种映射关系,使表中的字段与实体类中的属性一一对应。 在mapper.xml文件中,可以通过ResultMap属性来指定映射。首先,需要在mapper.xml文件中定义一个ResultMap,指定映射的JavaBean类型,并为该ResultMap指定一个唯一的id。在ResultMap中,使用<result>标签来指定字段和属性的映射关系。其中,column属性指代表数据库表中的字段,property属性指代JavaBean中的属性名。例如: ``` <resultMap id="brandResultMap" type="brand"> <result column="brand_name" property="brandName"/> <result column="company_name" property="companyName"/> </resultMap> ``` 接着,在sql语句中使用<select>标签,通过resultMap属性指定使用哪个ResultMap进行映射。例如: ``` <select id="selectAll" resultMap="brandResultMap"> select * from tb_brand; </select> ``` 如果需要返回的对象类中包含对象属性,同样需要进行映射。在ResultMap中,可以使用<association>标签来指定对象属性的映射关系。例如: ``` <resultMap type="com.pjf.mybatis.po.Hotel" id="myHotel"> <id column="id" property="id" jdbcType="INTEGER" /> <result column="hotel_name" property="hotelName" jdbcType="VARCHAR" /> <result column="hotel_address" property="hotelAddress" jdbcType="VARCHAR" /> <result column="price" property="price" jdbcType="INTEGER" /> <!-- 这里可以继续添加其他属性的映射 --> </resultMap> ``` 然后,在sql语句中使用<select>标签,通过resultMap属性指定使用哪个ResultMap进行映射。例如: ``` <select id="getHotel" resultMap="myHotel"> select * from hotel where id=#{id} </select> ``` 通过使用ResultMap,我们可以灵活地处理实体类与数据库表字段之间的映射关系,以及实体类中对象属性的映射。这样可以更好地组织和管理数据对象的映射规则,提高代码的可读性和可维护性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mybatis中关于resultMap使用](https://blog.csdn.net/a18372016358/article/details/129375251)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [MyBatis学习(3)—— resultMap使用](https://blog.csdn.net/weixin_41565013/article/details/88920898)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值