JavaWeb 增删改查快速开发总结

JavaWeb增删改查基本都是那一套,其中最繁琐 变动的便是围绕SQL语句、Servlet来展开,下面是我对这些左到一个小总结,希望能对您有所帮助

首先项目搭建好之后就是最关键的SQL语句

//分页条件查询
select e.eid, e.ename, d.dname, e.money, e.edate, e.did
        from dept d,
        emp e
        where e.did = d.did
        <if test="ename != null and ename != '' ">
            and e.ename like concat('%',#{ename},'%')
        </if>
        <if test="dname != null and dname != '' ">
            and d.dname = #{dname}
        </if>
        order by e.eid desc
//根据id查询(编辑,修改)
@Select("select e.eid, e.ename, d.dname, e.money, e.edate, e.did from dept d,emp e where e.did = d.did and eid=#{eid}")
    Emp selectById(String eid);

//添加语句
@Insert("INSERT INTO emp values(null,#{ename},#{did},#{edate},#{money})")
    int add(Emp emp);

//更新语句
@Update("update emp set ename = #{ename},did =#{did},edate = #{edate},money = #{money} where eid = #{eid}")
    int update(Emp emp);

//删除语句
@Delete("delete from emp where eid =#{eid}")
    int delete(String eid);

//批量删除
int delBatch(List<String> list);
delete from emp
        <where>
            <foreach collection="list" open="eid in (" close=")" item="eid" separator=",">
                #{eid}
            </foreach>
        </where>

Servlet代码

//获取接口type

String type = request.getParameter("type");

//判断type属性后调用的是哪个接口
//查询所有(分页)
if (type !=null && "query".equals(type)){
    try {
        this.query(request,response);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//根据id查询
if (type !=null && "selectById".equals(type)){
            try {
                this.selectById(request,response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

//添加或者修改
if (type !=null && "addOrUpdate".equals(type)){
            try {
                this.addOrUpdate(request,response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

//删除
if (type !=null && "delete".equals(type)){
            try {
                this.delete(request,response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

//批量删除
if (type !=null && "delBatch".equals(type)){
            try {
                this.delBatch(request,response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

Servlet方法

private void query(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Cond cond = JSONUtils.getInstance(request, Cond.class);
        PageHelper.startPage(cond.getPage(), cond.getSize());

        List<Emp> list = empService.selectByCond(cond);

        PageInfo pageInfo = new PageInfo(list);

        response.getWriter().write(JSON.toJSONString(new RespResult(2000,pageInfo)));
    }

private void selectById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String eid = request.getParameter("eid");

        Emp emp = empService.selectById(eid);

        response.getWriter().write(JSON.toJSONString(new RespResult(2000,emp)));

    }

private void addOrUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Emp emp = JSONUtils.getInstance(request, Emp.class);
        int count =0;

        if (emp.getEid() !=null && emp.getEid() !=0){
            count = empService.update(emp);
        }else {
            count = empService.add(emp);
        }

        if (count>0){
            response.getWriter().write(JSON.toJSONString(new RespResult(2000,"操作成功")));
        }else {
            response.getWriter().write(JSON.toJSONString(new RespResult(5000,"操作失败")));
        }

    }

private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String eid = request.getParameter("eid");
        int count = empService.delete(eid);

        if (count>0){
            response.getWriter().write(JSON.toJSONString(new RespResult(2000,"操作成功")));
        }else {
            response.getWriter().write(JSON.toJSONString(new RespResult(5000,"操作失败")));
        }
    }

private void delBatch(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BufferedReader reader = request.getReader();

        String line = reader.readLine();

        List<String> list = JSON.parseArray(line, String.class);

        int count = empService.delBatch(list);

        if (count>0){
            response.getWriter().write(JSON.toJSONString(new RespResult(2000,"操作成功")));
        }else {
            response.getWriter().write(JSON.toJSONString(new RespResult(5000,"操作失败")));
        }

    }

然后是前端methods()方法

//分页
		queryAll() {
                axios.post("/empServlet?type=query", this.cond).then(resp => {
                    this.tableData = resp.data.data.list;
                    this.total = resp.data.data.total;
                })
            }

//添加
		add(){
                axios.post("/empServlet?type=addOrUpdate",this.emp).then(resp=>{
                    if (resp.data.code == 2000){
                        this.$message.success(resp.data.message);
                        this.dialogFormVisible =false;
                        this.queryAll();
                    }else {
                        this.$message.error(resp.data.message);
                    }

                })
            }

//修改(根据Id查询)
		handleEdit(eid) {
                axios.get("/empServlet?type=selectById&eid="+eid).then(resp=>{
                    console.log(resp);
                    this.emp = resp.data.data;
                    this.dialogFormVisible = true;
                })
            }

删除和批量删除(这里用element做了个删除提示)

//刪除      
    handleDelete(eid) {
                this.$confirm('您确定要删除', '提示', {   
                 confirmButtonText: '确定',                    
                 cancelButtonText: '取消',                    
                 type: 'warning'
              }).then(() => { 
                   axios.get("/empServlet?type=delete&eid="+eid).then(resp=>{
                        if (resp.data.code ==2000){ 
                           this.$message({                                
                           type: 'success',                                
                           message: '删除成功!'                            
                        });                            
                           this.queryAll();
                      }                    
                   })                                    
            }).catch(() => {                    
                this.$message({                        
                type: 'info',                        
                message: '已取消删除'                    
             });
         });            
       }
//批量删除

//先获取全选数组
		handleSelectionChange(val) {
                this.ids=[];
                for (var i=0;i<val.length;i++){
                    this.ids.push(val[i].eid);
                }
            }
//然后调用接口
	同上删除一样(换个axios请求接口)
	axios.post("/empServlet?type=addOrUpdate",this.emp)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值