delete from tag where t_type in('xx','xx','xx')这种形式,但是in后面在mapper里面不知道咋写了,括号里面也不是固定的,是根据哪些复选框被选中来确定的,do me a fever
controller:
@RequestMapping("/deleteTag1")
@ResponseBody
public Map<String,Object> deleteTag1(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
Map<String,Object> resultMap = new HashMap<String, Object>();
String tType1 = request.getParameter("str1");
String tType ="(" +tType1+")";
tagService.deleteTag1(tType.replace('"',' '));
resultMap.put("result", "true");
return resultMap;
}
mapper:
<delete id="deleteTagBytType" parameterType="java.lang.String" >
delete from tag
where t_type in #{tType,jdbcType=VARCHAR}
</delete>
js:
function checkTag() {
if(!confirm("确定要删除这些类别及文章吗?")){
return ;
}
else {
var cks=document.getElementsByName("title");
var str1="";
var str2="";
for(var i=0;i<cks.length;i++){
if(cks[i].checked){
var tType=document.getElementById("id"+(1+i)).innerHTML;
var tType1=tType.substring(0, tType.lastIndexOf("("));
var tType2=tType.substring(tType.indexOf("(")+1,tType.lastIndexOf(")"));
if(tType2==0) {str1+="'"+tType1+"'"+ ",";}
else {str1+="'"+tType1+"'"+ ",";}
}
} //去掉字符串末尾的‘,'
str1=str1.substring(0, str1.length-1);
str2=str2.substring(0, str2.length-1);
alert(str1);
if (!(str1==null) && !(str1=="")) {
$.ajax({
url:"http://localhost:8080/blog/deleteTag1",
type:"post",
data:{str1:str1},
dataType: "Json",
async:false,
cache:false,
success:function(data1) {
if(data1.result=="true") {
alert("删除成功!");
}
},
error : function(data1) {
alert(data1.result);
}
});
}
if (!(str2==null) && !(str2=="")) {
$.ajax({
url:"http://localhost:8080/blog/deleteTag2",
type:"post",
data:{str2:str2},
dataType: "Json",
async:false,
cache:false,
success:function(data2) {
if(data2.result=="true") {
alert("删除成功!");
}
},
error : function(data2) {
alert(data2.result);
}
});
} else { }
}
}
3 个解决方案
#1
<update id="deleteFromRecycleBin" parameterType="java.util.Map">
DELETE FROM meter_user WHERE `code` in
<foreach collection="code" item="code" index="index" open="(" separator="," close=")">
#{code}
</foreach>
</update>
DELETE FROM meter_user WHERE `code` in
<foreach collection="code" item="code" index="index" open="(" separator="," close=")">
#{code}
</foreach>
</update>
#2
int deleteTagBytType(String tType);
这个怎么改?
#3
要不就用一楼这种,传集合或者数组参数,xml里写foreach
要不就直接传拼接好的字符串参数,比如传"(1,2,3)",xml里面写 in ${参数},这种方式注意要用$,用#会默认在两头加上单引号'参数'
要不就直接传拼接好的字符串参数,比如传"(1,2,3)",xml里面写 in ${参数},这种方式注意要用$,用#会默认在两头加上单引号'参数'
#1
<update id="deleteFromRecycleBin" parameterType="java.util.Map">
DELETE FROM meter_user WHERE `code` in
<foreach collection="code" item="code" index="index" open="(" separator="," close=")">
#{code}
</foreach>
</update>
DELETE FROM meter_user WHERE `code` in
<foreach collection="code" item="code" index="index" open="(" separator="," close=")">
#{code}
</foreach>
</update>
#2
int deleteTagBytType(String tType);
这个怎么改?
#3
要不就用一楼这种,传集合或者数组参数,xml里写foreach
要不就直接传拼接好的字符串参数,比如传"(1,2,3)",xml里面写 in ${参数},这种方式注意要用$,用#会默认在两头加上单引号'参数'
要不就直接传拼接好的字符串参数,比如传"(1,2,3)",xml里面写 in ${参数},这种方式注意要用$,用#会默认在两头加上单引号'参数'