Mybatis實現(xiàn)批量刪除和多條件查詢方式
更新時間:2026年05月14日 09:12:43 作者:Rk..
本文介紹了批量刪除功能的實現(xiàn)步驟,包括后臺代碼的修改,具體涉及到mapper層、Service層、Controller接口等層面的修改,同時也介紹了多條件查詢功能的實現(xiàn),具體包括創(chuàng)建封裝查詢條件的類,mapper層、Service層、Controller接口的修改等
一、批量刪除后臺代碼
1、mapper層
Mapper接口
//批量刪除商品的功能
int deleteBatch(String[] ids);
Mapper.xml
<!-- 批量刪除-->
<delete id="deleteBatch">
delete from product_info where p_id in
<foreach collection="array" item="pid" separator="," open="(" close=")">
#{pid}
</foreach>
</delete>注意:接口傳過來的是String數組,foreach循環(huán)中取的是array
2、Service層
Service接口
int deleteBatch(String[] ids);
Service實現(xiàn)類
@Override
public int deleteBatch(String[] ids) {
return productInfoMapper.deleteBatch(ids);
}3、Controller接口
//批量刪除 pids是要刪除的商品的id字符串,例如:1,2,3,4,5,
@RequestMapping("/deletebatch")
public String deleteBatch(String pids,HttpServletRequest request) {
String[] ps = pids.split(",");//轉為數組[1,2,3,4,5]
int num= 0;
try {
num = productInfoService.deleteBatch(ps);
if(num>0)
{
request.setAttribute("msg","批量刪除成功!");
}else {
request.setAttribute("msg","批量刪除失敗");
}
} catch (Exception e) {
request.setAttribute("msg","商品不可刪除!");
}
//批量刪除后 重新分頁查詢
return "forward:/prod/deleteAjaxSplit.action";
}4、前臺視圖
<input type="button" class="btn btn-warning" id="btn1"
value="批量刪除" onclick="deleteBatch()">
</div>
js部分:
//批量刪除
function deleteBatch() {
//取得所有被選中刪除商品的pid
var zhi=$("input[name=ck]:checked");
var str="";
var id="";
if(zhi.length==0){
alert("請選擇將要刪除的商品!");
}else{
// 有選中的商品,則取出每個選 中商品的ID,拼提交的ID的數據
if(confirm("您確定刪除"+zhi.length+"條商品嗎?")){
//拼接ID
$.each(zhi,function (index,item) {
id=$(item).val(); //22 33
if(id!=null)
str += id+","; //22,33,44
});
//發(fā)送請求到服務器端
// window.location="${pageContext.request.contextPath}/prod/deletebatch.action?str="+str;
$.ajax({
url: "${pageContext.request.contextPath}/prod/deletebatch.action",
data: {"pids":str},
type: "post",
dataType:"text",
success:function (msg)
{
alert(msg);
$("#table").load("http://localhost:8099/admin/product.jsp #table");
}
});
}
}
}二、多條件查詢后臺代碼
1、創(chuàng)建一個封裝查詢條件的類
//封裝查詢條件
public class ProductInfoVo {
//商品名稱
private String pname;
//商品類型
private Integer typeid;
//最低價格
private Double lprice;
//最高價格
private Double hprice;
//補全get set 有/無參構造方法 toString()
}2、mapper層
Mapper接口:
//多條件查詢商品
List<ProductInfo> selectCondition(ProductInfoVo vo);
Mapper.xml:
<!--多條件查詢-->
<select id="selectCondition" parameterType="com.rk.pojo.vo.ProductInfoVo" resultMap="BaseResultMap">
select *
from product_info
<!--拼條件-->
<where>
<if test="pname!=null and pname!=''">
and p_name like '%${pname}%'
</if>
<if test="typeid!=null and typeid!=-1">
and type_id =#{typeid}
</if>
<if test="(lprice!=null and lprice!='') and (hprice==null or hprice=='')">
and p_price >= #{lprice}
</if>
<if test="(lprice==null or lprice=='') and (hprice!=null and hprice!='')">
and p_price <= #{hprice}
</if>
<if test="(lprice!=null and lprice!='') and (hprice!=null and hprice!='')">
and p_price between #{lprice} and #{hprice}
</if>
</where>
order by p_id desc
</select>
3、Service層
service接口:
//多條件商品查詢
List<ProductInfo> selectCondition(ProductInfoVo vo);
service實現(xiàn)類:
@Override
public List<ProductInfo> selectCondition(ProductInfoVo vo) {
return productInfoMapper.selectCondition(vo);
}4、Controller接口
//多條件查詢
@ResponseBody
@RequestMapping("/condition")
public void condition(ProductInfoVo vo, HttpSession session)
{
List<ProductInfo> list = productInfoService.selectCondition(vo);
session.setAttribute("list",list);
}查詢到的結果存入session,前端頁面從新加載table表格
5、前臺視圖
<div id="condition" style="text-align: center">
<form id="myform">
商品名稱:<input name="pname" id="pname">
商品類型:<select name="typeid" id="typeid">
<option value="-1">請選擇</option>
<c:forEach items="${ptlist}" var="pt">
<option value="${pt.typeId}">${pt.typeName}</option>
</c:forEach>
</select>
價格:<input name="lprice" id="lprice">-<input name="hprice" id="hprice">
<input type="button" value="查詢" onclick="condition()">
</form>
</div>
js部分:
//多條件查詢
function condition(){
//取出查詢條件
var pname=$("#pname").val();
var typeid=$("#typeid").val();
var lprice=$("#lprice").val();
var hprice=$("#hprice").val();
$.ajax({
type:"post",
url:"${pageContext.request.contextPath}/prod/condition.action",
data:{"pname":pname,"typeid":typeid,
"lprice":lprice,"hprice":hprice},
success:function () {
//刷新表格 后臺將查詢到的結果存儲到seession
$("#table").load("http://localhost:8099/admin/product.jsp #table");
}
}
)
}效果展示:

總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springboot中spring.profiles.include的妙用分享
這篇文章主要介紹了springboot中spring.profiles.include的妙用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
SpringCloud的JPA連接PostgreSql的教程
這篇文章主要介紹了SpringCloud的JPA接入PostgreSql 教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-06-06
Java詳解ScriptEngine接口動態(tài)執(zhí)行JS腳本
ScriptEngine是基本接口,其方法必須在本規(guī)范的每個實現(xiàn)中完全起作用。這些方法提供基本腳本功能。 寫入這個簡單接口的應用程序可以在每個實現(xiàn)中進行最少的修改。 它包括執(zhí)行腳本的方法,以及設置和獲取值的方法2022-08-08
在MyBatis的XML映射文件中<trim>元素所有場景下的完整使用示例代碼
在MyBatis的XML映射文件中,<trim>元素用于動態(tài)添加SQL語句的一部分,處理前綴、后綴及多余的逗號或連接符,示例展示了如何在UPDATE、SELECT、INSERT和SQL片段中使用<trim>元素,以實現(xiàn)動態(tài)的SQL構建,感興趣的朋友一起看看吧2025-01-01
SpringMVC中的SimpleUrlHandlerMapping用法詳解
這篇文章主要介紹了SpringMVC中的SimpleUrlHandlerMapping用法詳解,SimpleUrlHandlerMapping是Spring MVC中適用性最強的Handler Mapping類,允許明確指定URL模式和Handler的映射關系,有兩種方式聲明SimpleUrlHandlerMapping,需要的朋友可以參考下2023-10-10

