步骤4:mybatis对sql进行对象化

  

例:完整的Sql语句
        select id,name,description,img_url,web_site,sort,is_display
from bbs_brand
        where is_display = #{isDisplay} and name = #{name} and ...
        group by
        having
        order by name desc(asc),id desc,...
        limit 开始行,每页数

第一步:从品牌BrandDao.xml 中的sql开始

<select id="getBrandList1" parameterType="Brand" resultMap="brand">
select id,name,description,img_url,web_site,sort,is_display from bbs_brand where is_display = 1
order by id desc
</select>

第二步:用<sql>提取查询表部分Sql语句

    <!-- 品牌选择器 -->
    <sql id="brandSelector">
        select
        id,name,description,img_url,web_site,sort,is_display
        from bbs_brand
    </sql>
<select id="getBrandList1" parameterType="Brand" resultMap="brand">              <include refid="brandSelector"/>
        where is_display = 1                                                        order by id desc
</select>

第三步:用<sql>提取where部分

 

    <!-- 品牌选择器 -->
    <sql id="brandSelector">
        select
        id,name,description,img_url,web_site,sort,is_display
        from bbs_brand
    </sql>
         <!-- 品牌where -->
     <sql id="brandListWhere">
        <where>
            <if test="name != null">
                name = #{name}
            </if>
            <if test="isDisplay != null">
                and is_display = #{isDisplay}
            </if>
        </where>
     </sql>
<select id="getBrandList1" parameterType="Brand" resultMap="brand">          
   <include refid="brandSelector"/>
        <include refid="brandListWhere"/>                                          
        order by id desc
</select>
 
第四步:测试
 
@Test
       public void TestGetBrandList1()
       {
              Brand brand=new Brand();
              brand.setIsDisplay(1);
              List<Brand> brands=  brandService.getBrandList1(brand);
              for (Brand brand1 : brands) {
                     System.out.println(brand1.toString());
              }
       }

 

 
posted @ 2017-08-15 15:11  担禾滩  阅读(623)  评论(0编辑  收藏  举报