解决jdbctemplate查询参数为集合类型时,查不出数据的问题
This commit is contained in:
+6
-1
@@ -58,7 +58,12 @@ public class SqlMapperInterceptor implements Interceptor {
|
||||
Object res = null;
|
||||
if (sqlParser.isSelect()) {
|
||||
if (sqlParser.isReturnCollection()) {
|
||||
res = jdbcTemplate.queryForList(resultComponentType, sql, inv.getArgs());
|
||||
Object params = getParams(inv);
|
||||
if (null == params || params.getClass().isArray()) {
|
||||
res = jdbcTemplate.queryForList(resultComponentType, sql, inv.getArgs());
|
||||
} else {
|
||||
res = jdbcTemplate.queryForListByMap(resultComponentType, sql, (Map)params);
|
||||
}
|
||||
} else {
|
||||
if (Page.class.isAssignableFrom(inv.getTargetMethod().getParameters()[0].getType())) {
|
||||
// 分页查询 TODO 此处暂时临时处理,假设后面的参数是一个DO对象
|
||||
|
||||
@@ -261,19 +261,6 @@ public class JdbcTemplate {
|
||||
Connection conn = null;
|
||||
|
||||
try {
|
||||
// TODO 临时处理集合参数
|
||||
if (ArrayUtil.notEmpty(params)) {
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
Object item = params[i];
|
||||
if (null != item && Collection.class.isAssignableFrom(item.getClass()) && !((Collection)item).isEmpty()) {
|
||||
Object first = ((Collection)item).stream().findFirst().get();
|
||||
if (Integer.class.isAssignableFrom(first.getClass())) {
|
||||
params[i] = ((Collection)item).stream().map(String::valueOf).collect(Collectors.joining(","));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
conn = dataSourceHolder.getConnection();
|
||||
res = jdbcOperations.executeQueryForList(conn, clazz, sql, params);
|
||||
} finally {
|
||||
|
||||
@@ -92,8 +92,34 @@ public class SqlAndParams {
|
||||
for(String key : paramMap.keySet()){
|
||||
int index = originSql.indexOf(":" + key);
|
||||
if(-1 != index){
|
||||
orderlyList.add(new Orderly(paramMap.get(key), index));
|
||||
sql = sql.replaceFirst(":" + key, "?");
|
||||
List<Orderly> currentList = new ArrayList<>();
|
||||
|
||||
// 解决数组、集合参数问题
|
||||
int count = 1;
|
||||
Object tmp = paramMap.get(key);
|
||||
if (null != tmp) {
|
||||
if (tmp.getClass().isArray()) {
|
||||
count = ((Object[])tmp).length;
|
||||
currentList.addAll(Stream.of((Object[])tmp).map(e -> new Orderly(e, index)).collect(Collectors.toList()));
|
||||
} else if (Collection.class.isAssignableFrom(tmp.getClass())) {
|
||||
count = ((Collection)tmp).size();
|
||||
Object[] arr = ((Collection)tmp).toArray();
|
||||
paramMap.put(key, arr);
|
||||
currentList.addAll((List)((Collection)tmp).stream().map(e -> new Orderly(e, index)).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
List<String> s = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
s.add("?");
|
||||
}
|
||||
sql = sql.replaceFirst(":" + key, s.stream().collect(Collectors.joining(",")));
|
||||
|
||||
if (currentList.isEmpty()) {
|
||||
currentList.add(new Orderly(paramMap.get(key), index));
|
||||
}
|
||||
|
||||
orderlyList.addAll(currentList);
|
||||
// orderlyList.add(new Orderly(paramMap.get(key), index));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+11
-1
@@ -22,6 +22,7 @@
|
||||
package fun.asgc.neutrino.core.db.template;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.google.common.collect.Sets;
|
||||
import fun.asgc.neutrino.core.db.annotation.Id;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
@@ -29,6 +30,7 @@ import org.junit.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -40,7 +42,8 @@ public class JdbcTemplateTestForSqlite {
|
||||
|
||||
{
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:sqlite:" + JdbcTemplateTestForSqlite.class.getResource("/sqlite.db").getPath());
|
||||
// dataSource.setUrl("jdbc:sqlite:" + JdbcTemplateTestForSqlite.class.getResource("/sqlite.db").getPath());
|
||||
dataSource.setUrl("jdbc:sqlite:../data.db");
|
||||
dataSource.setDriverClassName("org.sqlite.JDBC");
|
||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
@@ -56,6 +59,13 @@ public class JdbcTemplateTestForSqlite {
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void 查询列表() throws SQLException {
|
||||
// 此处只能传数组,不能传集合
|
||||
List<User> userList = jdbcTemplate.queryForList(User.class, "select * from user where id in (?,?,?,?)", new Object[]{1,2,3,4});
|
||||
System.out.println(userList);
|
||||
}
|
||||
|
||||
@Accessors(chain = true)
|
||||
@Data
|
||||
public static class User {
|
||||
|
||||
+3
-2
@@ -22,6 +22,7 @@
|
||||
package fun.asgc.neutrino.proxy.server.dal;
|
||||
|
||||
import fun.asgc.neutrino.core.annotation.Component;
|
||||
import fun.asgc.neutrino.core.annotation.Param;
|
||||
import fun.asgc.neutrino.core.db.annotation.ResultType;
|
||||
import fun.asgc.neutrino.core.db.annotation.Select;
|
||||
import fun.asgc.neutrino.core.db.mapper.SqlMapper;
|
||||
@@ -55,6 +56,6 @@ public interface UserMapper extends SqlMapper {
|
||||
UserDO findById(Integer id);
|
||||
|
||||
@ResultType(UserDO.class)
|
||||
@Select("select * from user where id in (?)")
|
||||
List<UserDO> findByIds(Set<Integer> ids);
|
||||
@Select("select * from user where id in (:ids)")
|
||||
List<UserDO> findByIds(@Param("ids") Set<Integer> ids);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user