使用jdbcTemplate在object数组中插入10个间隔为“10分钟”的数据,可以按照以下步骤进行操作:
<dependencies>
<!-- Spring JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
spring.datasource.url=jdbc:mysql://localhost:3306/db_name
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
CREATE TABLE IF NOT EXISTS data_table (
id INT AUTO_INCREMENT PRIMARY KEY,
value VARCHAR(255)
);
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class DataInsertion {
private final JdbcTemplate jdbcTemplate;
@Autowired
public DataInsertion(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void insertData() {
String sql = "INSERT INTO data_table (value) VALUES (?)";
for (int i = 0; i < 10; i++) {
jdbcTemplate.update(sql, "10分钟");
}
}
}
insertData()
方法。import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
DataInsertion dataInsertion = context.getBean(DataInsertion.class);
dataInsertion.insertData();
}
}
这样,就可以使用jdbcTemplate在object数组中插入10个间隔为“10分钟”的数据。请注意,以上示例中使用的是MySQL数据库,如果使用其他数据库,需要相应地修改数据库连接配置和SQL语句。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云