mybatis动态sql查询Dynamic SQL之if,foreach,choose,trim,where,set

来源:互联网 发布:软件shop floor editor 编辑:程序博客网 时间:2024/04/29 13:47

               由于实际开发中,经常要用到拼动态sql进行查询,实际写的时候要用很多判断,是很麻烦的,有了mbatis,它可以帮我们很好的解决这个问题。

              if用法

                 

<select id="<span style="color:#FF0000;">getUserList</span>" parameterType="map" resultMap="getUserLists">select<include refid="useridNames" /><!-- 引用sql id -->from userinfo where 1=1<if test="username!=null">and name like '%'||#{username}||'%' //这里仅对oracle数据库拼接有效,其他没试过</if></select>public List<User> <span style="color:#FF0000;">getUserList</span>(Map<String, Object> map);//调用的接口private static void printUsers(SqlSession sqlSession, UserDao userDao)    {        Map<String, Object> map = new HashMap<String, Object>();        map.put("username", "张");//如果注释掉,会查询到所有结果       List<User> users = userDao.getUserList(map);        for (User user : users)        {            System.out.println(user.getId() + ":" + user.getName());        }    }//测试类,

       choose用法:

<select id="getUserList" parameterType="map" resultMap="getUserLists">select<include refid="useridNames" /><!-- 引用sql id -->from userinfo where 1=1<choose><when test="username!=null">and name like '%'||#{username}||'%'</when><otherwise>and name ='张三'  //如果username为null,则查张三这个条件的,只会执行其中一个分支,与jsp中的c:choose用法类似</otherwise></choose></select>

        where用法:

<select id="getUserList" parameterType="map" resultMap="getUserLists">select<include refid="useridNames" /><!-- 引用sql id -->from userinfo<where>   <!-- where 会自动把最后结果(如果前面有and ,or)的最前面and or,给去掉 --><if test="1==2"> 1=2</if><if test="1==1">  and   1=1</if>   <if test="username!=null">   and name like '%'||#{username}||'%'   </if></where></select>

            trim用法:

  

<select id="getUserList" parameterType="map" resultMap="getUserLists">select<include refid="useridNames" /><!-- 引用sql id -->from userinfo<trim prefix="where" <span style="color:#FF0000;">prefix</span>Overrides="AND |OR ">  <!-- where 会自动把最后结果(如果前面(<span style="color:#FF0000;">prefix</span>)有and ,or)的最前面and or,给去掉 --><if test="1==2"> 1=2</if><if test="1==1">  and   1=1</if>   <if test="username!=null">   and name like '%'||#{username}||'%'   </if></trim></select>

这里的trim相当于where element作用了,可以去and 和or,如果把上面的and去掉,会报下面错

### SQL: select    id,name    from userinfo    where <span style="color:#FF0000;">and</span>   1=1                and name like '%'||?||'%'### Cause: java.sql.SQLSyntaxErrorException: ORA-00936: 缺失表达式

从上面可以看出,where 后面的and 没有被截去

把上面的or去掉也会报下面的错,

### SQL: select    id,name    from userinfo    where <span style="color:#FF0000;">or</span>   1=1                or name like '%'||?||'%'### Cause: java.sql.SQLSyntaxErrorException: ORA-00936: 缺失表达式

         set用法:

<update id="modifyuser" parameterType="user"><!-- sql的类型可以是 statement,preparement,callable,默认是preparement-->update userinfo <set>     <!-- 会自动去掉最后面的逗号(,) --><if test="name!=null"> name=#{name},</if><if test="1==2"><!--  为false不添加--> 1=1</if></set> where id=#{id}</update>

上面也可以用trim来代替,如下:

<update id="modifyuser" parameterType="user"><!-- sql的类型可以是 statement,preparement,callable,默认是preparement-->update userinfo <trim prefix="set" <span style="color:#FF0000;">suffix</span>Overrides=",">     <!-- 会自动去掉最后面(suffix,注意它与where中的区别)的逗号(,) --><if test="name!=null"> name=#{name},</if><if test="1==2"><!--  为false不添加--> 1=1</if></trim> where id=#{id}</update>

      foreach用法:

根据它的语义就应该要循环,那么什么情况下会用到多次循环呢,可以想到是in查询了

<select id="getUserList" resultMap="getUserLists">select<include refid="useridNames" /><!-- 引用sql id -->   <!-- 传的参数可以是list或者array -->from userinfo where id in<foreach item="item" index="index" collection="list" open="("  <span style="color:#FF0000;">list可以是array</span>,像下面String数组那样传,注意array大小是固定的,list不定separator="," close=")"> #{item}</foreach></select>  public List<User> getUserList(List list);  //调用的接口测试方法: private static void printUsers(SqlSession sqlSession, UserDao userDao)    {        List list = new ArrayList();     <span style="color:#FF0000;">String [] list=new String[3];                                          list[0]="8";                                          list[1]="9";                                          list[2]="21";</span>        list.add("8");        list.add("9");        list.add("43");        list.add("21");       List<User> users = userDao.getUserList(list);        for (User user : users)        {            System.out.println(user.getId() + ":" + user.getName());        }    }执行结果:43:张四21:张五9:98:8

    bind用法:

<select id="getUserList" resultMap="getUserLists"><bind name="var" value="'%'+name+'%'"/>  <!-- 定义一个变量 -->select<include refid="useridNames" /><!-- 引用sql id -->        <!-- 传的参数可以是list或者array -->from userinfo where name like #{var}  <!-- 引用上面定义的变量 --></select>


自定义主键

<insert id="insert"><selectKey keyProperty="id" resultType="int" order="BEFORE">//  before代表之前执行select seq_users.nextval from dual</selectKey>insert into users values (#{id}, #{name})</insert>




0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果5s来电接听屏幕卡顿怎么办? 手机摔了一下触屏失灵怎么办 小米手机摔了一下触屏失灵怎么办 苹果6sp触摸ic坏了怎么办 苹果4s屏幕摔裂了怎么办 新换的手机内屏颜色太亮怎么办 苹果手机摔了一下屏幕失灵怎么办 苹果手机6s屏幕坏了怎么办 苹果6s屏幕摔坏了怎么办 苹果手机屏幕进油了屏幕变暗怎么办 苹果手机不小心屏幕进油了怎么办 苹果6充电插口螺丝口坏了怎么办 苹果5s手机安装屏幕翘边怎么办 苹果手机摔了一下触摸屏失灵怎么办 苹果手机摔了下触摸屏失灵怎么办 苹果6老是屏幕失灵或者闪屏怎么办 华为荣耀8手触摸屏乱跳失灵怎么办 苹果5s屏幕有半边竖纹怎么办 苹果5s电源键坏了怎么办 苹果6s手机触屏失灵怎么办 不能取电池的手机触屏不灵怎么办? 7p主板触摸ic坏了怎么办 小米六手机摔了一下屏幕失灵怎么办 魅蓝5s触屏失灵怎么办 苹果手机键盘字母顺序乱了怎么办 红米手机进水后屏幕失灵怎么办 苹果7plus主屏按键坏了怎么办 苹果六手机上的红色变成粉色怎么办 苹果手机出现闪屏和手感不灵怎么办 荣耀6plus后开不了机怎么办 苹果手机touch id密码忘了怎么办 苹果7摔出了一个裂缝怎么办 屏幕摔坏了下键盘失灵怎么办 苹果4s手机按健失灵怎么办 金立手机摔了一下黑屏了怎么办 苹果5s屏幕不亮了怎么办 苹果6s手机开不了机怎么办 苹果手机充电的地方坏了怎么办 手机丢了只有imei码该怎么办 在保修期的苹果被偷了怎么办 苹果手机在保修期之外坏了怎么办