mybatis 09 转义与批量操作

转义
使用mybatis的时候,特殊字符,例如<,>,<>,.....
需使用以下进行转义

<       < 
>       >  
<>   <>
&      &
'     '
"     "

使用CDATA部件

<![CDATA[ ]]>

<![CDATA[ select * from user_t where age > 20]]> 

批量操作
deptMapper.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="com.xxjqr.batch.deptMapper">

    <resultMap type="Dept" id="deptResultMap">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <result column="dept_address" property="deptAddress"/>
    </resultMap>
    
    <!-- 插入操作  -->
    <insert id="insertDeptList" >
    <!-- insert into dept_t(dept_name,dept_address) values('市场部','广州'),('行政部','广州'); -->
        insert into dept_t(dept_name,dept_address) values
        <foreach collection="list" item="dept" separator="," >
        (#{dept.deptName},#{dept.deptAddress})
        </foreach>
    </insert>
    <!-- array,list不用写参数类型 -->
    
    
    <!-- 批量删除部门 -->
    <delete id="deleteDeptList" >
    delete from dept_t where dept_id in(
        <foreach collection="array" item="deptId" separator=",">
            #{deptId}
        </foreach>
    )
    </delete>
    
    
    <!-- 批量修改部门信息 -->
    <update id="updateDeptList" ><!-- 执行的是多条语句 -->
        <foreach collection="list" item="dept" separator=";">
        update dept_t set dept_name = #{dept.deptName},dept_address = #{dept.deptAddress} 
        where dept_id = #{dept.deptId}
        </foreach>
    </update>
</mapper>

public class DeptDao {
    /**批量插入操作*/
    public int insertList (List<Dept> list){
        SqlSession session = null;
        int i = 0;
        try {
            session = MybatisSessionFactory.getSession();
            i = session.insert("com.xxjqr.batch.deptMapper.insertDeptList", list);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        } finally {
            try {
                MybatisSessionFactory.closeSession();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        return i;
    }
    
    /**
     * 批量删除操作*/
    public int deleteList (Integer[] deptIds){
        SqlSession session = null;
        int i = 0;
        try {
            session = MybatisSessionFactory.getSession();
            i = session.insert("com.xxjqr.batch.deptMapper.deleteDeptList", deptIds);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        } finally {
            try {
                MybatisSessionFactory.closeSession();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        return i;
    }
    
    /**
     * 批量修改操作*/
    public int updateList (List<Dept> list){
        SqlSession session = null;
        int i = 0;
        try {
            session = MybatisSessionFactory.getSession();
            i = session.insert("com.xxjqr.batch.deptMapper.updateDeptList", list);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        } finally {
            try {
                MybatisSessionFactory.closeSession();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        return i;
    }
}

public class TestDeptDao {
    private DeptDao deptDao = new DeptDao();
    
//  @Test
    public void testInsert(){
        List<Dept> list = new ArrayList<Dept>();
        for(int i=0;i<3;i++){
            Dept dept = new Dept();
            dept.setDeptName("丁丁"+i);
            dept.setDeptAddress("杭州"+i+"号");
            list.add(dept);
        }
        System.out.println("受影响行数:"+deptDao.insertList(list));
    }
    
//  @Test
    public void testDelete(){
        Integer[] deptIds = {20,21,22};
        System.out.println("受影响行数:"+deptDao.deleteList(deptIds));
    }
    
    @Test
    public void testUpdate(){
        List<Dept> list = new ArrayList<Dept>();
        for(int i=0;i<3;i++){
            Dept dept = new Dept();
            dept.setDeptId(i+1);
            dept.setDeptName("丁丁"+i);
            dept.setDeptAddress("杭州"+i+"号");
            list.add(dept);
        }
        System.out.println("受影响行数:"+deptDao.updateList(list));
    }
}

MyBatisFactory

补充:
如果要让mybatis可以同时执行多条sql语句,那么需要在主配置文件中修改数据库配置信息

<property name="url" value="jdbc:mysql://localhost:3306/mybatis01?allowMultiQueries=true" />
<!--如果前面有参数,那么连接符应该是&-->
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容