码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis同时传递实体和分页数据

时间:2020-06-01 20:46:19      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:div   @param   ace   分页   and   select   bsp   dao   数据   

在使用mybatis查询数据库时,如果需要分页可以在dao接口方法中传递两个数据,一个是页数pageNum,一个是一面显示几页pageSize,在Mybatis编译sql语句时,会将这两个分页数据插入到sql语句中,在每个参数前要加上@Param注解,注解中的内容是有规定的,页数就是pageNum,显示几页是pageSize:

public interface CityDao{
    List<City> listCity(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);
}

cityMapper.xml:

<select id="listCity" resultMap="cityMap" parameterType="edu.nf.city.entity.City">
    select city_id, country, province, city_name from city_info
</select>

但是,当还需要传递一些条件时,如需要根据City对象中的省份或者城市字段模糊查询,并实现分页:

dao:

public interface CityDao{
    List<City> listCity(City city, @Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);  
}

cityMapper.xml(这里使用动态sql查询):

<!-- 根据province和city_name两个字段模糊查询出城市信息并实现分页,如果city对象为null,则表示查询所有
 同时,因为在前面还传了pageNum和pageSize两个分页数据,所以在sql中对city对象的取值前面需要加上city对象,
如下: -->
<select id="listCity" resultMap="cityMap" parameterType="edu.nf.city.entity.City">
    select city_id, country, province, city_name from city_info
    <where>
        <if test="city != null">
            <if test="city.province != null and city.province != ‘‘">
                province = #{city.province}
            </if>
            <if test="city.cityName != null and city.cityName != ‘‘">
                and city_name = #{city.cityName}
            </if>
        </if>
    </where>
</select>

 

Mybatis同时传递实体和分页数据

标签:div   @param   ace   分页   and   select   bsp   dao   数据   

原文地址:https://www.cnblogs.com/zhangcaihua/p/13027039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!