pom.xml

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-jdbc</artifactId>
    4. </dependency>
    5. <dependency>
    6. <groupId>mysql</groupId>
    7. <artifactId>mysql-connector-java</artifactId>
    8. <version>5.1.37</version>
    9. <scope>runtime</scope>
    10. </dependency>

    配置文件类

    1. package com.example.testaop.DataConfig;
    2. import org.springframework.beans.factory.annotation.Qualifier;
    3. import org.springframework.boot.context.properties.ConfigurationProperties;
    4. import org.springframework.boot.jdbc.DataSourceBuilder;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. import org.springframework.jdbc.core.JdbcTemplate;
    8. import javax.sql.DataSource;
    9. @Configuration
    10. public class DataSourceConfig {
    11. @Bean("mysql")
    12. // @Primary
    13. @Qualifier("mysql")
    14. @ConfigurationProperties(prefix = "spring.datasource.mysql")
    15. public DataSource dataSource() {
    16. return DataSourceBuilder.create().build();
    17. }
    18. //支持JdbcTemplate实现多数据源 @Qualifier("primaryDataSource"):使用ByName的方式注入数据源
    19. @Bean(name="mysqlJdbcTemplate")
    20. public JdbcTemplate mysqlJdbcTemplate(
    21. @Qualifier("mysql") DataSource dataSource){
    22. return new JdbcTemplate(dataSource);
    23. }
    24. }

    测试类并使用

    1. package com.example.testaop.task;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.beans.factory.annotation.Qualifier;
    4. import org.springframework.jdbc.core.BatchPreparedStatementSetter;
    5. import org.springframework.jdbc.core.JdbcTemplate;
    6. import org.springframework.stereotype.Component;
    7. import javax.annotation.PostConstruct;
    8. import javax.ws.rs.POST;
    9. import java.sql.PreparedStatement;
    10. import java.sql.SQLException;
    11. import java.util.ArrayList;
    12. import java.util.List;
    13. import java.util.Map;
    14. /**
    15. * description
    16. *
    17. * @author ${user}
    18. * @Time 2019-07-09
    19. */
    20. @Component
    21. public class TestJDBC {
    22. @Autowired
    23. @Qualifier("mysqlJdbcTemplate")
    24. private JdbcTemplate jdbcTemplate;
    25. @PostConstruct
    26. public void TestJDBC() {
    27. // String sql = "insert hb_city(ADDVCD, CITY) values (3419,'abc')";
    28. // int ret = jdbcTemplate.update(sql);
    29. // System.out.println(ret);
    30. List<Map<String, Object>> stationList = jdbcTemplate.queryForList("select ADDVCD from hb_city");
    31. System.out.println(stationList);
    32. List<StationID> stationIDS = new ArrayList<>();
    33. StationID stationID = null;
    34. List<String> stationList1 = new ArrayList<>();
    35. List<String> STCDLIst = new ArrayList<>();
    36. for( Map<String, Object> m:stationList) {
    37. System.out.println(m.values());
    38. stationList1.add(m.values().toString());
    39. }
    40. if ( !stationList1.contains("3420") ) {
    41. stationID = new StationID();
    42. stationID.setADDVCD("3420");
    43. stationID.setCITY("abc");
    44. stationIDS.add(stationID);
    45. }
    46. stationID = new StationID();
    47. stationID.setCITY("aie");
    48. stationID.setADDVCD("3421");
    49. stationIDS.add(stationID);
    50. int[] updatedCountArray = jdbcTemplate.batchUpdate("insert into hb_city(ADDVCD, CITY) VALUES (?,?)",
    51. new BatchPreparedStatementSetter() {
    52. @Override
    53. public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
    54. preparedStatement.setString(1, stationIDS.get(i).getADDVCD());
    55. preparedStatement.setString(2,stationIDS.get(i).getCITY());
    56. }
    57. @Override
    58. public int getBatchSize() {
    59. return stationIDS.size();
    60. }
    61. });
    62. int sumInsertedCount = 0;
    63. for(int a: updatedCountArray) {
    64. sumInsertedCount+=a;
    65. }
    66. System.out.println(sumInsertedCount);
    67. // System.out.println(rt);
    68. }
    69. }