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));
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章