springBatch完整案例 读取csv文件并保存到数据中

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

简单介绍

主要是三个步骤:
1. 读取数据
2. 对数据进行处理
3. 对数据进行写处理

图像表示为: springBatch完整案例 读取csv文件并保存到数据中_第1张图片

文件名csv_mysql 源码下载位置 github

文件目录:

springBatch完整案例 读取csv文件并保存到数据中_第2张图片

文件组成

  1. 读取csv文件

  2. pojo类 package com.hsm.model; public class Product { //产品id private int id; //产品名称 private String name; //产品描述 private String description; //产品数量 private int quantity; public Product(int id, String name, String description, int quantity) { super(); this.id = id; this.name = name; this.description = description; this.quantity = quantity; } public Product() { } } //省略getter/setter

  3. 读取字段映射文件 package com.hsm.readermapper; import org.springframework.batch.item.file.mapping.FieldSetMapper; import org.springframework.batch.item.file.transform.FieldSet; import org.springframework.validation.BindException; import com.hsm.model.Product; /** * 根据csv文件中的字段集合构建Product对象 * @author huangsm */ public class ProductFieldSetMapper implements FieldSetMapper{ public Product mapFieldSet(FieldSet fieldSet) throws BindException { Product product=new Product(); product.setId( fieldSet.readInt( "id" ) ); product.setName( fieldSet.readString( "name" ) ); product.setDescription( fieldSet.readString( "description" ) ); product.setQuantity( fieldSet.readInt( "quantity" ) ); return product; } }

  4. 数据库写入文件 public class ProductItemWriter implements ItemWriter{ private static final String GET_PRODUCT = "select * from PRODUCT where id = ?"; private static final String INSERT_PRODUCT = "insert into PRODUCT (id,name,description,quantity) values (?,?,?,?)"; private static final String UPDATE_PRODUCT = "update PRODUCT set name = ?, description = ?,quantity = ? where id = ?";

         @Autowired
         private JdbcTemplate jdbcTemplate;
    
         public void write(List products) throws Exception {
             for( Product product : products )
             {
     	//查看是否有对应的数据
                 List productList = jdbcTemplate.query(GET_PRODUCT, new Object[] {product.getId()}, new RowMapper() {
                     public Product mapRow( ResultSet resultSet, int rowNum ) throws SQLException {
                     Product p = new Product();
                     p.setId( resultSet.getInt( 1 ) );
                     p.setName( resultSet.getString( 2 ) );
                     p.setDescription( resultSet.getString( 3 ) );
                     p.setQuantity( resultSet.getInt( 4 ) );
                     return p;
                 }
             });
             //有数据更新
             if( productList.size() > 0 ){
                 jdbcTemplate.update( UPDATE_PRODUCT, product.getName(), product.getDescription(), product.getQuantity(), product.getId() );
             }
             //没有数据就插入
             else{
                 jdbcTemplate.update( INSERT_PRODUCT, product.getId(), product.getName(), product.getDescription(), product.getQuantity() );
             }
    
             }
         }
     }
    

5.文件处理文件 public class ProductItemProcessor implements ItemProcessor{

        private static final String GET_PRODUCT = "select * from PRODUCT where id = ?";
        @Autowired
        private JdbcTemplate jdbcTemplate;

        public Product process(Product product) throws Exception {
	        List productList = jdbcTemplate.query(GET_PRODUCT, new Object[] {product.getId()}, new RowMapper() {
                public Product mapRow( ResultSet resultSet, int rowNum ) throws SQLException {
                    Product p = new Product();
                    p.setId( resultSet.getInt( 1 ) );
                    p.setName( resultSet.getString( 2 ) );
                    p.setDescription( resultSet.getString( 3 ) );
                    p.setQuantity( resultSet.getInt( 4 ) );
                    return p;
                }
            });

            if( productList.size() > 0 )
            {
                Product existingProduct = productList.get( 0 );
                product.setQuantity( existingProduct.getQuantity() + product.getQuantity() );
            }
            return product;
        }
    }

转载于:https://my.oschina.net/u/2615530/blog/852705

你可能感兴趣的