Commit 11f564bf authored by cherrylzhao's avatar cherrylzhao
Browse files

refactor CommonRepository#insert P => Long.

parent aa1cd736
Loading
Loading
Loading
Loading
+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.
+4 −7
Original line number Diff line number Diff line
@@ -68,21 +68,18 @@ public final class CountryRepositoryImpl implements CountryRepository {
    }

    @Override
    public String insert(final Country country) {
    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.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
+0 −3
Original line number Diff line number Diff line
@@ -18,11 +18,8 @@
package org.apache.shardingsphere.example.common.mybatis.repository;

import org.apache.ibatis.annotations.Mapper;
import org.apache.shardingsphere.example.common.entity.Country;
import org.apache.shardingsphere.example.common.repository.CountryRepository;

@Mapper
public interface MybatisCountryRepository extends CountryRepository {

    Long insertStandard(Country country);
}
+1 −1
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@ public class SpringCountryServiceImpl implements SpringCountryService {
            currCountry.setCode(l.getCountry());
            currCountry.setName(l.getDisplayCountry(l));
            currCountry.setLanguage(l.getLanguage());
            repository.insertStandard(currCountry);
            repository.insert(currCountry);
            result.add(currCountry.getCode());
            if (++i == 10) {
                break;
+2 −2
Original line number Diff line number Diff line
@@ -3,8 +3,8 @@
<mapper namespace="org.apache.shardingsphere.example.common.mybatis.repository.MybatisCountryRepository">
    <resultMap id="baseResultMap" type="org.apache.shardingsphere.example.common.entity.Country">
        <result column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="code" property="code" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="language" property="language" jdbcType="VARCHAR"/>
    </resultMap>

@@ -20,7 +20,7 @@
        DROP TABLE IF EXISTS t_country;
    </update>

    <insert id="insertStandard" useGeneratedKeys="true" keyProperty="id">
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO t_country (code, name, language) VALUES (#{code,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{language,jdbcType=VARCHAR})
    </insert>