pooney
article thumbnail

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을 넘긴 결과 

 

 

 

 

 

profile

pooney

@pooney

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!