mybits的批量化操作

大量对千级别数据进行处理的时候,发现一般的单个多次提交数据库,访问数据极其慢,并且无法正常执行SQL语句,往往出现,数据库断连,终止操作。报错信息:

ORA-12519no appropriat  service handler found

出现的原因,在于软硬件之间的速度不匹配的问题,造成访问数据库事务的时候,关闭提交事务不及时。使得数据库认为当前的连接数目超过设定的数值。线程资源释放不及时,出现资源争夺的状况,造成死锁。一般而言数据库允许的最大连接数是150

1.【查看数据库当前允许的连接数量】

Select count(*) from v$process   ------当前的连接数

Select value from v$parameter where name=’processes ’   ----数据库允许的最大连接数量

 

2.【修改连接数的方法】【不建议使用会出现问题,指标不治本】

alter system set processes = 300 scope = spfile;-----修改最大连接数:

shutdown immediate;-------关闭数据库:

startup;  -------重启数据库

 

SELECT osuser, a.username,cpu_time/executions/1000000||'s', sql_fulltext,machine

from v$session a, v$sqlarea b

where a.sql_address =b.address order by cpu_time/executions desc;

-------------查看当前有哪些用户正在使用数据

3.解决方式

A.使用缓存队列,在线程不忙的时候提交,换句话说。现将数据缓存在队列之中,分批提交,降低数据库的访问次数。

B.针对SSM框架,使用mybits的时候,使用mybits<foreach>标签,批量操作;同时后台控制每次批量的个数,从而做到分批提交。

采取的方案为B

【说明】

A.关于mybits中的批量操作【数据库ORACLE

1.批量修改

xml

  <!-- 更新申报的剩余补助金额 【非国税统一支付的时候修改】-->

  <update id="updateAllyje"  parameterType="java.util.List">

   <foreach collection="list" item="item" index="index"  open="begin" close=";end;"   separator=";">

  update HZW_SBXX t  set SYJE=#{item.syje},  BCBZJE=t.bzje,  ZFZB=#{item.zfzb}

  where ID=#{item.id}

  </foreach>

  </update>

 

 

Dao

int updateAllyje(List<Sbxx> record);//非国税

Controller

跟新数据存放在list中,判断list.size>num;num为批量提交的个数。

Public xxx(){

For(-------){

List.add(--);

If(list.size>num){

  updateAllyje(list);//集合中的数据量,大于num就进行提交

  List.clear();//清空集合,重新存储数据

}

}

updateAllyje(list);//将剩余集合进行提交

}

2.批量存入数据

xml

<!-- 批量新增支付信息 -->  

  <insert id="Plinsert" parameterType="java.util.List">

  insert all

 <foreach collection="list" item="item" index="index" separator="">

 into   HZW_BZLSJL (ID, SBID, SBJE, SJBZJE, SYJE, CREATETIME,ZFFS,CZRY)

values (seq_hzwbzlsjl.nextval,#{item.sbid},#{item.sbje},#{item.sjbzje},#{item.syje},sysdate,#{item.zffs},#{item.czry})

</foreach>

select 1 from dual

</insert>

Dao

int Plinsert(List<BzHistory> history);

Controller

存入数据存放在list中,判断list.size>num;num为批量提交的个数。

Public xxx(){

For(-------){

List.add(--);

If(list.size>num){

  Plinsert(list);//集合中的数据量,大于num就进行提交

  List.clear();//清空集合,重新存储数据

}

}

Plinsert(list);//将剩余集合进行提交

}

 

 

 

3.批量查找

Xml

<!-- 查找状态为7的申报信息 -->

   <select id="selectSbxx7" resultMap="BaseResultMap" parameterType="java.util.List" >

    select

    <include refid="Base_Column_List" />

    from HZW_SBXX where status='7' and id in

    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  

        trim(#{item})  

     </foreach>

   </select>

Dao

   List<Sbxx> selectSbxx7(List<String> id);

Controller

存入查询条件数据存放在list中,判断list.size>num;num为批量提交的个数。

Public xxx(){

For(-------){

List.add(--);

}

List<Sbxx> jguolist= selectSbxx7(list)

}

结果集合:jguolist

4.批量删除

Xml

<delete id="batchDelete" parameterType="java.util.List">  

         <foreach collection="list" item="item" index="index"  open="begin" close=";end;"   separator=";">

            delete from user  

            where id=#{item.id,jdbcType=INTEGER}  

        </foreach>  

    </delete>

Dao

Public batchDelete(List<int> id)

Controller

存入查询条件数据存放在list中,判断list.size>num;num为批量提交的个数。

Public xxx(){

For(-------){

List.add(--);

}

List<Sbxx> jguolist= batchDelete(list)

}

结果集合:jguolist

B.MySQL数据库 
批量操作主要使用的是Mybatisforeach,遍历参数列表执行相应的操作,所以批量插入/更新/删除的写法是类似的,只是SQL略有区别而已。MySql批量操作需要数据库连接配置allowMultiQueries=true才可以。 
1)批量插入  

<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true">  

        <foreach close="" collection="list" index="index" item="item" open="" separator=";">  

            insert into user (name, age,dept_code) values  

            (#{item.name,jdbcType=VARCHAR},  

            #{item.age,jdbcType=INTEGER},  

             #{item.deptCode,jdbcType=VARCHAR}  

            )  

        </foreach>  

    </insert>  



上面演示的是MySql的写法(表主键自增的写法),因为MySql支持主键自增,所以直接设置useGeneratedKeys=true,即可在插入数据时自动实现主键自增;不需要自增时就不需要设置useGeneratedKeys,而且插入SQL包含所有字段即可。实际Mysql还有另外一种写法,就是拼接values的写法,这种方法我测试过比多条insert语句执行的效率会高些。不过需要注意一次批量操作的数量做一定的限制。具体写法如下:  

<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true">  

        insert into user (name, age,dept_code) values  

        <foreach collection="list" index="index" item="item" open="" close="" separator=",">  

            (#{item.name,jdbcType=VARCHAR},  

            #{item.age,jdbcType=INTEGER},  

             #{item.deptCode,jdbcType=VARCHAR}  

            )  

        </foreach>  

    </insert>  


对于Oracle不支持主键自增,需要序列替换,所以在SQL写法上略有不同,需要在insert语句前加个 <selectKey>...</selectKey>告知Mybatis主键如何生成(selectKey中间的内容有省略,实际是生成主键的SQL)。 

2)批量更新  

<update id="batchUpdate" parameterType="java.util.List">  

        <foreach close="" collection="list" index="index" item="item" open="" separator=";">  

            Update user set name=#{item.name,jdbcType=VARCHAR},age=#{item.age,jdbcType=INTEGER}  

            where id=#{item.id,jdbcType=INTEGER}  

        </foreach>  

    </update>  



3)批量删除  

<delete id="batchDelete" parameterType="java.util.List">  

        <foreach close="" collection="list" index="index" item="item" open="" separator=";">  

            delete from user  

            where id=#{item.id,jdbcType=INTEGER}  

        </foreach>  

    </delete>  



二、模糊查询  

<select id="selectLikeName" parameterType="java.lang.String" resultMap="BaseResultMap">  

        select  

        <include refid="Base_Column_List" />  

        from user  

        where name like CONCAT('%',#{name},'%' )   

    </select>  


上面的模糊查询语句是Mysql数据库的写法示例,用到了Mysql的字符串拼接函数CONCAT,其它数据库使用相应的函数即可。 

三、多条件查询 

多条件查询常用到Mybatisif判断,这样只有条件满足时,才生成对应的SQL  

<select id="selectUser" parameterType="map" resultMap="BaseResultMap">  

        select  

        <include refid="Base_Column_List" />  

        from user  

        <where>  

            <if test="name != null">  

                name = #{name,jdbcType=VARCHAR}  

            </if>  

            <if test="age != null">  

                and age = #{age,jdbcType=INTEGER}  

            </if>  

        </where>  

    </select>  



四、联表查询 
联表查询在返回结果集为多张表的数据时,可以通过继承resultMap,简化写法。例如下面的示例,结果集在User表字段的基础上添加了Dept的部门名称

<resultMap id="ExtResultMap" type="com.research.mybatis.generator.model.UserExt" extends="BaseResultMap">  

     <result column="name" jdbcType="VARCHAR" property="deptName" />  

  </resultMap>  

      

    <select id="selectUserExt" parameterType="map" resultMap="ExtResultMap">  

        select  

            u.*, d.name  

        from user u inner join dept d on u.dept_code = d.code  

        <where>  

            <if test="name != null">  

                u.name = #{name,jdbcType=VARCHAR}  

            </if>  

            <if test="age != null">  

                and u.age = #{age,jdbcType=INTEGER}  

            </if>  

        </where>  

</select>

 

 

 

26】MyBatis的foreach语句详解

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有 itemindexcollectionopenseparatorcloseitem表示集合中每一个元素进行迭代时的别名,index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:

1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,mapkey就是参数名,所以这个时候collection属性值就是传入的Listarray对象在自己封装的map里面的key

下面分别来看看上述三种情况的示例代码:

1.单参数List的类型:

    <select id="dynamicForeachTest" resultType="Blog">

        select * from t_blog where id in

        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">

            #{item}

        </foreach>

    </select>

上述collection的值为list,对应的Mapper是这样的

public List<Blog> dynamicForeachTest(List<Integer> ids);

测试代码:

    @Test

    public void dynamicForeachTest() {

        SqlSession session = Util.getSqlSessionFactory().openSession();

        BlogMapper blogMapper = session.getMapper(BlogMapper.class);

        List<Integer> ids = new ArrayList<Integer>();

        ids.add(1);

        ids.add(3);

        ids.add(6);

        List<Blog> blogs = blogMapper.dynamicForeachTest(ids);

        for (Blog blog : blogs)

            System.out.println(blog);

        session.close();

    }

2.单参数array数组的类型:

    <select id="dynamicForeach2Test" resultType="Blog">

        select * from t_blog where id in

        <foreach collection="array" index="index" item="item" open="(" separator="," close=")">

            #{item}

        </foreach>

    </select>

上述collectionarray,对应的Mapper代码:

public List<Blog> dynamicForeach2Test(int[] ids);

对应的测试代码:

    @Test

    public void dynamicForeach2Test() {

        SqlSession session = Util.getSqlSessionFactory().openSession();

        BlogMapper blogMapper = session.getMapper(BlogMapper.class);

        int[] ids = new int[] {1,3,6,9};

        List<Blog> blogs = blogMapper.dynamicForeach2Test(ids);

        for (Blog blog : blogs)

            System.out.println(blog);

        session.close();

}

 

 

3.自己把参数封装成Map的类型

    <select id="dynamicForeach3Test" resultType="Blog">

        select * from t_blog where title like "%"#{title}"%" and id in

        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">

            #{item}

        </foreach>

    </select>

上述collection的值为ids,是传入的参数Mapkey,对应的Mapper代码:

public List<Blog> dynamicForeach3Test(Map<String, Object> params);

对应测试代码:

    @Test

    public void dynamicForeach3Test() {

        SqlSession session = Util.getSqlSessionFactory().openSession();

        BlogMapper blogMapper = session.getMapper(BlogMapper.class);

        final List<Integer> ids = new ArrayList<Integer>();

        ids.add(1);

        ids.add(2);

        ids.add(3);

        ids.add(6);

        ids.add(7);

        ids.add(9);

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("ids", ids);

        params.put("title", "中国");

        List<Blog> blogs = blogMapper.dynamicForeach3Test(params);

        for (Blog blog : blogs)

            System.out.println(blog);

        session.close();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值