zoukankan      html  css  js  c++  java
  • 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]}

     
  • 相关阅读:
    五、异步任务编排CompletableFuture
    四、fork/join框架
    三、阻塞等待异步结果FutureTask
    二、synchronized同步锁
    一、Java多线程基础
    6. ZigZag Conversion (字符串的连接)
    5. Longest Palindromic Substring (DP)
    4. Median of Two Sorted Arrays (二分法;递归的结束条件)
    3. Longest Substring Without Repeating Characters (ASCII码128个,建立哈西表)
    2. Add Two Numbers
  • 原文地址:https://www.cnblogs.com/accc111/p/11449119.html
Copyright © 2011-2022 走看看