zoukankan      html  css  js  c++  java
  • mybatis中返回list集合为空的解决方法

    一.本人出现的问题是sql语句没报错,但是由于忽略了已修改的代码的传递参数的值,导致查询出来的结果为空,在controller中的参数互换一下就有值了

    二.用mybits查询数据库时,如果参数已传入sql,sql也已经执行了,但是返回结果为空,首先保证数据库中有对应数据,如果有对应数据仍返回null,是数据库配置文件有问题。解决方案如下:
    1、mapper.xml文件加入<resultMap>映射,column是数据库中的字段名,property是实体类javabean中的属性,要一一对应

    2、如果是多张表联合查询,查看是否有相同字段并且没有做区分,比如select s.* from single left join customer cu on s.customer_id = cu.customer_id;single的customer_id没有指定,建议s.* 换成指定字段
    3、<select>标签中不要用ResultType,要用ResultMap且名字要和<resultMap>属性的id相同。且select语句不要用"select * from user_info",要用具体的字段名如"select user_id,user_name from user_info"
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.springapp.maper.UserMapper">
        <resultMap type="User" id="BaseResultMap">
            <!--
                column:数据库中表的字段
                property:数据库中表所有映射的实体类javaBean中的属性名
             -->
            <result column="user_id" property="id"/>
            <result column="user_name" property="name"/>
        </resultMap>




        <!-- 这里的id必须和UserMapper接口中的接口方法名相同,resultMap和上面定义的id名字相同 -->
        <select id="getUser" resultMap="BaseResultMap" parameterType="Java.lang.Integer">
            select user_id,user_name from user_info where user_id=#{id}
        </select>


    </mapper>

  • 相关阅读:
    python之os模块
    python之字符串
    python之爬虫(beautifulsoup)
    python之常见算法
    python之装饰器(类装饰器,函数装饰器)
    python之mock使用,基于unittest
    python之定时器
    python基础语法随记
    redis基础
    移动端页面开发(二)
  • 原文地址:https://www.cnblogs.com/jbml-154312/p/7416560.html
Copyright © 2011-2022 走看看