1. 程式人生 > 資訊 >聯想上架拯救者電動升降桌 T7,售價 2699 元

聯想上架拯救者電動升降桌 T7,售價 2699 元

一、事情起因:在springboot整合mybatis時,測試出了異常

 

 

 二、通過排查,發現在xml配置檔案中,單表查詢中誤將resultType錯寫成resultMap

 

 

 三、resultType和resultMap區別

resultType是SQL語句的返回型別,需要有對應的pojo類,因此在單表查詢時,resultType是最合適的。因此將上述錯誤改為如下即可

<select id="getUserById" resultMap="com.boot.admin.bean.User">
        select * from t_admin where id = #{id} 
</select>

resultMap作SQL語句的返回型別時,需要引用外部resultMap標籤,也可以將上述查詢改為如下方式

<resultMap id="byIdResultMap" type="com.boot.admin.bean.User">
        <!--設定主鍵對映-->
        <id property="id" column="id" />
     <!--設定列名和pojo屬性對映-->
<result property="loginAcct" column="login_acct"
/> <result property="userPswd" column="user_pswd"/> <result property="userName" column="user_name"/> <result property="email" column="email"/> <result property="createTime" column="create_time"/> </resultMap> <select id="getUserById" resultMap
="byIdResultMap">
select * from t_admin where id = #{id}
</select>

注意:在springboot中,如果資料庫列名和POJO屬性不統一,需要開啟駝峰命名來設定對映關係,在application.xml中新增如下配置

mybatis.configuration.map-underscore-to-camel-case=true