spring整合mybatils的重要maven配置


@Configuration
@MapperScan(basePackages ="org.cyt.metaMysql.mapper",sqlSessionTemplateRef  = "metaDBSqlSessionTemplate")
public class MetaDataSourceConfiguration {


    @ConfigurationProperties("spring.datasource.meta-mysql.data")
    @Bean(name="metaDB")
    @Primary
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "metaDBSqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("metaDB") DataSource ds) throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(ds);
        return bean.getObject();
    }

    @Bean(name = "metaDBSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("metaDBSqlSessionFactory") SqlSessionFactory sessionFactory) throws  Exception{
        return  new SqlSessionTemplate(sessionFactory, ExecutorType.BATCH);
    }

    @Bean(name = "metaDBTransactionManager")
    @Primary
    public DataSourceTransactionManager transactionManager(@Qualifier("metaDB") DataSource ds){
        return new DataSourceTransactionManager(ds);
    }
}
TestMapper.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="org.cyt.metaMysql.mapper.TestMapper">

</mapper>

@Repository public class BaseDao {

public static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BaseDao.class);

@Autowired
@Qualifier(value="metaDBSqlSessionTemplate") 
protected SqlSessionTemplate sqlSessionTemplate;

public BaseDao() {
    log.info("metaDBDao...here...");
}

public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
    this.sqlSessionTemplate = sqlSessionTemplate;
}

}

    
    

@Repository public class TestV3Dao extends BaseDaoD {

public int upsertBatchX(List<TestModel> list, String tablename){
    TestMapper mapper = this.sqlSessionTemplate.getMapper(TestMapper.class);
    //显式commit,不交由spring维护batch
    SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
    if(list ==null || list.size() <= 0){
        return -1;
    }
    try {
        int batchCount = 500;//提交数量,到达这个数量就提交
        for (int index = 0; index < list.size(); index++) {
            PddProductPondV3 obj = list.get(index);
            mapper.insertOne(obj, tablename);
            if(index != 0 && index%batchCount == 0){
                sqlSession.commit();
            }
        }
        sqlSession.commit();
        return 0;
    }catch (Exception e){
        log.error(e.getMessage());
        sqlSession.rollback();
        return -2;
    }finally {
        sqlSession.close();
    }

}

}

    

public interface ShopeeHotTagMapper {

@Insert("<script> "           +
        "INSERT into `${tablename}`( " +
        " <trim prefix='' suffixOverrides=','> " +
        " <if test='obj.platformId != null'>platform_id,</if> " +
        " <if test='obj.catId != null'>cat_id,</if> " +
        " </trim> " +
        ") VALUES( " +
        " <trim prefix='' suffixOverrides=','> " +
        " <if test='obj.platformId != null'>#{obj.platformId},</if> " +
        " <if test='obj.catId != null'>#{obj.catId},</if> " +
        " </trim> )" +
        " ON DUPLICATE KEY UPDATE " +
        " <trim prefix='' suffixOverrides=','> " +
        " <if test='obj.platformId != null'>  platform_id = VALUES(platform_id), </if> " +
        " <if test='obj.catId != null'>  cat_id = VALUES(cat_id),  </if> " +
        " </trim> " +
        "</script>")
int insertOne(@Param("obj") TestModel testModel, @Param("tablename") String tablename);

}

@Component @Slf4j public class ItemSaleRunner {

@Autowired
private TestV3Dao testV3Dao;


public void test(List<TestModel> testModels,String tableName){
    testV3Dao.upsertBatchX(testModels,tableName);

}

}

Share