您的位置:首页 > 编程语言 > Java开发

Java学习札记——后台增删改查

2016-12-08 13:10 417 查看
最近在公司实习的过程中我接触到了我人生中第一个Project,今天就来说说我最近的一点小成果吧——增删改查的后台语句。

这个项目(Spring MVC)的后台主要分成了三层(在此我将实体类也写入了本文):

实体类:(实体类就是定义一些字段和构造函数的)

public class Compoents {
/**
*主键
*/
private Integer id;
/**
* 名称
*/
private String compoentname;

public Integer getId() {

return id;
}
public void setId(Integer id) {

this.id = id;
}
public String getCompoentname() {

 return compoentname;
}
public void setCompoentname(String compoentname) {

 this.compoentmodel = compoentmodel;
}

}

Mapper层:(Mapper会传递参数相对应的xml层,也就是Mybatis,有关Mybatis也就是数据库语言的增删改查,这个我会另讲。)

public interface CompoentsMapper {

/**
* 根据id查询
* @param id
* @return
*/
public List<Compoents> queryCompoentsById(Integer id);

/**
     * 修改元器件
     * @param map
     */
    public void updateCompoents(Map map);
    
/**
     * 删除元器件
     * @param id
     */
    public void deleteCompoents(Integer id);

/**
     * 添加元器件
     * @param map
     */
    public void insertCompoents(Map map);

}


Service层:(其中  private CompoentsMapper compoentsMapper;  就是连接Mapper层的)

public class CompoentsService {

 @Autowired
private CompoentsMapper compoentsMapper;

/**
     * 根据id查询
     * @param id
     * @return
     */
    public List<Compoents> QueryCompoentById(Integer id){

        return compoentsMapper.queryCompoentsById(id);
    }

/**
     * 添加
     * @param map
     */
    public void insertCompoent(Map map){

        compoentsMapper.insertCompoents(map);
    }

    /**
     * 修改
     * @param map
     */
    public void updateCompoent(Map map){

        compoentsMapper.updateCompoents(map);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteCompoent(Integer id){

        compoentsMapper.deleteCompoents(id);
    }

}


Controller层:(其中  private CompoentsService compoentsService;  就是注入Service层的)

public class CompoentsController {

 @Autowired
private CompoentsService compoentsService;

/***
     * 删除
     * @param req
     * @param resp
     * @throws IOException
     */
    @RequestMapping(value="/deletecm",method={RequestMethod.POST})
    public void deleteCM(HttpServletRequest req,HttpServletResponse resp) throws IOException{

        Integer id = Integer.parseInt(req.getParameter("id"));

        String addMsg = "";

        try {

            compoentsService.deleteCompoent(id);

        } catch (Exception e) {

            addMsg = "Error";
        }

        String result =new Gson().toJson(addMsg);

        resp.getWriter().write(result);
    }
    
    /**
     * 修改
     * @param id
     * @param compoentname
     * @param compoentmodel
     * @param manufacturefactory
     * @param req
     * @param resp
     * @throws IOException
     */
    @RequestMapping(value="/updatecm",method={RequestMethod.POST})
    public void UpdateCM(@RequestParam("id") Integer id,
@RequestParam("compoentname") String compoentname,
@RequestParam("compoentmodel")String compoentmodel,
HttpServletRequest req,HttpServletResponse resp) throws IOException{

            Map<String,Object> map = new HashMap<String,Object>();

            map.put("id", id);

            map.put("compoentname", compoentname);

            map.put("compoentmodel", compoentmodel);

            String addMsg = "";

            try {

                compoentsService.updateCompoent(map);

            } catch (Exception e) {

                addMsg = "Error";

            }

            String result =new Gson().toJson(addMsg);

            resp.getWriter().write(result);
    }

    /**
     * 添加
     * @param compoentname
     * @param compoentmodel
     * @param manufacturefactory
     * @param orgid
     * @param req
     * @param resp
     * @throws IOException
     */
    @RequestMapping(value="/insertcm",method={RequestMethod.POST})
    public void InsertCM(@RequestParam("compoentname") String compoentname,
@RequestParam("compoentmodel") String compoentmodel,
HttpServletRequest req,HttpServletResponse resp) throws IOException{

        Map<String,Object> map = new HashMap<String,Object>();

        map.put("compoentname", compoentname);

        map.put("compoentmodel", compoentmodel);

        String addMsg = "";

        try {

            compoentsService.insertCompoent(map);

        } catch (Exception e) {

            addMsg = "Error";

        }

        String result =new Gson().toJson(addMsg);

        resp.getWriter().write(result);
    }

    /**
     * 根据Id查询
     * @param req
     * @param resp
     * @throws IOException
     */
    @RequestMapping(value="querycmbyid",method={RequestMethod.GET})
    public void QueryCMById(HttpServletRequest req,HttpServletResponse resp) throws IOException{

        Integer id = Integer.parseInt(req.getParameter("id"));

        List<Compoents> listcm = new ArrayList<Compoents>();

        listcm = compoentsService.QueryCompoentById(id);

        String result=new Gson().toJson(listcm);

        String jsonp = req.getParameter("jsoncallback");

        resp.setCharacterEncoding("UTF-8");

        resp.setContentType("text/html");

        if(jsonp != null){

            result = jsonp+"("+result+")";

            resp.getWriter().write(result);

        }else{

            resp.getWriter().write(result);
        }
    }

}


注意:这里只是把大概代码给大家看一下,代码详细的功能我会再讲。
                                                                                                                                                                                          再接再励

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息