1.問題背景
最近在使用SpringBoot專案連接資料庫使用mybatis進行物體查詢時遇到的這個問題,物體類創建如下:

最后請求到url地址上發現物體類并沒有封裝到POJO上:

這里匯總一下這個問題的解決方法,大致有三種,
1.1.查詢的sql陳述句中使用別名進行查詢

1.2.使用resultMap映射物體
<resultMap id="deptMap" type="dept"> <!-- property: 物體類屬性名. column: 庫中表的列名 javaType: 資料型別. --> <id property="deptno" column="deptno" javaType="long"></id> <result property="dname" column="dname"></result> <result property="dbSource" column="db_source"></result> </resultMap> <select id="queryById" parameterType="long" resultMap="deptMap"> select * from springcloud_db01.dept where deptno = #{deptno} </select>
1.3.開啟SpringBoot-Mybatis駝峰命令配置
在application.yml中開啟駝峰命名識別,注意不能和mybatis-config.xml一起使用,這里有個坑(解決參考博文:博文鏈接),可以直接使用下面的配置避坑:
mybatis:
#config-location: classpath:mybatis/mybatis-config.xml
type-aliases-package: com.fengye.springcloud.pojo
mapper-locations: classpath:mybatis/mapper/*Mapper.xml
#注意config-location: classpath:mybatis/mybatis-config.xml不能與
#駝峰一起使用
#開啟物體類與資料庫映射駝峰命令識別
configuration:
map-underscore-to-camel-case: true
最終問題解決:

以上三種方式可以根據實際開發場景進行選擇,比如我這種情況就可以直接選擇第三種方式,比較簡單,
問題解決參考博客鏈接如下:
SpringBoot Mybatis 的駝峰命名 開啟駝峰命名的方法
MyBatis - 物體類的屬性名和資料庫列名不一致時的兩種解決辦法!
解決:java.lang.IllegalStateException: Property 'configuration' and 'configLocation' can not specified
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/270600.html
標籤:Java
上一篇:JAVA中如何實作字串的反轉?
下一篇:java
