Skip to content

Commit dde41ad

Browse files
authored
Merge pull request #28 from windybirth/feature_for_chen
Repair search logic by JDBC
2 parents 3afd97c + bf0699c commit dde41ad

File tree

10 files changed

+182
-65
lines changed

10 files changed

+182
-65
lines changed

skill/src/main/java/jp/wicresoft/controller/SearchController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public String loadPage(@Valid SkillSearchInfo skillSearchInfo, Model model) {
4848
}
4949

5050
try {
51-
List<Long> conditionList = new ArrayList<Long>();
51+
List<Integer> conditionList = new ArrayList<Integer>();
5252
for (String skillIdStr : skillSearchInfo.getLsSkill()) {
53-
conditionList.add(Long.parseLong(skillIdStr));
53+
conditionList.add(Integer.parseInt(skillIdStr));
5454
}
5555
List<IndexViewInfo> indexViewInfos = searchImpl.getSearchResultBySkill(conditionList);
5656
// 結果を出す

skill/src/main/java/jp/wicresoft/impl/SearchImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import jp.wicresoft.domain.SkillTitleMst;
1010
import jp.wicresoft.domain.StuffMeta;
1111
import jp.wicresoft.info.IndexViewInfo;
12+
import jp.wicresoft.jdbc.SkillSearchJdbcTemplate;
1213
import jp.wicresoft.repository.SkillTitleMstRepository;
1314
import jp.wicresoft.repository.StuffMetaRepository;
1415

@@ -21,12 +22,15 @@ public class SearchImpl {
2122
@Autowired
2223
StuffMetaRepository stuffMetaRepository;
2324

25+
@Autowired
26+
SkillSearchJdbcTemplate skillSearchJdbcTemplate;
27+
2428
public List<SkillTitleMst> getSkillNameList() {
2529
return skillTitleMstRepository.findAll();
2630
}
2731

28-
public List<IndexViewInfo> getSearchResultBySkill(List<Long> titleIds) {
29-
List<StuffMeta> stuffMetas = stuffMetaRepository.findByIds(titleIds);
32+
public List<IndexViewInfo> getSearchResultBySkill(List<Integer> titleIds) {
33+
List<StuffMeta> stuffMetas = skillSearchJdbcTemplate.findByIds(titleIds);
3034
List<IndexViewInfo> indexViewInfos = new ArrayList<>();
3135
for (StuffMeta stuffMeta : stuffMetas) {
3236
indexViewInfos.add(new IndexViewInfo(stuffMeta));

skill/src/main/java/jp/wicresoft/info/IndexViewInfo.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ public class IndexViewInfo {
4545
*/
4646
public IndexViewInfo(StuffMeta stuffMeta) {
4747

48-
setId(stuffMeta.getId().toString());
48+
setId(String.valueOf(stuffMeta.getId()));
4949

5050
setName(stuffMeta.getName());
5151

52-
setAge(stuffMeta.getAge().toString());
52+
setAge(String.valueOf(stuffMeta.getAge()));
5353

5454
setSex(stuffMeta.isSex() ? "true" : "false");
5555

@@ -70,11 +70,11 @@ public StuffMeta toStuffMeta() {
7070
StuffMeta stuffMeta = new StuffMeta();
7171

7272
stuffMeta.setName(getName());
73-
stuffMeta.setAge(Long.parseLong(getAge()));
73+
stuffMeta.setAge(Integer.parseInt(getAge()));
7474
// TODO
7575
stuffMeta.setSex(false);
76-
stuffMeta.setExperienceYear(Long.parseLong(getExperienceYear()));
77-
stuffMeta.setPrice(Long.parseLong(getPrice()));
76+
stuffMeta.setExperienceYear(Integer.parseInt(getExperienceYear()));
77+
stuffMeta.setPrice(Integer.parseInt(getPrice()));
7878
stuffMeta.setNationality(getNationality());
7979
stuffMeta.setMember(isMember());
8080

skill_db/src/main/java/jp/wicresoft/domain/SkillTitleMst.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
public class SkillTitleMst {
1111

1212
@Id
13-
private Long skillTitleId;
13+
private int skillTitleId;
1414

1515
private String titleName;
1616

17-
private Long skillCategory;
17+
private int skillCategory;
1818
}

skill_db/src/main/java/jp/wicresoft/domain/StuffMeta.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ public class StuffMeta implements Serializable{
1919

2020
@Id
2121
@GeneratedValue
22-
private Long id;
22+
private int id;
2323

2424
private String name;
2525

26-
private Long age;
26+
private int age;
2727

2828
private boolean sex;
2929

3030
private String nationality;
3131

32-
private Long experienceYear;
32+
private int experienceYear;
3333

34-
private Long price;
34+
private int price;
3535

3636
private boolean isMember;
37+
3738
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package jp.wicresoft.jdbc;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class BasicJdbc {
10+
11+
public <T> List<T> convertJdbcMapToBean(List<Map<String, Object>> mapList, Class<T> clazz) {
12+
List<T> resultList = new ArrayList<T>();
13+
for (Map<String, Object> map : mapList) {
14+
try {
15+
// initial Object T
16+
T object = clazz.newInstance();
17+
Method[] methods = clazz.getMethods();
18+
for (String key : map.keySet()) {
19+
// get each object and name
20+
Object value = map.get(key);
21+
// get setMethod
22+
for (Method method : methods) {
23+
if (method.getName().equals(translateSetMethodName(key))) {
24+
method.invoke(object, value);
25+
}
26+
}
27+
}
28+
resultList.add(object);
29+
} catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
return resultList;
34+
}
35+
36+
private String translateSetMethodName(String key) {
37+
return "set" + key.substring(0, 1).toUpperCase() + key.substring(1);
38+
}
39+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package jp.wicresoft.jdbc;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.jdbc.core.JdbcTemplate;
8+
import org.springframework.stereotype.Component;
9+
10+
import jp.wicresoft.domain.StuffMeta;
11+
12+
@Component
13+
public class SkillSearchJdbcTemplate extends BasicJdbc{
14+
15+
@Autowired
16+
JdbcTemplate jdbcTemplate;
17+
18+
final private int INDEX_START = 1;
19+
20+
public List<StuffMeta> findByIds(List<Integer> titleIds) {
21+
if (titleIds.isEmpty()) {
22+
return new ArrayList<StuffMeta>();
23+
}
24+
25+
StringBuffer joinBuffer = new StringBuffer();
26+
StringBuffer andBuffer = new StringBuffer();
27+
StringBuffer paramBuffer = new StringBuffer();
28+
for (int currentIndex = INDEX_START; currentIndex <= titleIds.size(); currentIndex++) {
29+
joinBuffer.append(" inner join stuff_skill s");
30+
joinBuffer.append(currentIndex);
31+
32+
if (currentIndex > INDEX_START) {
33+
andBuffer.append(" and s");
34+
andBuffer.append(currentIndex - 1);
35+
andBuffer.append(".stuff_id = s");
36+
andBuffer.append(currentIndex);
37+
andBuffer.append(".stuff_id");
38+
}
39+
40+
paramBuffer.append(" and s");
41+
paramBuffer.append(currentIndex);
42+
paramBuffer.append(".skill_id = ?");
43+
}
44+
String queryStr = "select meta.* from stuff_meta meta "
45+
+ joinBuffer.toString()
46+
+ " on meta.id = s"
47+
+ INDEX_START
48+
+ ".stuff_id "
49+
+ andBuffer.toString()
50+
+ paramBuffer.toString();
51+
List<Map<String, Object>> mapList = jdbcTemplate.queryForList(queryStr, titleIds.toArray());
52+
return convertJdbcMapToBean(mapList, StuffMeta.class);
53+
}
54+
}

skill_db/src/main/java/jp/wicresoft/repository/SkillTitleMstRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import jp.wicresoft.domain.SkillTitleMst;
88

9-
public interface SkillTitleMstRepository extends Repository<SkillTitleMst, Long> {
9+
public interface SkillTitleMstRepository extends Repository<SkillTitleMst, Integer> {
1010

1111
public List<SkillTitleMst> findAll();
1212
}

skill_db/src/main/java/jp/wicresoft/repository/StuffMetaRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import jp.wicresoft.domain.StuffMeta;
99

10-
public interface StuffMetaRepository extends Repository<StuffMeta, Long>{
10+
public interface StuffMetaRepository extends Repository<StuffMeta, Integer>{
1111

1212
public List<StuffMeta> findAll();
1313

0 commit comments

Comments
 (0)