首批通过分布式安全可靠测评,为关键业务系统打造
SpringBatch 连接 OceanBase 数据库
更新时间:2023-07-28 10:45:57
本文介绍 SpringBatch 连接示例。
环境配置
JDK1.8。
OceanBase 3.x(MySQL 模式/Oracle 模式)。
基于 spring-boot 集成 spring-batch。
配置依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>com.alipay.oceanbase</groupId>
<artifactId>oceanbase-client</artifactId>
<version>2.4.0</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<!--基于实体类自动建表-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--spring-boot核心等其他依赖省略。。。。-->
配置文件
#yml文件:
spring:
datasource:
url: jdbc:oceanbase://10.100.xxx.xxx:18815/test
driver-class-name: com.alipay.oceanbase.jdbc.Driver
# username: admin@mysql
username: admin@oracle
password: ******
jpa:
hibernate:
ddl-auto: update
示例代码
通过调用数据库写入类方法写入数据,具体步骤如下:
创建需要的类。
实体类 :
People和PeopleDESC。//基本people类 @Entity @Table(name = "PEOPLE") public class People { @Id @GeneratedValue private int id; private String lastName; private String firstName; //。。。。。。略 }//加工后的peopledesc类(增加desc属性) @Entity @Table(name = "OK_PEOPLE") public class PeopleDESC { @Id @GeneratedValue //strategy = AUTO / IDENTITY / SEQUENCE 三种主键生成策略均不支持 private int id; private String lastName; private String firstName; private String desc1; //。。。。。。略 }批处理类:
AddPeopleDescProcessor和AddDescPeopleWriter。//用于给people添加desc属性返回一个peopledesc类的处理器 public class AddPeopleDescProcessor implements ItemProcessor<People, PeopleDESC> { public PeopleDESC process(People item) throws Exception { return new PeopleDESC(item.getId() , item.getLastName(), item.getFirstName(), Thread.currentThread().getName()); } }将 peopledesc 写入数据库的类。
//将peopledesc写入数据库的类 public class AddDescPeopleWriter implements ItemWriter<PeopleDESC> { private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void write(List<? extends PeopleDESC> items) throws Exception { for (PeopleDESC peopleDESC : items) { this.jdbcTemplate .update("insert into ok_people (id,first_name,last_name,desc1) values (?, ?, ?, ?)", peopleDESC.getId(), peopleDESC.getFirstName(), peopleDESC.getLastName(), peopleDESC.getDesc()); } } }启动工程
spring-data-jpa会根据实体类People及PeopleDESC自动建表,示例结果如下。
批量添加测试数据至
people。public void addData(){ jdbcTemplate = new JdbcTemplate(dataSource); StringBuilder stringBuilder = new StringBuilder(""); for (int i = 1 ; i<=100 ; i++){ stringBuilder.append("insert into people values ("+i+",'first_test"+i+"','last_test"+i+"');"); jdbcTemplate.execute(stringBuilder.toString()); stringBuilder.delete(0, stringBuilder.length()); } }测试下结果,显示如下:

执行批处理方法。
public void writerTest() throws Exception { //获得people表的结果集 String sql = "select * from people;"; preparedStatement = dataSource.getConnection().prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery(); List list = new ArrayList<PeopleDESC>(); //基于添加desc属性的处理器对结果集进行加工,并封装成一个List<PeopleDESC> while (resultSet.next()){ People people = peopleRowMapper.mapRow(resultSet, resultSet.getRow()); PeopleDESC desc = addPeopleDescProcessor.process(people); list.add(desc); } //调用数据库写入类方法写入数据 addDescPeopleWriter.setDataSource(dataSource); addDescPeopleWriter.write(list); }