场景:从数据库中一张表批量查出数,每条记录封装到一个实例对象中,再将批量查出的数据插入到另一张数据库表
1.实现类与测试main方法
public class BatchOperationDao {
private static String querySql = "SELECT CITY_NAME,LAND_AREA,POPULATION,GROSS,AREA_NUMBER,POSTAL_CODE,TELEPHONE_CODE,CAR_CODE,CITY_DESCRIBE FROM T_CITY WHEREAREA_NUMBER= ? ";
private static String insertBatchSql = "INSERT INTO T_CITY_INFO(CITY_NAME,LAND_AREA,POPULATION,GROSS,AREA_NUMBER,POSTAL_CODE,TELEPHONE_CODE,CAR_CODE,CITY_DESCRIBE) VALUES (?,?,?,?,?,?,?,?,?) ";
private static Logger logger = LoggerFactory.getLogger(BatchOperationDao.class);
@SuppressWarnings("unchecked")
public List
2.实体类
public class CityModel implements Serializable { private static final long serialVersionUID = 4443714978780088961L;
String cityName;
double landArea;
long population;
double gross ;
String areaNumber;
String postalCode;
String telephoneCode;
String carCode;
String cityDescribe;
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public double getLandArea() {
return landArea;
}
public void setLandArea(double landArea) {
this.landArea = landArea;
}
public long getPopulation() {
return population;
}
public void setPopulation(long population) {
this.population = population;
}
public double getGross() {
return gross;
}
public void setGross(double gross) {
this.gross = gross;
}
public String getAreaNumber() {
return areaNumber;
}
public void setAreaNumber(String areaNumber) {
this.areaNumber = areaNumber;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getTelephoneCode() {
return telephoneCode;
}
public void setTelephoneCode(String telephoneCode) {
this.telephoneCode = telephoneCode;
}
public String getCarCode() {
return carCode;
}
public void setCarCode(String carCode) {
this.carCode = carCode;
}
public String getCityDescribe() {
return cityDescribe;
}
public void setCityDescribe(String cityDescribe) {
this.cityDescribe = cityDescribe;
}
@Override
public String toString() {return "CityModel [cityName=" + cityName
+",landArea=" + landArea
+",population=" + population
+",gross=" + gross
+",areaNumber=" + areaNumber
+",postalCode=" + postalCode
+",telephoneCode=" + telephoneCode
+",carCode=" + carCode
+",cityDescribe=" + cityDescribe
+"]";
}
}
3.JdbcTemplate实例类
public class OperateJdbcUtils {
/**数据库连接需要字符串*/
public static final String username = "root";
public static final String password = "123456";
public static final String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/zbzdb";
public static final String driverName = "com.mysql.jdbc.Driver";
public static JdbcTemplate jdbcTemplate = getJdbcTemplate();
/**获取一个JdbcTemplate实例对象*/
public static JdbcTemplate getJdbcTemplate() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setPassword(password);
dataSource.setUrl(jdbcUrl);
dataSource.setUsername(username);
dataSource.setDriverClassName(driverName);
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
}
4.数据库两张表
CREATE TABLE `t_city` (
`CITY_NAME` VARCHAR(64) COLLATE utf8_bin NOT NULL COMMENT '城市名',
`LAND_AREA` DOUBLE DEFAULT NULL COMMENT '城市面积',
`POPULATION` BIGINT(16) DEFAULT NULL COMMENT '城市人口',
`GROSS` DOUBLE DEFAULT NULL COMMENT '生产总值',
`AREA_NUMBER` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '行政区划代码',
`POSTAL_CODE` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '邮政编码',
`TELEPHONE_CODE` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '电话区号',
`CAR_CODE` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '车牌代码',
`CITY_DESCRIBE` VARCHAR(512) COLLATE utf8_bin DEFAULT NULL COMMENT '城市描述'
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='城市信息表'CREATE TABLE `t_city_info` (
`CITY_NAME` VARCHAR(64) COLLATE utf8_bin NOT NULL COMMENT '城市名',
`LAND_AREA` DOUBLE DEFAULT NULL COMMENT '城市面积',
`POPULATION` BIGINT(16) DEFAULT NULL COMMENT '城市人口',
`GROSS` DOUBLE DEFAULT NULL COMMENT '生产总值',
`AREA_NUMBER` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '行政区划代码',
`POSTAL_CODE` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '邮政编码',
`TELEPHONE_CODE` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '电话区号',
`CAR_CODE` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '车牌代码',
`CITY_DESCRIBE` VARCHAR(512) COLLATE utf8_bin DEFAULT NULL COMMENT '城市描述'
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='城市信息表'
【使用spring的JdbcTemplate操作批量插入数据】以上,TKS.
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)