where set trim sql if foreach 實現xml方式的動態sql

1.student-mapping.xml

<?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">
<mapper namespace="dyna">
    <!-- create table STUDENT ( 
                ID NUMBER not null, 
                NAME VARCHAR2(20), 
                SEX NUMBER(2), 
                HOBBY VARCHAR2(200), 
                AGE NUMBER ) 
    -->

    <!-- sql用於定義共享的sql 其他select中通過include引用該sql -->
    <sql id="queryColumn">
       name,sex,age
   </sql>
   <select id="queryStudnetABCDEFG">
      select
      <!-- include 應用 -->
      <include refid="queryColumn"></include>
      from book
   </select>

    <!-- trim 

         suffixOverrides 
         prefixOverrides 要覆蓋的字符串
         suffix 
         prefix 替換的字符串
    -->
    <insert id="addStudent" parameterType="map" >
        insert into student(
        <!-- trim 前綴替換用法 -->
        <trim prefixOverrides="," prefix="">
            ,id
            <if test="name!=null and name!=null" >
                ,name
            </if>
            <if test="sex!=null and sex!=null" >
                ,sex
            </if>
            <if test="hobby!=null and hobby!=null" >
                ,hobby
            </if>
            <if test="age!=null and age!=null" >
                ,age
            </if>
        </trim>
        )
        values(
        #{id},
        <!-- trim 後綴替換用法 -->
        <trim suffixOverrides="," suffix="">
            <if test="name!=null and name!=''" >
                #{name},
            </if>
            <if test="sex!=null and sex!=''" >
                #{sex},
            </if>
            <if test="hobby!=null and hobby!=''" >
                #{hobby},
            </if>
            <if test="age!=null and age!=''" >
                #{age},
            </if>

        </trim>
        )
        <!-- selectKey主鍵生成 -->
        <selectKey keyProperty="id"
                   order="BEFORE"
                   resultType="string"
        >
            select max(id)+1 from student
        </selectKey>
    </insert>

    <!-- where -->
    <select id="queryStudent" parameterType="map" resultType="map">
        select * from student
        <!-- where 默認代替第一的 and 爲 where-->
        <where>
            <if test="id!=null and id!=''" >
                and id=#{id}
            </if>
            <if test="name!=null and name!=''" >
                and name=#{name}
            </if>
            <if test="sex!=null and sex!=''" >
                and sex=#{sex}
            </if>
            <if test="hobby!=null and hobby!=''" >
                and hobby=#{hobby}
            </if>
            <if test="age!=null and age!=''" >
                and age=#{age}
            </if>
        </where>
    </select>

    <!-- set -->
    <update id="updateStudent" parameterType="map"  >
        update student
        <!-- set 默認會去掉最後一個   逗號(,)  -->
        <set>
            <if test="name!=null and name!=''" >
                name=#{name},
            </if>
            <if test="sex!=null and sex!=''" >
                sex=#{sex},
            </if>
            <if test="hobby!=null and hobby!=''" >
                hobby=#{hobby},
            </if>
            <if test="age!=null and age!=''" >
                age=#{age},
            </if>
        </set>
        where id=#{id}
    </update>

    <!-- foreach -->
    <select id="queryStudentByHobby" parameterType="map" resultType="map">
        <!-- open  循環開始前的字符串 
             close 循環結束的字符串
             collection 循環的參數(該參數是一個集合或數組)
             index 循環的索引
             item  循環所使用的變量
             separator 除了最後一次循環外 所要拼接的字符串(分隔符)
        -->
        <foreach  open="select * from student where hobby in(" 
                  close=")" 
                  collection="hobbys" 
                  index="i" 
                  item="hobby" 
                  separator=","
        >
            #{hobby}
        </foreach>
    </select>



</mapper>
private static SqlSession sqlSession;
    @Before
    public void getSqlSession() throws IOException{
        if (sqlSession==null) {
            final String mybatisConf = "mybatis-conf.xml";
            InputStream in = Resources.getResourceAsStream(mybatisConf);
            SqlSessionFactory ssf= new SqlSessionFactoryBuilder() .build(in);
            sqlSession=ssf.openSession();
        }
    }
    @After
    public void closeSqlSession(){
        sqlSession.commit();
        sqlSession.close();
    }
@Test
    public void testDynaSqlWhere(){
        String statement="dyna.queryStudent";
        Map map= new HashMap();
        map.put("id", "1");
        map.put("name", "oracle");
        List result=sqlSession.selectList(statement, map);
        System.out.println("--->  " +result);
    }
    @Test
    public void testDynaSqlSet(){
        String statement="dyna.updateStudent";
        Map map= new HashMap();
        map.put("id", "1");
        map.put("name", "11122");
        map.put("age", "30");
        sqlSession.update(statement, map);

    }
    @Test
    public void testDynaSqlForEach(){
        String statement="dyna.queryStudentByHobby";
        Map map= new HashMap();
        String[] hobbys={"playgame","readbook","listenCD"};
        map.put("hobbys",hobbys );
        List result=sqlSession.selectList(statement, map);
        System.out.println("--->  " +result);

    }
    @Test
    public void testDynaSqlTrim(){
        String statement="dyna.addStudent";
        Map map= new HashMap();
        map.put("hobby","playgame");
        sqlSession.insert(statement, map);
    }
發佈了60 篇原創文章 · 獲贊 16 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章