[英]Designing INSERT mapper for myBatis3 for given pojo type
如果我有像這樣的pojo模型,myBatis3中的INSERT映射器將如何顯示
class DR {
FormatType format;
}
class SQLDR extends DR{
String sql;
}
class PDR extends DR {
Predicate predicate;
}
class Download {
DR request;
String status;
long size;
}
我想在表中插入下載對象,如何在myBatis3中設計INSERT映射器,因為我在運行時設置了SQLDR和PDR類型的下載,因此它應該能夠根據格式類型取值並進行設置。
我正在嘗試這樣的映射器, 它不起作用
<sql id="DOWNLOAD_FIELD_TYPES">
#{key,jdbcType=VARCHAR},
<choose>
<when test="format = 'SQL'"> #{request.sql,jdbcType=VARCHAR},</when>
<otherwise> #{request.predicate,jdbcType=VARCHAR}, </otherwise>
</choose>
#{status,jdbcType=OTHER},
#{size,jdbcType=BIGINT},
#{request.format,jdbcType=OTHER},
</sql>
<sql id="DOWNLOAD_FIELDS">
key,filter,status,size,format
</sql>
<insert id="create" parameterType="Download">
INSERT INTO download(<include refid="DOWNLOAD_FIELDS"/>)
VALUES(<include refid="DOWNLOAD_FIELD_TYPES"/>)
</insert>
原因:org.apache.ibatis.reflection.ReflectionException:名稱為“ sql”的屬性沒有吸氣劑。 在“類DR”中
在這種情況下,最簡單的方法是使DR
抽象化並為其添加一個方法:
abstract class DR {
public String getFilter();
}
class SQLDR {
String sql;
public String getFilter() {
return sql;
}
}
class PDR {
Predicate predicate;
public String getFilter() {
return toString(predicate);
}
}
然后在沒有任何條件的情況下在映射器中使用此新屬性:
<sql id="DOWNLOAD_FIELD_TYPES">
#{key,jdbcType=VARCHAR},
#{request.filter},
#{status,jdbcType=OTHER},
#{size,jdbcType=BIGINT},
#{request.format,jdbcType=OTHER},
</sql>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.