myBatis動態sql

一.動態查詢語句

1.StudentMapper.xml

<select id="findAll" parameterType="com.mybatis.dynamic.Student" resultMap="studentMap">
        select id,name,age from student
        <where>
            <if test="id!=null">
                and id = #{id}
            </if>
                <if test="name!=null">
                and name = #{name}
            </if>
                <if test="age!=null">
                and age = #{age}
            </if>
        </where>    
    </select>

2.dao類

public List<Student> findAll(Student student){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            List<Student> students = sqlSession.selectList("studentDynamic.findAll",student);
            sqlSession.commit();
            return students;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.toString());
            sqlSession.rollback();
        }finally{
            MyBatisUtil.closeSqlSession();
        }
        return null;
    }

3.測試

/**
     * @param args
     */
    public static void main(String[] args) {

        StudentDao dao = new StudentDao();
        System.out.println("-----------3------------");
        List<Student> students3 = dao.findAll(new Student(null, "hehe", 10));
        for (Student student : students3) {
            System.out.println(student.getName()+":"+student.getAge());
        }
    }

二.動態更新語句

1.StudentMapper.xml

    <update id="update" parameterType="com.mybatis.dynamic.Student">
        update student 
        <set>
            <if test="name!=null">
                name = #{name},
            </if>
            <if test="age!=null">
                age = #{age},
            </if>
        </set>
        <where>
            <if test="id!=null">
                and id = #{id}
            </if>
        </where>
    </update>

2.dao類

public void update(Student student){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            sqlSession.update("studentDynamic.update",student);
            sqlSession.commit();
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.toString());
            sqlSession.rollback();
        }finally{
            MyBatisUtil.closeSqlSession();
        }
    }

3.測試

        StudentDao dao = new StudentDao();
        dao.update(new Student("0002","xiaoNiao",100));

三.動態刪除

1.StudentMapper.xml

<!-- 
        foreach:迭代數組元素
        open:表示開始符號
        close:表示結束符號
        separator:表示分割符號
        item:表示迭代的數組
     -->
    <delete id="delete">
        delete from student where id in
        <foreach collection="array" open="(" close=")" separator="," item="ids">
            ${ids}
        </foreach>
    </delete>
<!-- 
        foreach:迭代list元素
        open:表示開始符號
        close:表示結束符號
        separator:表示分割符號
        item:表示迭代的list元素
     -->
    <delete id="deleteList">
        delete from student where id in
        <foreach collection="list" open="(" close=")" separator="," item="ids">
            ${ids}
        </foreach>
    </delete>

2.dao類

    public void delete(String[] ids){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            sqlSession.delete("studentDynamic.delete",ids);
            sqlSession.commit();
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.toString());
            sqlSession.rollback();
        }finally{
            MyBatisUtil.closeSqlSession();
        }
    }

3.測試類

        StudentDao dao = new StudentDao();
        dao.delete(new String[]{"0001","0003"});

四.動態添加

1.StudentMapper.xml

<sql id="key">
        <trim suffixOverrides=",">
            <if test="id!=null">
                id,
            </if>
            <if test="name!=null">
                name,
            </if>
            <if test="age!=null">
                age,
            </if>
        </trim>
    </sql>
    <sql id="value">
    <!-- 去掉最後的逗號 -->
        <trim suffixOverrides=",">
            <if test="id!=null">
                #{id},
            </if>
            <if test="name!=null">
                #{name},
            </if>
            <if test="age!=null">
                #{age},
            </if>
        </trim>
    </sql>
    <insert id="addStudent" parameterType="com.mybatis.dynamic.Student">
        insert into student(<include refid="key"/>) values(<include refid="value"/>);
    </insert>

2.dao類

    public void insert(Student student){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            sqlSession.insert("studentDynamic.addStudent",student);
            sqlSession.commit();
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.toString());
            sqlSession.rollback();
        }finally{
            MyBatisUtil.closeSqlSession();
        }
    }

3.測試

        dao.insert(new Student("0001","1111", 20));
        dao.insert(new Student("0003",null, 20));
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章