美文网首页
mybatis 批量添加

mybatis 批量添加

作者: feiai | 来源:发表于2020-02-23 23:10 被阅读0次

    批量添加怎么做呢?

    1:mybatis <foreach> 标签拼接 insert values;

    2:今天说第二种:mybatis ExecutorType.BATCH;

    大致逻辑就是从 sqlsessionfactory 取出 mapper,然后 sqlsession 是 ExecutorType.BATCH;

    再获取的 mapper 再执行 insert 等操作;

    原理类似: 开启 一个 mysql 事物,写一批的insert,最后事物提交,错误就回滚;

    具体代码如下:

    @Component 

    public class SqlSessionBatch<T> {

        public class Entry{

            private T t;

            private SqlSession sqlSession;

            public Entry(T t, SqlSession sqlSession) {

                this.t= t;

                this.sqlSession= sqlSession;

    }

            public T getT() {

                return t;

    }

            public void setT(T t) {

                this.t= t;

    }

            public SqlSession getSqlSession() {

                return sqlSession;

    }

            public void setSqlSession(SqlSession sqlSession) {

                this.sqlSession= sqlSession;

    }

    }

        @Autowired

        private BeanFactory beanFactory;

        /**

        * 获取指定 mapper 使用的 SqlSessionFactory(适合多数据源配置多个 SqlSessionFactory)

        * @param mapper

        * @return

        */

        public Entry getSqlSession(Class<T> mapper) {

            Asserts.beanNotEmpty(mapper, "params mapper can't null!");

            // 获取每个数据源配置的sqlSessionTemplate

            String[] sqlSessionTemplates=

                    ((DefaultListableBeanFactory)beanFactory).getBeanNamesForType(SqlSessionFactory.class);

            for(String sqlSessionTemplate: sqlSessionTemplates){

                SqlSessionFactory sf= beanFactory.getBean(sqlSessionTemplate, SqlSessionFactory.class);

                if(sf.getConfiguration().hasMapper(mapper)){

                    SqlSession sqlSession= sf.openSession(ExecutorType.BATCH, false);

                    return new Entry(sqlSession.getMapper(mapper), sqlSession);

    }

    }

            throw new GeneralException(String.format("batch error: mapper - %s can't found!", mapper.getName()));

    }

        /**

        * 提交

        * @param e

        */

        public void commit(Entry e){

            e.getSqlSession().flushStatements();

            e.getSqlSession().clearCache();

    }

    }

    然后在service实现层这么用:

    @Service

    public class UserSiteServiceImpl implements UserSiteService {

        @Autowired

        private WnUserSiteMapper wnUserSiteMapper;

        @Autowired

        private SqlSessionBatch<WnUserSiteMapper> userSiteMapperSqlSessionBatch;

        @Transactional(rollbackFor = Exception.class)

        protected void batchAddUserSites(List<WnUserSite> userSites) {

        SqlSessionBatch.Entry e= userSiteMapperSqlSessionBatch.getSqlSession(WnUserSiteMapper.class);

        WnUserSiteMapper userSiteMapper= (WnUserSiteMapper) e.getT();

        for(WnUserSite userSite: userSites){

            userSiteMapper.insert(userSite);

         }

            userSiteMapperSqlSessionBatch.commit(e);

         }

    }

    以上就是代码;注意一点哦:记得在数据源上的bean配置上加上 @EnableTransactionManagement 事物注解哦,

    这样遇到错误的时候,批量添加的时候也可以回滚哦;同时在一个数据源上,mapper不要跨sqlsessionfactory哦

    相关文章

      网友评论

          本文标题:mybatis 批量添加

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