java - Not able to access Second Database using Spring batch -
i new spring . goal read existing data 1 database(mysql) , write same data database(mysql). have created 2 databases batchtest & batchtestdb.
batchtest database contains table product 2 columns product_name & product_price & data in it. batchtestdb database contains table testproduct 2 columns product_name & product_price. have declared batchtest main database using **@primary**. issue trying read data product table , write same data testproduct table. have declared both databases connection details still not recognising second database , says testproduct doesnot exist here's code...
batch configuration program:
@enablebatchprocessing
public class batchconfiguration
{
@autowired
private jobregistry jobregistry;
@autowired private datasource datasource; @autowired private jobbuilderfactory jobs; @autowired private stepbuilderfactory steps; @bean public job importproductjob() { return jobs.get("databasetodatabasejob") .incrementer(new runidincrementer()) .start(step1()) .build(); } @bean public step step1() { return steps.get("databasetodatabasestep") .<product, product> chunk(10) .reader(reader()) .processor(processor()) .writer(writer()) .build(); } @bean public itemstreamreader<product> reader() { jdbccursoritemreader<product> reader = new jdbccursoritemreader<product>(); reader.setdatasource(datasource); reader.setsql("select product_name, product_price product"); reader.setrowmapper(new productrowmapper()); return reader; } @bean public itemprocessor<product, product> processor() { return new productitemprocessor(); }
@bean
public itemwriter<product> writer() { jdbcbatchitemwriter<product> writer = new jdbcbatchitemwriter<product>(); writer.setitemsqlparametersourceprovider(new beanpropertyitemsqlparametersourceprovider<product>()); writer.setsql("insert testproduct(product_name, product_price) values (:productname, :productprice)"); writer.setdatasource(datasource); return writer; }
}
datasource configuration program:
@configuration
public class datasourceconfiguration {
@bean(name="datasource") @primary public datasource datasource() { drivermanagerdatasource datasource= new drivermanagerdatasource(); datasource.setdriverclassname("com.mysql.jdbc.driver"); datasource.seturl("jdbc:mysql://localhost/batchtest"); datasource.setusername("root"); datasource.setpassword("admin"); return datasource; } @bean(name="datasources") public datasource datasources(){ drivermanagerdatasource datasources= new drivermanagerdatasource(); datasources.setdriverclassname("com.mysql.jdbc.driver"); datasources.seturl("jdbc:mysql://localhost/batchtestdb"); datasources.setusername("root"); datasources.setpassword("admin"); return datasources; }
}
productrowmapper program:
public class productitemprocessor implements itemprocessor {
@override
public product process(product product) throws exception {
string productname= product.getproductname(); bigdecimal productprice=product.getproductprice(); product products= new product(); products.setproductname(productname); products.setproductprice(productprice); return products; } }
4. product.java:
public class product { private string productname; private bigdecimal productprice; public string getproductname() { return productname; } public void setproductname(string productname) { this.productname = productname; } public bigdecimal getproductprice() { return productprice; } public void setproductprice(bigdecimal productprice) { this.productprice = productprice; } }
5. productitemprocessor:
public class productitemprocessor implements itemprocessor<product, product> { @override public product process(product product) throws exception { string productname= product.getproductname(); bigdecimal productprice=product.getproductprice(); product products= new product(); products.setproductname(productname); products.setproductprice(productprice); return products; } }
Comments
Post a Comment