zoukankan      html  css  js  c++  java
  • Mybatis同时传递实体和分页数据

    在使用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>
  • 相关阅读:
    如何根据二叉树 前序遍历 中序遍历 后序遍历 中的两种遍历来反推另一种遍历
    dijkstral改编
    纪念做出来的第一道计算几何题
    链式前向星
    一道简单树形dp
    算法进阶指南—特殊排序
    算法进阶指南二分章节的两道题
    秦皇岛winter camp 总结
    C
    一道cf水题
  • 原文地址:https://www.cnblogs.com/zhangcaihua/p/13027039.html
Copyright © 2011-2022 走看看