有時我們控制同一張表,但傳入的引數不固定,操作表的字段不固定,就要用到mybatis動態插入。.xml如下:
"insertmessage" parametertype="com.sf.ccsp.member.client.request.messagereq">
insert cx_customer_message
(id,memberid,
and messageclassify != "" '>
messagee_classify,
if>
and messagecode != "" '>
message_code,
if>
and messagecontent != "" '>
message_content
if>
)values (
#,#,
and messageclassify != "" '>
#,if>
and messagecode != "" '>
#,if>
and messagecontent != "" '>
# if>
)
問題來了,當messageclassify !=null,但messagecode 和messagecontent 為空時,會多出來逗號,以至於mybatis報錯。正確的用法如下.xml:
"insertmessage" parametertype="com.sf.ccsp.member.client.request.messagereq">
insert cx_customer_message
"(" suffix=")" suffixoverrides="," >
id,memberid,
and messageclassify != "" '>
messagee_classify,
if>
and messagecode != "" '>
message_code,
if>
and messagecontent != "" '>
message_content,
if>
"values (" suffix=")" suffixoverrides="," >
#,#,
and messageclassify != "" '>
#,if>
and messagecode != "" '>
#,if>
and messagecontent != "" '>
#,if>
trim標籤的屬性
prefix:字首覆蓋並增加其內容。也就是給中的sql語句加上字首;
suffix:字尾覆蓋並增加其內容。給包裹的sql語句加上字尾;
prefixoverrides:字首判斷的條件。取消指定的字首,如where;
suffixoverrides:字尾判斷的條件。取消指定的字尾,如and | or.,逗號等。
Mybatis動態插入資料(使用trim標籤)
有時我們控制同一張表,但傳入的引數不固定,操作表的字段不固定,就要用到mybatis動態插入。xml如下 insertmessage parametertype com.sf.ccsp.member.client.request.messagereq insert cx customer messa...
Mybatis 動態表名,插入資料
int insert param tablename string tablename param user user user mybatis xml id insert parametertype com.ihuaqiang.spring.bean.user insert into id,nam...
MyBatis 動態插入多條資料
mysql支援的一種插入多行資料的insert語句寫法是 insert into 表名 欄位名1,欄位名2,欄位名3 values 值1,值2,值3,值1,值2,值3,對應的介面方法 給news插入多條類別 public int addmorenewstypebynewsid param newsi...