
Cannot resolve symbol异常产生原因及解决方案
·
01 异常发生场景
- 当我使用xml文件配置springboot框架下的mybatis时
- 出现了resultType无法匹配爆红的现象
- 以下是代码部分
<resultMap id="OrdersVoResult" type="OrdersVo">
<!-- id配置的是主键,就是表的主键 -->
<!-- property是实体类的属性名 -->
<!-- cloumn是sql查询出来的字段名 -->
<id property="orderId" column="order_id"></id>
<!-- result是其他的字段 -->
<result property="orderNum" column="order_num"></result>
<result property="orderTime" column="order_time"></result>
<association property="products" javaType="ordersDtlVo">
<id property="orderDtlId" column="order_dtl_id"></id>
<result property="productId" column="product_id"></result>
<result property="productName" column="product_name"></result>
<result property="productSellingPrice" column="product_selling_price"></result>
<result property="num" column="num"></result>
<result property="productPicture" column="product_picture"></result>
</association>
</resultMap>
<select id="selectOrders" resultType="OrdersVoResult">
SELECT
orders.order_id,
orders.order_num,
orders.create_time,
orders_dtl.order_dtl_id,
orders_dtl.product_id,
orders_dtl.product_name,
orders_dtl.product_selling_price,
orders_dtl.num,
orders_dtl.product_picture
FROM
orders ,
orders_dtl
WHERE
orders.order_id = orders_dtl.order_id AND
orders.user_id = #{userId}
</select>
02 尝试解决问题的过程
1.yml配置问题
- 我一开始认为是yml配置别名包有问题,毕竟系统报的错误是没有找到对应实体类
mybatis:
# mapper配置文件
mapper-locations: classpath:mapper/*.xml
# resultType别名,没有这个配置resultType包名要写全,配置后只要写类名
type-aliases-package: com.example.demo.com.mashang.dao
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
- 但是反复确认后,确定resultType别名包无问题(事后想想也确实,resultMap是映射标签,怎么可能会和resultType别名包有关系)
2.缓存问题
- 在面向百度编程后,认为问题出在缓存
- 在target文件包的对应xml文件里也确实没有找到映射类
- 在使用clean处理了缓存后依旧爆红
3 .lombok依赖冲突
- lombok是我使用的用于自动生成get和set方法的模块化插件,过时的版本会在编译时失去作用
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<version>1.18.28</version>
</dependency>
-
在尝试添加依赖后依旧没有解决resultType标签爆红问题
-
在我一筹莫展之时,请求了老师的帮助
03 问题的产生及其原因
- 会出现爆红是因为我图方便让系统自动生成resultType标签
- 爆红的原因是因为映射需要的结果集是resultMap标签,所以对应不上,自然就爆红了
04 解决方式
- 将系统生成的resultType标签换成resultMap标签就可以解决Cannot resolve symbol 'XXXX’异常(标签查询不到异常)了
<select id="selectOrders" resultMap="OrdersVoResult">
SELECT
orders.order_id,
orders.order_num,
orders.create_time,
orders_dtl.order_dtl_id,
orders_dtl.product_id,
orders_dtl.product_name,
orders_dtl.product_selling_price,
orders_dtl.num,
orders_dtl.product_picture
FROM
orders ,
orders_dtl
WHERE
orders.order_id = orders_dtl.order_id AND
orders.user_id = #{userId}
</select>
更多推荐
所有评论(0)