具有不同对象类型参数的重复查询方法

时间:2014-12-11 09:32:16

标签: java spring-boot dao spring-jdbc

我创建了一个Spring Boot数据库连接器,我想用它来对Azure数据库进行所有jdbc调用。目前,它将处理ClientsLocations的更新和插入。

方法

public List<Client> query(String queryString, RowMapper<Client> azureClientMapper) {
            return jdbc.query(queryString, azureClientMapper);
        }

public List<Location> query(String queryString, RowMapper<Location> azureLocationMapper) {
            return jdbc.query(queryString, azureLocationMapper);
        }
当我尝试编译时,

发出错误。 Eclipse认为他们的方法相同:

Erasure of method query(String, RowMapper<Location>) is the same as another method in type AzureDatabaseConnector

我不想要一个方法&#34; queryLocation&#34;和&#34; queryClient&#34;但该解决方案最终会奏效。 有更好的方法吗

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.zeta.model.Client;
import com.zeta.model.Location;

@Repository
public class AzureDatabaseConnector {

    @Autowired
    @Qualifier("jdbcAzure")
    private JdbcTemplate jdbc;

    public void execute(String queryString) {
        jdbc.execute(queryString);
    }


    public int update(String queryString, Object[] params, int[] types) {   
        return jdbc.update(queryString, params, types);
    }


    /* CLIENT Related Query Strings */

    /**
     * 
     * @param queryString
     * @param azureClientMapper
     * @param id
     * @return
     */
    public Client query(String queryString, RowMapper<Client> azureClientMapper, long id) {
        return jdbc.queryForObject(queryString, azureClientMapper, id);
    }

    public List<Client> query(String queryString, RowMapper<Client> azureClientMapper) {
        return jdbc.query(queryString, azureClientMapper);
    }

    public int[] clientBatchUpdate(final List<Client> clients, String queryString) {
        int[] updateCnt = jdbc.batchUpdate(
                queryString,
                new BatchPreparedStatementSetter() {
                    public void setValues(PreparedStatement ps, int i) throws SQLException {
                        ps.setString(1,clients.get(i).clientname);
                        ps.setString(2,clients.get(i).links);
                        ps.setString(3,clients.get(i).uniquenoteid);
                        ps.setString(4,clients.get(i).channelname);
                        ps.setLong(5,clients.get(i).id);
                    }
                    public int getBatchSize() {
                        return clients.size();
                    }
                } );
        return updateCnt;
    }

    public int[] clientBatchSave(final List<Client> clients, String queryString) {
        int[] updateCnt = jdbc.batchUpdate(
                queryString,
                new BatchPreparedStatementSetter() {
                    public void setValues(PreparedStatement ps, int i) throws SQLException {
                        ps.setLong(1,(long) clients.get(i).id);
                        ps.setString(2,clients.get(i).clientname);
                        ps.setString(3,clients.get(i).links);
                        ps.setString(4,clients.get(i).uniquenoteid);
                        ps.setString(5,clients.get(i).channelname);
                    }
                    public int getBatchSize() {
                        return clients.size();
                    }
                } );

        return updateCnt;
    }

    ////////////////////////

/* LOCATION Related Query Strings */

    /**
     * 
     * @param queryString
     * @param azureClientMapper
     * @param id
     * @return
     */
    public Location query(String queryString, RowMapper<Location> azureLocationMapper, long id) {
        return jdbc.queryForObject(queryString, azureLocationMapper, id);
    }

    public List<Location> query(String queryString, RowMapper<Location> azureLocationMapper) {
        return jdbc.query(queryString, azureLocationMapper);
    }

    public int[] locationBatchUpdate(final List<Location> locations, String queryString) {
    int[] updateLocations = jdbc.batchUpdate(
            queryString,
            new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ps.setString(1,locations.get(i).fulllocationname);
                    ps.setString(2,locations.get(i).barcode);
                    ps.setLong(3,locations.get(i).id);
                }
                public int getBatchSize() {
                    return locations.size();
                }
            } );
    return updateLocations;
}

public int[] locationBatchSave(final List<Location> locations, String queryString) {
    int[] saveLocations = jdbc.batchUpdate(
            queryString,
            new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ps.setLong(1,(long) locations.get(i).id);
                    ps.setLong(2,locations.get(i).client_id);
                    ps.setString(3,locations.get(i).fulllocationname);
                    ps.setString(4,locations.get(i).barcode);
                }
                public int getBatchSize() {
                    return locations.size();
                }
            } );

    return saveLocations;
}

    ////////////////////////
}

0 个答案:

没有答案