Mybatis-動態sql和模糊查詢


 

 sql片段,解決重復sql字段輸入

where:添加where,去除第一個and

set:添加set,去除最后一個,號

<?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">
<!-- namespace:表示名稱空間。現在的目的是區分id的. -->
<mapper namespace="com.zhiyou100.xf.dao.UsersDao">
    <!-- 根據id查詢用戶。id:標識該標簽。 parameterType:參數類型。可以寫 也可以省略 resultType:返回結果的類型。 
        #{id}:類似於EL表達式。 解析id的值 -->
        <sql id="content">
        id,name,age,sex,address
        </sql>
        <select id="selAll" resultType="com.zhiyou100.xf.bean.Users">
            select 
            <include refid="content"></include>
            from users
        </select>
    <select id="getUser" parameterType="com.zhiyou100.xf.bean.Users" resultType="com.zhiyou100.xf.bean.Users">
        select 
        <include refid="content"></include>
         from users where 
        <if test="id!=null">
            id=#{id}
        </if>
        <if test="age!=null">
            and age=#{age}
        </if>
    </select>
    <select id="getWhere" parameterType="com.zhiyou100.xf.bean.Users" resultType="com.zhiyou100.xf.bean.Users">
        select 
        <include refid="content"></include>
         from users
         <where>
             <if test="id!=null">
            id=#{id}
            </if>
            <if test="age!=null">
            and age=#{age}
            </if>
         </where>
    </select>
    <update id="update" parameterType="com.zhiyou100.xf.bean.Users">
        update Users
        <set>
            <if test="id!=null">
            name=#{name},
            </if>
            <if test="age!=null">
            age=#{age}
            </if>
        </set>
        where id=#{id}
    </update>
    <select id="sel1" parameterType="com.zhiyou100.xf.bean.Users" resultType="com.zhiyou100.xf.bean.Users">
        select
        <include refid="content"></include>
        from users
        <where>
            <choose>
            <when test="id!=null">
                id=#{id}
            </when>
            <when test="age!=null">
                age=#{age}
            </when>
        </choose>
        </where>
        
    </select>
    <select id="sel2" parameterType="com.zhiyou100.xf.bean.Users" resultType="com.zhiyou100.xf.bean.Users">
        select
        <include refid="content"></include>
        from users
        <trim prefix="where" prefixOverrides="and">
            <if test="id!=null">
                id=#{id}
            </if>
            <if test="age!=null">
                and age=#{age}
            </if>
        </trim>
        
    </select>
    <update id="update2" parameterType="com.zhiyou100.xf.bean.Users">
        update Users
        <trim prefix="set" suffixOverrides=",">
            <if test="age!=null">
                age=#{age},
            </if>
            <if test="name!=null">
                name=#{name},
            </if>
        </trim>
        where id=#{id}
    </update>
    <select id="sellByIds" resultType="com.zhiyou100.xf.bean.Users">
        select * from Users
        <where>
            <foreach collection="list" item="id" separator="or">
                id=#{id}
            </foreach>
        </where>
    </select>
    <select id="sellIn" resultType="com.zhiyou100.xf.bean.Users">
        select * from Users
        <where>
            <foreach collection="list" open="id in(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </where>
    </select>
</mapper>

 分頁助手pagehelper(詳細可去github查看)

jsqlparser-2.0.jar
pagehelper-5.1.10.jar

conf.xml添加配置

<plugins>
    <!-- com.github.pagehelper為PageHelper類所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置參數,后面會有所有的參數介紹 -->
        <property name="param1" value="value1"/>
    </plugin>
</plugins>

使用方法

      int pageNum=1;
        int pageSize=8;
        PageHelper.startPage(pageNum,pageSize);
        
        List<Users> lu=ud.selAll();
        PageInfo page=new PageInfo(lu);
        System.out.println("總記錄數"+page.getTotal());
        System.out.println("總頁數"+page.getPages());
        System.out.println(page.getPrePage());
        System.out.println(page.getNavigatepageNums());
        System.out.println(page.getNextPage());

封裝成PageInfo
以下為其具體內容

PageInfo{pageNum=1, pageSize=8, size=8, startRow=1, endRow=8, total=145, pages=19,

list=Page{count=true, pageNum=1, pageSize=8, startRow=0, endRow=8, total=145, pages=19, reasonable=false, pageSizeZero=false}

[Users [id=2, name=zous, age=999, sex=null, address=null], Users [id=3, name=我王五, age=47, sex=null, address=null], Users [id=4, name=我五, age=47, sex=null, address=null], Users [id=5, name=sada, age=888, sex=null, address=null], Users [id=6, name=gg, age=77, sex=男, address=上海], Users [id=7, name=asd, age=88, sex=男, address=南京], Users [id=8, name=asd, age=88, sex=男, address=南京], Users [id=9, name=asd, age=88, sex=男, address=南京]],
prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=8, navigateFirstPage=1, navigateLastPage=8, navigatepageNums=[1, 2, 3, 4, 5, 6, 7, 8]}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM