1、blob字段存取

1.1、存入blob字段

  1. //sql="INSERT INTO "EMS"."D5000"."REPORT"("filename","update_time","file") VALUES(?,?,?)"
  2. FileInputStream inputStream = new FileInputStream(file);
  3. int length = inputStream.available();
  4. Timestamp timestamp = new Timestamp(System.currentTimeMillis());
  5. jdbcTemplate.execute(sql, new AbstractLobCreatingPreparedStatementCallback(new DefaultLobHandler()) {
  6. @Override
  7. protected void setValues(PreparedStatement pre, LobCreator lobCreator) throws SQLException, DataAccessException {
  8. pre.setString(1,fileName);
  9. pre.setTimestamp(2,timestamp);
  10. lobCreator.setBlobAsBinaryStream(pre,3, inputStream,length);
  11. }
  12. });

1.2、取出blob字段,保存文件

  1. // SELECT * FROM "EMS"."D5000"."REPORT" WHERE "id"=?
  2. DefaultLobHandler lobHandler = new DefaultLobHandler();
  3. FileChannel channel = new FileOutputStream("C:\\Users\\tk40q\\Desktop\\xx.docx").getChannel();
  4. jdbcTemplate.query(sql, new Object[]{3}, new AbstractLobStreamingResultSetExtractor<Object>() {
  5. @Override
  6. protected void streamData(ResultSet resultSet) throws SQLException, IOException, DataAccessException {
  7. byte[] bytes = lobHandler.getBlobAsBytes(resultSet, 4);
  8. if (bytes!=null){
  9. channel.write(ByteBuffer.wrap(bytes));
  10. }
  11. }
  12. });

2、查询

rowmapper使用

2.1、查询单条

  1. // 查询结果是一个值 ,查询个数
  2. Integer total = jdbcTemplate.queryForObject(sql, new Object[]{fileId}, Integer.class);
  3. // 查询结果是一个对象
  4. Student student=jdbcTemplate.queryForObject(sql, new Object[]{name}, new BeanPropertyRowMapper<>(Student.class));

2.2、查询多条

  1. List<Integer> query = jdbcTemplate.query(sql, new Object[]{stationName}, new RowMapper<Integer>() {
  2. @Override
  3. public Integer mapRow(ResultSet resultSet, int i) throws SQLException {
  4. return resultSet.getInt(1);
  5. }
  6. });
  1. // 查询结果是多个对象
  2. List<Student> list=jdbcTemplate.query(sql, new Object[]{name},new BeanPropertyRowMapper<>(Student.class));

2.3、使用rowmapper自定义封装

当属性和字段不对应的时候可以使用

  1. List<Report> result = jdbcTemplate.query(sql, new Object[]{flag}, (resultSet, i) -> {
  2. Report report = new Report();
  3. report.setId(resultSet.getInt(1));
  4. report.setFilename(resultSet.getString(2));
  5. report.setUpdateTime(resultSet.getTimestamp(3).toLocalDateTime());
  6. report.setNote(resultSet.getString(5));
  7. report.setFlag(resultSet.getInt(6));
  8. return report;
  9. });

2.4、查询范围 in

  1. @Autowired
  2. private JdbcTemplate jdbcTemplate;
  3. @GetMapping("/queryUsersByIds")
  4. @ApiOperation(value = "查询用户列表")
  5. public List<User> queryUsersByIds() {
  6. String sql = "select * from t_user where id in (:ids);";
  7. // id 集合
  8. List<Integer> ids = new ArrayList<>();
  9. ids.add(3);
  10. ids.add(1);
  11. MapSqlParameterSource parameters = new MapSqlParameterSource();
  12. // 传参
  13. parameters.addValue("ids", ids);
  14. NamedParameterJdbcTemplate givenParamJdbcTemp = new NamedParameterJdbcTemplate(jdbcTemplate);
  15. List<User> data = givenParamJdbcTemp.query(sql, parameters, new RowMapper<User>() {
  16. @Override
  17. public User mapRow(ResultSet rs, int rowNum) throws SQLException {
  18. // TODO Auto-generated method stub
  19. User user = new User();
  20. user.setId(rs.getLong("id"));
  21. user.setUserName(rs.getString("user_name"));
  22. user.setRemark(rs.getString("remark"));
  23. return user;
  24. }
  25. });
  26. return data;
  27. }

3、批量插入、更新、删除

image.png
image.png
image.png
image.png