Mybatis允许批量执行多条SQL

1在数据库连接的URL后面追加allowMultiQueries=true

jdbc:mysql://localhost:3306/db0?characterEncoding=utf-8&allowMultiQueries=true

如果使用的阿里的Druid的数据库连接池,需要修改连接配置

    @Bean(name = "wallFilter")
    @DependsOn("wallConfig")
    public WallFilter wallFilter(WallConfig wallConfig){
        WallFilter wallFilter = new WallFilter();
        wallFilter.setConfig(wallConfig);
        return wallFilter;
    }
 
    @Bean(name = "wallConfig")
    public WallConfig wallConfig(){
        WallConfig wallConfig = new WallConfig();
        wallConfig.setMultiStatementAllow(true);//允许一次执行多条语句
        wallConfig.setNoneBaseStatementAllow(true);//允许一次执行多条语句
        return wallConfig;
    }

在datasource初始化的设置

List<Filter> filters = new ArrayList<>();
filters.add(wallFilter(wallConfig()));
druidDataSource.setProxyFilters(filters);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
针对Oracle数据库,Mybatis框架下批量执行多条Update语句,可以使用Mybatis的BatchExecutor来实现。具体步骤如下: 1. 在Mybatis的配置文件中,将executor-type设置为BATCH,如下所示: ``` <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/> <property name="username" value="username"/> <property name="password" value="password"/> </dataSource> <mapper resource="com/example/mapper.xml"/> <property name="defaultExecutorType" value="BATCH"/> </environment> </environments> </configuration> ``` 2. 在Mapper接口中,定义批量执行Update语句的方法,如下所示: ``` public interface UserMapper { void batchUpdate(List<User> userList); } ``` 3. 在Mapper.xml文件中,编写批量执行Update语句的SQL语句,如下所示: ``` <update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" index="index"> update user set name=#{item.name}, age=#{item.age} where id=#{item.id} </foreach> </update> ``` 4. 在Java代码中,调用Mapper接口的批量执行Update语句的方法,如下所示: ``` List<User> userList = new ArrayList<User>(); // 添加待更新的User对象到userList中 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.batchUpdate(userList); sqlSession.commit(); ``` 注意事项: 1. 执行批量更新时,需要使用JDBC事务,因此需要在Mybatis配置文件中配置transactionManager为JDBC。 2. 使用BatchExecutor时,需要手动提交事务,因此需要在Java代码中调用sqlSession.commit()方法提交事务。 3. 执行批量更新时,需要将多条Update语句封装到一个List中,然后传递给Mapper接口的批量执行方法。在Mapper.xml文件中,使用foreach标签遍历List中的每个元素,执行批量更新操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值