欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于mybatis查詢結(jié)果映射不到對象的處理

 更新時間:2021年08月25日 14:24:56   作者:別急,已經(jīng)在路上了  
這篇文章主要介紹了mybatis查詢結(jié)果映射不到對象的處理方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

mybatis查詢結(jié)果映射不到對象

項(xiàng)目場景

使用mybatis+springboot 進(jìn)行數(shù)據(jù)庫的數(shù)據(jù)查詢操作,一直拿不到返回結(jié)果。

問題描述

后端dao層(service層調(diào)mapper,方法的返回結(jié)果一直null)代碼一直空指針,

APP 中接收數(shù)據(jù)代碼:

 //分類名稱
        Integer blogCategoryId = blog.getBlogCategoryId();//這里有數(shù)據(jù)  22
        BlogCategory category = blogCategoryMapper.getCategoryById(blogCategoryId);//這里返回結(jié)果就一直null
        blog.setBlogCategoryName(category.getCategoryName());//導(dǎo)致這里一調(diào)用方法就報(bào)空指針了。

原因分析

仔細(xì)檢查了代碼(debug),controller層+ service層沒問題,那問題坑定再dao層。檢查xml文件,但發(fā)現(xiàn)xml文件中查詢方法的sql代碼寫的沒問題:

 <select id="getCategoryById" parameterType="java.lang.Integer" resultType="com.hhh.blog.entity.BlogCategory">
        SELECT
            category_id,
            category_name,
            category_icon,
            category_rank,
            create_time,
            is_deleted
        FROM
            tb_blog_category
        WHERE
            category_id = #{blogCategoryId}
    </select>

這里理論上沒啥問題,但特么的就是數(shù)據(jù)庫的數(shù)據(jù)映射不到對象中(實(shí)體類都是按照數(shù)據(jù)庫數(shù)據(jù)對應(yīng)的,只多不少)。

解決方案

resultType=“com.hhh.blog.entity.BlogCategory”

返回結(jié)果改成使用映射:

<select id="getBlogCategoryPage" resultMap="getBlogCategoryPageMap">

  <resultMap id="getBlogCategoryPageMap" type="com.hhh.blog.entity.BlogCategory">
        <id column="category_id" jdbcType="INTEGER" property="categoryId" />
        <result column="category_name" jdbcType="VARCHAR" property="categoryName" />
        <result column="category_icon" jdbcType="VARCHAR" property="categoryIcon" />
        <result column="category_rank" jdbcType="INTEGER" property="categoryRank" />
        <result column="is_deleted" jdbcType="TINYINT" property="isDeleted" />
        <result column="create_time" jdbcType="DATE" property="createTime" />
    </resultMap>

開啟駝峰式命名匹配也可能解決上述問題。沒試過。建議自己搞起來

mybatis結(jié)果映射遇到的問題

錯誤如下

org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in POJO.User matching [java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.String]
### The error may exist in Mapper/UserMapper
### The error may involve test.selectUserById
### The error occurred while handling results
### SQL: SELECT * FROM USER WHERE id=?
### Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in POJO.User matching [java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.String]

解決方案

最后,將User構(gòu)造器中int改為Integer即可、

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論