1,后端代码的实现:
(1)编写一个BaseService泛型接口
public interface BaseService<T> {
public int insert(T t);
public int update(T t);
public int delete(T t);
public List<T> queryData(T t);
public int deleteByPrimaryKey(T t);
(2)编写一个EmpService接口
public interface EmpService extends BaseService<EmpBean> {
(3)再写一个EmpServiceImpl实现类,实现增删改查的功能
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
EmpBeanMapper empBeanMapper;//这是依赖注入!
public int insert(EmpBean empBean) {
return empBeanMapper.insertSelective(empBean);
}
有mybatis直接调用配置文件中的方法,就不需要自己写方法了。
(4)写一个JsonResultBean来存储信息
public class JsonResultBean {
private int code;
private String msg;
private Object data;
里面实现get,set方法,还有toString方法。
2,创建一个controller包。
(1)实现增加操作:
@RequestMapping("/empAction")
@Controller
public class EmpAction {
@Autowired
EmpService empService;
@RequestMapping("/insert")
@ResponseBody
public JsonResultBean insert(EmpBean empBean){
JsonResultBean jsonResultBean = new JsonResultBean();
try {
empService.insert(empBean);
jsonResultBean.setMsg("执行成功!");
}catch (Exception e){
e.printStackTrace();
jsonResultBean.setCode(1);
jsonResultBean.setMsg("系统错误"+e.getMessage());
}
return jsonResultBean;
}
(2)实现查询操作,将数据库中所有的数据查询到网页表格中
@RequestMapping("/query")
@ResponseBody
public JsonResultBean query(EmpBean empBean){
JsonResultBean jsonResultBean = new JsonResultBean();
try {
// 用一个List集合
List<EmpBean> empBeanList = empService.queryData(empBean);
jsonResultBean.setMsg("成功");
jsonResultBean.setData(empBeanList);
}catch (Exception e){
e.printStackTrace();;
jsonResultBean.setCode(1);
jsonResultBean.setMsg("系统错误:"+e.getMessage());
}
return jsonResultBean;
}
(3) 实现删除操作
@RequestMapping(value = "/delete",params = {"empno"})
// 将empno映射到empBean中。
@ResponseBody
public JsonResultBean delete(EmpBean empBean){
System.out.println(empBean);
JsonResultBean bean = new JsonResultBean();
try{
empService.deleteByPrimaryKey(empBean);
bean.setMsg("删除成功!");
}catch(Exception e){
e.printStackTrace();
bean.setCode(1);
bean.setMsg("系统错误"+e.getMessage());
}
return bean;
}
原文地址:https://blog.csdn.net/weixin_51773317/article/details/119078614
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。