美文网首页
mybatis 09 转义与批量操作

mybatis 09 转义与批量操作

作者: 小小机器人 | 来源:发表于2016-10-17 20:37 被阅读45次

    转义
    使用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" />
    <!--如果前面有参数,那么连接符应该是&-->
    

    相关文章

      网友评论

          本文标题:mybatis 09 转义与批量操作

          本文链接:https://www.haomeiwen.com/subject/yqrzyttx.html