pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
<scope>runtime</scope>
</dependency>
配置文件类
package com.example.testaop.DataConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean("mysql")
// @Primary
@Qualifier("mysql")
@ConfigurationProperties(prefix = "spring.datasource.mysql")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
//支持JdbcTemplate实现多数据源 @Qualifier("primaryDataSource"):使用ByName的方式注入数据源
@Bean(name="mysqlJdbcTemplate")
public JdbcTemplate mysqlJdbcTemplate(
@Qualifier("mysql") DataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
测试类并使用
package com.example.testaop.task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.ws.rs.POST;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* description
*
* @author ${user}
* @Time 2019-07-09
*/
@Component
public class TestJDBC {
@Autowired
@Qualifier("mysqlJdbcTemplate")
private JdbcTemplate jdbcTemplate;
@PostConstruct
public void TestJDBC() {
// String sql = "insert hb_city(ADDVCD, CITY) values (3419,'abc')";
// int ret = jdbcTemplate.update(sql);
// System.out.println(ret);
List<Map<String, Object>> stationList = jdbcTemplate.queryForList("select ADDVCD from hb_city");
System.out.println(stationList);
List<StationID> stationIDS = new ArrayList<>();
StationID stationID = null;
List<String> stationList1 = new ArrayList<>();
List<String> STCDLIst = new ArrayList<>();
for( Map<String, Object> m:stationList) {
System.out.println(m.values());
stationList1.add(m.values().toString());
}
if ( !stationList1.contains("3420") ) {
stationID = new StationID();
stationID.setADDVCD("3420");
stationID.setCITY("abc");
stationIDS.add(stationID);
}
stationID = new StationID();
stationID.setCITY("aie");
stationID.setADDVCD("3421");
stationIDS.add(stationID);
int[] updatedCountArray = jdbcTemplate.batchUpdate("insert into hb_city(ADDVCD, CITY) VALUES (?,?)",
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
preparedStatement.setString(1, stationIDS.get(i).getADDVCD());
preparedStatement.setString(2,stationIDS.get(i).getCITY());
}
@Override
public int getBatchSize() {
return stationIDS.size();
}
});
int sumInsertedCount = 0;
for(int a: updatedCountArray) {
sumInsertedCount+=a;
}
System.out.println(sumInsertedCount);
// System.out.println(rt);
}
}