Unverified Commit 157a1aac authored by zhaojun's avatar zhaojun Committed by GitHub
Browse files

Merge pull request #125 from cherrylzhao/dev

refactor broadcast-table example.
parents 19cbb22b 0b654e10
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -25,10 +25,10 @@ public class Country implements Serializable {

    private long id;
    
    private String name;

    private String code;

    private String name;

    private String language;

    public long getId() {
@@ -65,6 +65,6 @@ public class Country implements Serializable {

    @Override
    public String toString() {
        return String.format("id: %s, name: %s, code: %s, language: %s", id, name, code, language);
        return String.format("id: %s, code: %s, name: %s, language: %s", id, code, name, language);
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -39,9 +39,9 @@ public interface CommonRepository<T, P> {
    /**
     * insert one entity.
     * @param entity entity
     * @return primary key
     * @return count or primary key
     */
    P insert(T entity);
    Long insert(T entity);
    
    /**
     * Do delete.
+12 −15
Original line number Diff line number Diff line
@@ -29,17 +29,17 @@ import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

public class CountryRepositroyImpl implements CountryRepository {
public final class CountryRepositoryImpl implements CountryRepository {

    private final DataSource dataSource;

    public CountryRepositroyImpl(final DataSource dataSource) {
    public CountryRepositoryImpl(final DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void createTableIfNotExists() {
        String sql = "CREATE TABLE IF NOT EXISTS t_country (id BIGINT NOT NULL AUTO_INCREMENT, name VARCHAR(50),code VARCHAR(50), language VARCHAR(50), PRIMARY KEY (id))";
        String sql = "CREATE TABLE IF NOT EXISTS t_country (id BIGINT NOT NULL AUTO_INCREMENT, code VARCHAR(50), name VARCHAR(50), language VARCHAR(50), PRIMARY KEY (id))";
        try (Connection connection = dataSource.getConnection();
             Statement statement = connection.createStatement()) {
            statement.executeUpdate(sql);
@@ -68,21 +68,18 @@ public class CountryRepositroyImpl implements CountryRepository {
    }

    @Override
    public String insert(final Country country) {
        String sql = "INSERT INTO t_country (name, code, language) VALUES (?, ?, ?)";
    public Long insert(final Country country) {
        String sql = "INSERT INTO t_country (code, name, language) VALUES (?, ?, ?)";
        int result = 0;
        try (Connection connection = dataSource.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, country.getName());
            preparedStatement.setString(2, country.getCode());
            preparedStatement.setString(1, country.getCode());
            preparedStatement.setString(2, country.getName());
            preparedStatement.setString(3, country.getLanguage());
            preparedStatement.executeUpdate();
            ResultSet rs = connection.createStatement().executeQuery("SELECT @@IDENTITY");
            while (rs.next()) {
                country.setId(rs.getLong("@@IDENTITY"));
            }
            result = preparedStatement.executeUpdate();
        } catch (final SQLException ignored) {
        }
        return country.getCode();
        return (long) result;
    }

    @Override
@@ -110,8 +107,8 @@ public class CountryRepositroyImpl implements CountryRepository {
            while (resultSet.next()) {
                Country country = new Country();
                country.setId(resultSet.getLong(1));
                country.setName(resultSet.getString(2));
                country.setCode(resultSet.getString(3));
                country.setCode(resultSet.getString(2));
                country.setName(resultSet.getString(3));
                country.setLanguage(resultSet.getString(4));
                result.add(country);
            }
+8 −13
Original line number Diff line number Diff line
@@ -80,22 +80,17 @@ public class CountryServiceImpl implements CommonService {
    private List<String> insertData() {
        System.out.println("---------------------------- Insert Data ----------------------------");
        List<String> result = new ArrayList<>();
        Locale[] locales = Locale.getAvailableLocales();
        int i = 0;
        for (Locale l:locales) {
            final String country = l.getCountry();
        for (Locale each : Locale.getAvailableLocales()) {
            final String country = each.getCountry();
            if (country == null || "".equals(country)) {
                continue;
            }
            Country currCountry = new Country();
            currCountry.setName(l.getDisplayCountry(l));
            currCountry.setLanguage(l.getLanguage());
            currCountry.setCode(l.getCountry());
            countryRepository.insert(currCountry);
            result.add(currCountry.getCode());
            if (++i == 10) {
                break;
            }
            Country entity = new Country();
            entity.setName(each.getDisplayCountry(each));
            entity.setLanguage(each.getLanguage());
            entity.setCode(each.getCountry());
            countryRepository.insert(entity);
            result.add(entity.getCode());
        }
        return result;
    }
+6 −6
Original line number Diff line number Diff line
@@ -38,18 +38,18 @@ public class CountryEntity extends Country {
        return super.getId();
    }
    
    @Column(name = "name")
    @Override
    public String getName() {
        return super.getName();
    }

    @Column(name = "code")
    @Override
    public String getCode() {
        return super.getCode();
    }

    @Column(name = "name")
    @Override
    public String getName() {
        return super.getName();
    }

    @Column(name = "language")
    @Override
    public String getLanguage() {
Loading