ibatis 批量查询 注解方式 多参数(多字段)


1
//@Select("select id, student_id, test_paper_problem_id, score, respondence, mistake_id from T_RESPONDENCE where student_id in #{studentId} and test_paper_problem_id=#{testPaperProblemId}") 2 @SelectProvider(type = RespondenceBeanProvider.class, method = "getRespondenceBeanByStudentIdANDTestPaperProblemIdBatch") 3 @Results(value = {@Result(property = "id", column = "id"), @Result(property = "studentId", column = "student_id"), 4 @Result(property = "testPaperProblemId", column = "test_paper_problem_id"), 5 @Result(property = "score", column = "score"), @Result(property = "respondence", column = "respondence"), 6 @Result(property = "mistakeId", column = "mistake_id")}) 7 List<RespondenceBean> getRespondenceBeanByStudentIdANDTestPaperProblemIdBatch(List<Set<String>> studentIds);

上面这个代码是mapper   可以理解成dao

 

 

 8 package cn.edu.njfu.cs.eas.db.provider;

 9 

10 import java.util.List;

11 import java.util.Map;

12 import java.util.Set;

13 

14 import org.apache.commons.lang.StringUtils;

15 

16 /**19  * 

20  * @author  [email protected]

21  * @version  [版本号, 2012-9-28]24  */

25 public class RespondenceBeanProvider

26 {

27     

28     public String getRespondenceBeanByStudentIdANDTestPaperProblemIdBatch(Map map)

29     {

30 

31         /**

32          * param1-->studentIds

33          * param2-->problemIds

34          */

35         System.out.println("---" + map);

36         List<Set<String>> studentIdsAndProblems = (List<Set<String>>)map.get("list");

37 //        List<String> problemIds = (List<String>)map.get("param2");

38 

39         String studentIdsStr = StringUtils.join(studentIdsAndProblems.get(0), ',');

40         String problemIdsStr = StringUtils.join(studentIdsAndProblems.get(1), ',');

41         return "select * from T_RESPONDENCE where student_id in (" + studentIdsStr + ") and test_paper_problem_id in ("

42                 + problemIdsStr + ")";

43 //        return "select * from T_RESPONDENCE where student_id in (540) and test_paper_problem_id in (31)";

44     }

45 }

上面这个 是provider  为mapper准备

 

 

 8 package cn.edu.njfu.cs.eas.db.mapper;

 9 

10 import java.util.ArrayList;

11 import java.util.HashSet;

12 import java.util.List;

13 import java.util.Set;

14 

15 import org.junit.Before;

16 import org.junit.Test;

17 

18 import cn.edu.njfu.cs.eas.common.bean.RespondenceBean;

19 import cn.edu.njfu.cs.eas.common.util.SpringUtils;

20 

21 /**24  * 

25  * @author  [email protected]

26  * @version  [版本号, 2012-9-28]29  */

30 public class RespondenceBeanMapperTest

31 {

32     

33     private RespondenceBeanMapper mapper;

34 

35     /**36      * 

37      * @throws java.lang.Exception

38      * @see [类、类#方法、类#成员]

39      */

40     @Before

41     public void setUp() throws Exception

42     {

43         SpringUtils springUtils = new SpringUtils(new String[]{"classpath:databaseContext.xml"});

44         mapper = springUtils.getBean(RespondenceBeanMapper.class, "respondenceBeanMapper");

45     }

46 

47     /**

48      * Test method for {@link cn.edu.njfu.cs.eas.db.mapper.RespondenceBeanMapper#getRespondenceBeanByStudentIdANDTestPaperProblemIdBatch(java.util.Set, java.util.Set)}.

49      */

50     @Test

51     public void testGetRespondenceBeanByStudentIdANDTestPaperProblemIdBatch()

52     {

53         Set<String> studentIds = new HashSet<String>();

54         Set<String> testPaperProblemIds = new HashSet<String>();

55         studentIds.add("535");

56         testPaperProblemIds.add("27");

57         studentIds.add("536");

58         testPaperProblemIds.add("28");

59         

60         List<Set<String>> xxxargs = new ArrayList<Set<String>>();

61         xxxargs.add(studentIds);

62         xxxargs.add(testPaperProblemIds);

63         List<RespondenceBean> ress = this.mapper.getRespondenceBeanByStudentIdANDTestPaperProblemIdBatch(xxxargs);

64         System.out.println(ress.size());

65     }

66 

67 }

单元测试代码

  

你可能感兴趣的:(ibatis)