2014-03-30 3 views
1

내가 MyBatis로의 조항 및 다음있어 예외가 여기에 문제를 식별하는 데 도움 주시기 바랍니다 다음 시도하면 ..MyBatis로 절

public class Student{ 

private Integer studId; 
private String name; 
private String email; 
private Date dob; 
} 

매핑

<select id="searchStudent" parameterType="hashmap" resultMap="StudentResult"> 
    <![CDATA[ 
    SELECT * FROM STUDENTS 
    WHERE 1 = 1 

    <if test="studId != null"> 
    AND STUD_ID= #{studId} 
    </if> 

    <if test="name != null"> 
    AND NAME like #{name} 
    </if> 

    ]]> 
</select> 

는 예외 내가 얻을 :

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test="studId != null"> 
    AND STUD_ID= 1 
    </if> 

    <if test="name != null"> 
    ' at line 4 
### The error may exist in StudentMapper.xml 
### The error may involve com.maventest.mytest.StudentMapper.searchStudent-Inline 
### The error occurred while setting parameters 
### SQL: SELECT * FROM STUDENTS WHERE 1 = 1  <if test="studId != null"> AND STUD_ID= ? </if>  <if test="name != null"> AND NAME like ? </if> 
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test="studId != null"> 
    AND STUD_ID= 1 
    </if> 

    <if test="name != null"> 
    ' at line 4 
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23) 

답변

4

<![CDATA[]]>을 제거하면 모든 쿼리와 mybat가 이스케이프됩니다. is가 전혀 처리하지 않으므로 if이 그대로 쿼리에 전달됩니다.

<select id="searchStudent" parameterType="hashmap" resultMap="StudentResult"> 
    SELECT * FROM STUDENTS 
    WHERE 1 = 1 

    <if test="studId != null"> 
    AND STUD_ID= #{studId} 
    </if> 

    <if test="name != null"> 
    AND NAME like #{name} 
    </if> 

</select> 
0

CDATA 섹션은 마크 업 [ORACLE 정의]로 간주되는 문자가 포함 된 텍스트 블록을 이스케이프하는 데 사용됩니다. 때로는

, 우리는 우리가 같은 마크 업이있는 곳 조건이 특별히 때, 그것을 필요 :

<![CDATA[ SELECT * FROM STUDENTS WHERE 1 = 1 ]]> 
<if test="studId != null"> 
<![CDATA[ AND STUD_ID= #{studId} ]]> 
</if> 

<if test="name != null"> 
**<![CDATA[ AND COD_PER <> 'ASSU' ]]>** 
</if> 

단지에 추가 등 <,>, <>을 마크 업 문제가있는 라인도 작동합니다.