|
| 1 | +package com.codingapi.springboot.generator.dao; |
| 2 | + |
| 3 | +import com.codingapi.springboot.generator.domain.IdGenerator; |
| 4 | +import org.apache.commons.dbutils.QueryRunner; |
| 5 | +import org.apache.commons.dbutils.ResultSetHandler; |
| 6 | + |
| 7 | +import java.sql.Connection; |
| 8 | +import java.sql.SQLException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class IdGeneratorDao { |
| 13 | + |
| 14 | + private final DbHelper<List<IdGenerator>> dbHelper; |
| 15 | + |
| 16 | + private final ResultSetHandler<List<IdGenerator>> handler; |
| 17 | + |
| 18 | + public IdGeneratorDao(String jdbcUrl) { |
| 19 | + this.dbHelper = new DbHelper<>(jdbcUrl); |
| 20 | + this.handler = rs -> { |
| 21 | + List<IdGenerator> list = new ArrayList<>(); |
| 22 | + while (rs.next()) { |
| 23 | + IdGenerator generator = new IdGenerator(); |
| 24 | + generator.setKey(rs.getString("TAG")); |
| 25 | + generator.setId(rs.getInt("ID")); |
| 26 | + generator.setUpdateTime(rs.getDate("UPDATE_TIME")); |
| 27 | + list.add(generator); |
| 28 | + } |
| 29 | + return list; |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + public boolean save(IdGenerator generator) throws SQLException{ |
| 34 | + return dbHelper.update(new DbHelper.IUpdate() { |
| 35 | + @Override |
| 36 | + public int update(Connection connection, QueryRunner queryRunner) throws SQLException { |
| 37 | + List<IdGenerator> list = queryRunner.query(connection, "SELECT * FROM ID_GENERATOR WHERE TAG = ?", handler, generator.getKey()); |
| 38 | + if (list != null && list.size() > 0) { |
| 39 | + return 0; |
| 40 | + } |
| 41 | + |
| 42 | + String sql = "INSERT INTO ID_GENERATOR (ID, UPDATE_TIME, TAG) VALUES (?, ?, ?)"; |
| 43 | + return queryRunner.update(connection, sql, generator.getId(), generator.getUpdateTime(), generator.getKey()); |
| 44 | + } |
| 45 | + }) > 0; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public IdGenerator updateMaxId(IdGenerator generator) throws SQLException { |
| 50 | + return dbHelper.updateAndQuery(new DbHelper.IUpdateAndQuery<List<IdGenerator>>() { |
| 51 | + @Override |
| 52 | + public List<IdGenerator> updateAndQuery(Connection connection, QueryRunner queryRunner) throws SQLException { |
| 53 | + queryRunner.update(connection, "UPDATE ID_GENERATOR SET ID = ID + 1 WHERE TAG = ?", generator.getKey()); |
| 54 | + return queryRunner.query(connection, "SELECT * FROM ID_GENERATOR WHERE TAG = ?", handler, generator.getKey()); |
| 55 | + } |
| 56 | + }).stream().findFirst().orElse(null); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + public List<IdGenerator> findAll() throws SQLException { |
| 62 | + return dbHelper.query(new DbHelper.IQuery<List<IdGenerator>>() { |
| 63 | + @Override |
| 64 | + public List<IdGenerator> query(Connection connection, QueryRunner queryRunner) throws SQLException { |
| 65 | + return queryRunner.query(connection, "SELECT * FROM ID_GENERATOR", handler); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + private void init() throws SQLException { |
| 71 | + dbHelper.execute(new DbHelper.IExecute() { |
| 72 | + @Override |
| 73 | + public void execute(Connection connection, QueryRunner queryRunner) throws SQLException { |
| 74 | + String sql = "CREATE TABLE IF NOT EXISTS ID_GENERATOR (TAG VARCHAR(128) NOT NULL, ID BIGINT NOT NULL, UPDATE_TIME TIMESTAMP NOT NULL, PRIMARY KEY (TAG))"; |
| 75 | + queryRunner.execute(connection, sql); |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | +} |
0 commit comments