1. mybatis를 통해 주어진 값에 따라 다르게 실행 할 수 있는 방법이 존재한다 대표적으로 , 이다
<choose> 태그 사용
2. controller
<java />
@Controller
public class HomeController {
@Inject
SqlSession sqlSession;
@RequestMapping(value = "pooney", method = RequestMethod.GET)
public String pooney(@RequestParam String id) {
System.out.println("id : "+ id);
List<String> list = sqlSession.selectList("list.selectlist" ,id);
System.out.println(list);
return "index";
}
}
3. xml
when절에 변수명은 단일파라미터의 경우 _parameter를 사용해야한다
<html />
<select id="selectlist" resultType="string">
<choose>
<when test="_parameter.equals('h11') ">
select userpwd from test1 where userid = #{_parameter}
</when>
<otherwise>
select name from test2 where id = #{_parameter}
</otherwise>
</choose>
</select>
4. test1 table

5. table2 table

6. 실행화면
id 값으로 h11을 넘긴 결과


id 값으로 h66을 넘긴 결과


id 값에 따라 쿼리문이 다르게 실행되는 것을 확인 할 수 있다.
<if> 태그 사용
if문도 조건에 따라 쿼리를 다르게 실행가능하게 할 수 있다. 아래는 id가 h11이면 where절을 붙이고 아니면 where 절을 안붙이게 하는 코드이다.
7. xml
<html />
<select id="selectlist" resultType="string">
select name from test2
<if test="_parameter.equals('h11')">
where id = #{_parameter}
</if>
</select>
8.
9. 실행화면
id 값으로 h11을 넘긴 결과


id 값으로 h66을 넘긴 결과


'Spring' 카테고리의 다른 글
Spring Ajax 사용시 json으로 응답하기 (0) | 2020.05.12 |
---|---|
Spring jsp 사용시 include를 통해 css , js 가져오기 (1) | 2020.05.11 |
Spring mybatis 단일파라미터 전송시 no getter 에러 (0) | 2020.05.09 |
Spring mybatis resultMap (0) | 2020.05.07 |
Spring mybatis를 통해 객체 안에있는 객체에 값 넣기 (0) | 2020.05.07 |