Java SpringMVC + Mybatis A set of steps for complete data acquisition from front to back

[Reprint] https://blog.csdn.net/x277151676/article/details/76045368

1. In the front-end jsp page, the Ajax method is generally used to obtain back-end data for front-end use.
$.ajax({
url: "<c:url value='/strategy/deleteCelue'/>",//The url address of the request is also the address of the controller method you need to jump to (for reference only, specifically Actual prevail!)
async:true,//Whether the request is asynchronous, the default is asynchronous, which is also an important feature of ajax
data: {
data:data
},//Parameter value
type: "POST", //Request method
success:function( data){
//Process when the request is successful
}
}); 

 

Note: A point at the core of SpringMvc is transactions! Among them, the addition, deletion, modification and query of the database must be operated according to the transaction!


@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);//It is used to get the accepted id and assign it to the module and pass it to the Service!
i = strategyService.deleteCelue(strategyEntity);
}
} catch (Exception e) {
// TODO: handle exception
}
return i;
}
3. Method in Service:
public Integer deleteCelue(StrategyEntity strategyEntity);//Model for receiving !
4.StrategyServiceImpl class is mainly used to implement methods in Service!
@Autowired
private StrategyMapper strategyMapper;//For implementing the methods in the mapper interface
@Override
public Integer deleteCelue(StrategyEntity strategyEntity) {
// TODO Auto-generated method stub
return strategyMapper.deleteCelue(strategyEntity);//? ? ?
}
 5. In addition, the Mpper interface needs to be created and placed under the dao backpack to send the request to the xml to perform the corresponding operation. The following is part of the project structure:

continue to look at the method in the mapper interface: public Integer deleteCelue(StrategyEntity strategyEntity) ;//The front is the method implemented in the return type 6.mapper xml:
 <delete id="deleteCelue" parameterType="com.pushtime.ferry.model.StrategyEntity">
   DELETE FROM file_strategy WHERE user_id = #{userId};
   </ delete>

id: is the method name that needs to be implemented in the mapper interface
parameterType: the model class that needs to be brought in!
 #{userId} : The userId in the model class is obtained with #{}!
Delete success returns 1 and failure is -1!


Then you can judge whether the deletion is successful! There are mistakes, please advise!

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297530&siteId=291194637