Mybatis實(shí)現(xiàn)批量刪除和多條件查詢方式
一、批量刪除后臺(tái)代碼
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數(shù)組,foreach循環(huán)中取的是array
2、Service層
Service接口
int deleteBatch(String[] ids);
Service實(shí)現(xiàn)類
@Override
public int deleteBatch(String[] ids) {
return productInfoMapper.deleteBatch(ids);
}3、Controller接口
//批量刪除 pids是要?jiǎng)h除的商品的id字符串,例如:1,2,3,4,5,
@RequestMapping("/deletebatch")
public String deleteBatch(String pids,HttpServletRequest request) {
String[] ps = pids.split(",");//轉(zhuǎn)為數(shù)組[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","商品不可刪除!");
}
//批量刪除后 重新分頁(yè)查詢
return "forward:/prod/deleteAjaxSplit.action";
}4、前臺(tái)視圖
<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("請(qǐng)選擇將要?jiǎng)h除的商品!");
}else{
// 有選中的商品,則取出每個(gè)選 中商品的ID,拼提交的ID的數(shù)據(jù)
if(confirm("您確定刪除"+zhi.length+"條商品嗎?")){
//拼接ID
$.each(zhi,function (index,item) {
id=$(item).val(); //22 33
if(id!=null)
str += id+","; //22,33,44
});
//發(fā)送請(qǐng)求到服務(wù)器端
// 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");
}
});
}
}
}二、多條件查詢后臺(tái)代碼
1、創(chuàng)建一個(gè)封裝查詢條件的類
//封裝查詢條件
public class ProductInfoVo {
//商品名稱
private String pname;
//商品類型
private Integer typeid;
//最低價(jià)格
private Double lprice;
//最高價(jià)格
private Double hprice;
//補(bǔ)全get set 有/無參構(gòu)造方法 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實(shí)現(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);
}查詢到的結(jié)果存入session,前端頁(yè)面從新加載table表格
5、前臺(tái)視圖
<div id="condition" style="text-align: center">
<form id="myform">
商品名稱:<input name="pname" id="pname">
商品類型:<select name="typeid" id="typeid">
<option value="-1">請(qǐng)選擇</option>
<c:forEach items="${ptlist}" var="pt">
<option value="${pt.typeId}">${pt.typeName}</option>
</c:forEach>
</select>
價(jià)格:<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 () {
//刷新表格 后臺(tái)將查詢到的結(jié)果存儲(chǔ)到seession
$("#table").load("http://localhost:8099/admin/product.jsp #table");
}
}
)
}效果展示:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- mybatis多條件in查詢的實(shí)現(xiàn)
- MybatisPlus實(shí)現(xiàn)多表的關(guān)聯(lián)查詢,實(shí)現(xiàn)分頁(yè),多條件查詢?nèi)^程
- MyBatis-Plus 復(fù)雜查詢Lambda+Wrapper 多條件功能實(shí)現(xiàn)
- MybatisPlus分頁(yè)查詢與多條件查詢介紹及查詢過程中空值問題的解決
- MyBatisPlus-QueryWrapper多條件查詢及修改方式
- MyBatis中多條件查詢商品的三種方法及區(qū)別
- mybatis collection 多條件查詢的實(shí)現(xiàn)方法
相關(guān)文章
springboot中spring.profiles.include的妙用分享
這篇文章主要介紹了springboot中spring.profiles.include的妙用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
SpringCloud的JPA連接PostgreSql的教程
這篇文章主要介紹了SpringCloud的JPA接入PostgreSql 教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-06-06
Java詳解ScriptEngine接口動(dòng)態(tài)執(zhí)行JS腳本
ScriptEngine是基本接口,其方法必須在本規(guī)范的每個(gè)實(shí)現(xiàn)中完全起作用。這些方法提供基本腳本功能。 寫入這個(gè)簡(jiǎn)單接口的應(yīng)用程序可以在每個(gè)實(shí)現(xiàn)中進(jìn)行最少的修改。 它包括執(zhí)行腳本的方法,以及設(shè)置和獲取值的方法2022-08-08
在MyBatis的XML映射文件中<trim>元素所有場(chǎng)景下的完整使用示例代碼
在MyBatis的XML映射文件中,<trim>元素用于動(dòng)態(tài)添加SQL語(yǔ)句的一部分,處理前綴、后綴及多余的逗號(hào)或連接符,示例展示了如何在UPDATE、SELECT、INSERT和SQL片段中使用<trim>元素,以實(shí)現(xiàn)動(dòng)態(tài)的SQL構(gòu)建,感興趣的朋友一起看看吧2025-01-01
SpringMVC中的SimpleUrlHandlerMapping用法詳解
這篇文章主要介紹了SpringMVC中的SimpleUrlHandlerMapping用法詳解,SimpleUrlHandlerMapping是Spring MVC中適用性最強(qiáng)的Handler Mapping類,允許明確指定URL模式和Handler的映射關(guān)系,有兩種方式聲明SimpleUrlHandlerMapping,需要的朋友可以參考下2023-10-10
Maven多模塊項(xiàng)目調(diào)試與問題排查的完整指南
在現(xiàn)代企業(yè)級(jí)Java開發(fā)中,Maven多模塊項(xiàng)目因其清晰的代碼組織,依賴管理和高效的構(gòu)建流程已成為主流架構(gòu)模式,本文深入剖析多模塊項(xiàng)目的四大核心痛點(diǎn)解決方案,感興趣的小伙伴可以跟隨小編一起了解下2025-06-06
java實(shí)現(xiàn)Yaml轉(zhuǎn)Json示例詳解
這篇文章主要為大家介紹了java實(shí)現(xiàn)Yaml轉(zhuǎn)Json示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

