【轉載】https://blog.csdn.net/x277151676/article/details/76045368
1.在前台jsp頁面中一般使用Ajax方法去獲取后台數據用於前端使用。
$.ajax({
url: "<c:url value='/strategy/deleteCelue'/>",//請求的url地址也就是你所需要跳轉的controller的方法的地址(僅用參考,具體以實際為准!)
async:true,//請求是否異步,默認為異步,這也是ajax重要特性
data: {
data:data
},//參數值
type: "POST", //請求方式
success:function(data){
//請求成功時處理
}
});
注意:SpringMvc核心的一個點就是事務!其中對數據庫的增刪改查 都要根據事務來進行操作!
@Controller
@RequestMapping(value = "/strategy")
@RequestMapping(value="/deleteCelue",method=RequestMethod.POST)
public @ResponseBody Integer deleteCelue(StrategyEntity strategyEntity,String getId){
Integer i=0;
try {
if(null != getId && "" != getId){
strategyEntity.setUserId(getId);//用於獲取接受的id並賦值給modle里面 傳給Service!
i = strategyService.deleteCelue(strategyEntity);
}
} catch (Exception e) {
// TODO: handle exception
}
return i;
}
3.Service中的方法:
public Integer deleteCelue(StrategyEntity strategyEntity);//用於接收的model!
4.StrategyServiceImpl 類主要用於實現Service中的方法!
@Autowired
private StrategyMapper strategyMapper;//用於實現mapper接口里面的方法
@Override
public Integer deleteCelue(StrategyEntity strategyEntity) {
// TODO Auto-generated method stub
return strategyMapper.deleteCelue(strategyEntity);//???
}
5.此外還需要創建Mpper接口 放在dao背包下 用於將請求發送到xml中去執行相應的操作 以下是部分項目結構:
繼續看所在的mapper接口中的方法:public Integer deleteCelue(StrategyEntity strategyEntity);//前面是返回類型6.mapper xml中實現的方法:
<delete id="deleteCelue" parameterType="com.pushtime.ferry.model.StrategyEntity">
DELETE FROM file_strategy WHERE user_id = #{userId};
</delete>
id:就是在mapper接口中需要實現的方法名稱
parameterType:需要帶入的model類!
#{userId} :model類中的userId 用#{}獲取!
刪除成功返回1失敗為-1!
然后就可以判斷是否刪除成功!有錯誤之處請大家指教!