2011-12-10 29 views
9

tôi đã sử dụng mybatis xuân-1.0.3-SNAPSHOT mybatis-3.0.6 spring3.0.6.I cố gắng để xóa bản ghi từ một bảng như thế này:Mybatis-Lỗi thiết lập tham số rỗng

 <delete id="deleteNote" parameterType="hashMap"> 
    DELETE FROM BBSCS_NOTE 
    <where> 
     <if test="ids !=null and ids.length > 0"> 
      <foreach collection="ids" item="id" open="(" close=")" separator=","> 
       ID IN #{id} 
      </foreach> 
     </if> 
     <if test="toID != null and toID != ''">AND TOID = #{toID}</if> 
     <if test="fromID != null and fromID != ''">AND FROMID = #{fromID}</if> 
     <if test="noteType != -1">AND NOTETYPE = #{noteType}</if> 
    </where>   
</delete> 

Như bạn có nhìn thấy, đó là một sql.The mã kiểm tra java năng động như thế này:

Map map = new HashMap(); 
String ids[] = {"1","2","3"}; 
map.put("ids", ids); 
noteService.del(map); 

Khi tôi thực hiện mã kiểm tra java, đã có một số ngoại lệ như thế này:

org.springframework.jdbc.UncategorizedSQLException: Error setting null parameter. Most JDBC drivers require that the JdbcType must be specified for all nullable parameters. Cause: java.sql.SQLException: Invalid column type 
; uncategorized SQLException for SQL []; SQL state [null]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type 

Wh Bạn có thể cho tôi một số lời khuyên để giải quyết vấn đề này không?

Trả lời

15

OK Tôi thấy một vài vấn đề. Đầu tiên, khi thiết lập một tham số null vào một câu lệnh đã chuẩn bị hoặc một tuyên bố có thể gọi, MyBatis cần phải biết kiểu jdbc. Như thế này,

#{myNullParamenter, jdbcType=VARCHAR} 

Bạn cũng đang tạo mệnh đề 'in không chính xác'. Bạn cần sử dụng thẻ foreach để chỉ tạo danh sách các giá trị. Di chuyển phần "ID IN" ra khỏi thẻ foreach.

<if test="ids !=null and ids.length > 0"> 
    ID IN 
    <foreach collection="ids" item="id" open="(" close=")" separator=","> 
     #{id} 
    </foreach> 
</if> 

Tôi cũng khuyên bạn không nên sử dụng HashMaps. Các lớp Mapper mới tốt hơn nhiều.

7

Vấn đề là do phiên bản 3.0.x, kiểu JDBC mặc định cho tham số null là Types.OTHER không được hỗ trợ bởi một số trình điều khiển JDBC như Oracle 10g.

Here bài đăng giải thích vấn đề này.

Giải pháp tôi tìm thấy rất đơn giản, tôi đặt jdbcTypeForNull thành NULL trong tệp cấu hình.

<configuration> 
    <properties resource="mybatis-config.properties" /> 
    <settings> 
     <setting name="jdbcTypeForNull" value="NULL" /> 
    </settings> 

    <environments default="development"> 
    .... 
    </environments> 

    <mappers> 
    .... 
    </mappers> 
</configuration> 
Các vấn đề liên quan