mybatis 09 转义与批量操作

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

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

使用CDATA部件

 20]]> 

批量操作
deptMapper.xml




 


    
        
        
        
    
    
    
    
    
        insert into dept_t(dept_name,dept_address) values
        
        (#{dept.deptName},#{dept.deptAddress})
        
    
    
    
    
    
    
    delete from dept_t where dept_id in(
        
            #{deptId}
        
    )
    
    
    
    
    
        
        update dept_t set dept_name = #{dept.deptName},dept_address = #{dept.deptAddress} 
        where dept_id = #{dept.deptId}
        
    

public class DeptDao {
    /**批量插入操作*/
    public int insertList (List 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 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 list = new ArrayList();
        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 list = new ArrayList();
        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语句,那么需要在主配置文件中修改数据库配置信息



你可能感兴趣的:(mybatis 09 转义与批量操作)