这里我们介绍一个从数据库读取数据并写入到文件中的案例。如果能真心爱上一个人,那么不管对方是何等恶劣,哪怕对方并不爱自己,人生也至少不会是地狱,就算多少有点黯淡。
读取数据库数据
一、定义一个读写的job
<!--读取数据库数据到文件--> <job id="readDatabaseJob"> <step id="readDatabaseStep"> <tasklet transaction-manager="transactionManager"> <chunk reader="userReader" commit-interval="100" writer="userWriter"/> </tasklet> </step> </job>
二、定义并实现读取数据库的reader
<!--读取数据库到文件中--> <bean id="userReader" class="org.springframework.batch.item.database.JdbcCursorItemReader"> <property name="sql" value="select user_name, age, address, birthday from batch_user where age > ?;"/> <property name="rowMapper" ref="userRowMapper"/> <property name="dataSource" ref="dataSource"/> <property name="preparedStatementSetter" ref="paramStatementSetterUser"/> </bean> <bean id="userRowMapper" class="spring.batch.readDatabase.UserRowMapper"/> <bean id="paramStatementSetterUser" class="org.springframework.batch.core.resource.ListPreparedStatementSetter" scope="step"> <property name="parameters"> <list> <value>#{jobParameters['age']}</value> </list> </property> </bean>
上述的UserRowMapper类代码如下
package spring.batch.readDatabase; import org.springframework.jdbc.core.RowMapper; import spring.batch.readFile.People; import java.sql.ResultSet; import java.sql.SQLException; /** * @Author: huhx * @Date: 2017-11-02 上午 10:16 */ public class UserRowMapper implements RowMapper<People> { @Override public People mapRow(ResultSet rs, int rowNum) throws SQLException { People people = new People(); people.setUsername(rs.getString("user_name")); people.setAge(rs.getInt("age")); people.setAddress(rs.getString("address")); people.setBirthday(rs.getDate("birthday")); return people; } }
三、定义并实现写入文件的writer
<bean id="userWriter" class="spring.batch.readDatabase.UserFileWriter"> <property name="targetDirectory" value="file"/> <property name="targetFile" value="user.txt"/> </bean>
UserFileWriter的实现代码
package spring.batch.readDatabase; import org.apache.commons.io.FileUtils; import org.springframework.batch.item.ItemWriter; import spring.batch.readFile.People; import java.io.File; import java.util.List; /** * @Author: huhx * @Date: 2017-11-02 上午 10:19 */ public class UserFileWriter implements ItemWriter<People> { private String targetDirectory; private String targetFile; public void setTargetDirectory(String targetDirectory) { this.targetDirectory = targetDirectory; } public void setTargetFile(String targetFile) { this.targetFile = targetFile; } @Override public void write(List<? extends People> items) throws Exception { File targetDirectoryAsFile = new File(targetDirectory); if (!targetDirectoryAsFile.exists()) { FileUtils.forceMkdir(targetDirectoryAsFile); } File target = new File(targetDirectory, targetFile); FileUtils.writeLines(target, items, true); } }
四、其它的一些信息及注意的地方
- 这里我们复写了People类的toString方法
@Override public String toString() { return "username=" + username + "|age=" + age + "|address=" + address + "|birthday=" + birthday; }
- JobLaunch.java类里面,我们增加参数
JobParameters jobParameters = parametersBuilder.addString("age", "25").toJobParameters();
- 数据库表user_batch的所有数据
- 最终生成的user.txt的文件内容如下
username=孙尚香|age=27|address=天津|birthday=1969-12-12 username=牛魔王|age=26|address=武汉|birthday=1999-09-12