0.背景

springMVC+mybatis

有这样一个需求,原本是一个简单的查询,mybatis中直接用的实体类进行查询,但是后来要加很多额外的字段和查询要求,于是想能不能除了实体类之外在额外加其他字段。

事实证明是可以的。

1.mapper层的写法

 List<IndexStorageSave> queryAll(@Param("indexStorageSave") IndexStorageSave indexStorageSave,
                                    @Param("beginTimeCity") String beginTimeCity,
                                    @Param("endTimeCity") String endTimeCity);

说明,IndexStorageSave 是实体类。其他的两个是传递的参数。

2.xml层的写法

 <select id="queryAll" resultMap="IndexStorageMap">
        select
        *
        from index_storage
        <where>
            <if test="indexStorageSave.id != null and indexStorageSave.id != ''">
                and id = #{indexStorageSave.id}
            </if>
            <if test="indexStorageSave.time != null">
                and time = #{indexStorageSave.time}
            </if>
            <if test="beginTimeCity !=null and beginTimeCity !=''">
                and city_report_time >= #{beginTimeCity}
            </if>
            <if test="endTimeCity !=null and endTimeCity!=''">
                and #{endTimeCity} >= city_report_time
            </if>
        </where>
    </select>

可以看出,实体类里面的字段通过 实体类名.字段名 就可以正常调用了。其余的还是跟原来一样调用。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐