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));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值