欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

easyui 1.5+spring 4.2+mybatis 3.4 Web示例(六) 单表操作之Controller层

程序员文章站 2022-05-29 14:41:50
...

controller层是spring mvc中的c。因此除了对spring要有了解,对spring mvc也要有了解。

代码结构

easyui 1.5+spring 4.2+mybatis 3.4 Web示例(六) 单表操作之Controller层

SysLogController

package com.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.model.SysLog;
import com.service.ISysLogService;
import com.util.CommUtil;
import com.util.DateUtil;

@Controller
@RequestMapping("/syslog")
public class SysLogController {
	private static Logger logger = Logger.getLogger(SysLogController.class);
	
	@Resource
	private ISysLogService sysLogServiceImpl;
	
	/**
	 * 跳转到日志页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/goSysLogList")
	public String goSysLogList(){
		logger.debug("goSysLogList");
		return "syslog/sysloglist";
	}
	
	/**
	 * 根据条件获取日志记录
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getSysLogList")
	public Map<String, Object> getSysLogList(HttpServletRequest req){
		logger.debug("getSysLogList");
		Map<String, Object> map = new HashMap<String, Object>();  
		Map paraMap = new HashMap();
		List<SysLog> list = sysLogServiceImpl.selectAll(paraMap);
		int total = list.size();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	/**
	 * 添加日志
	 * @param request
	 * @return
	 */
	@ResponseBody 
	@RequestMapping("/add")
	public Map<String, String> add(HttpServletRequest request){
		logger.debug("add");
		String operatorid = request.getParameter("operatorid").trim();
		String code = request.getParameter("code").trim();
		String logtime = request.getParameter("logtime");
		String logtype = request.getParameter("logtype");
		String loginfo = request.getParameter("loginfo").trim();
		
		Date date = null;
		try {
			date = DateUtil.convertStringToDate(DateUtil.DATETIME_FORMAT,logtime);
		} catch (Exception e) {
			logger.info("日期转换出错。");
			e.printStackTrace();
		}
		
		Map<String, String> map = new HashMap<String, String>(); 
		
		SysLog sysLog = new SysLog();
		sysLog.setLoginfo(loginfo);
		sysLog.setLogtype(CommUtil.parseInt(logtype));
		sysLog.setOperatorid(operatorid);
		sysLog.setCode(code);
		sysLog.setLogtime(date);
		
		int result = 0;
		try{
			result = sysLogServiceImpl.insertSelective(sysLog);
		}catch(Exception e){
			map.put("success", "false");
			map.put("errorMsg", "操作失败!");  
		}
		
		if(result > 0){
			   map.put("success", "true");
		}else{
			   map.put("success", "false");
			   map.put("errorMsg", "操作失败!");  
		}
		return map;
	}
	
	/**
	 * 修改日志
	 * @param request
	 * @return
	 */
	@ResponseBody 
	@RequestMapping("/update")
	public Map<String, String> update(HttpServletRequest request){
		logger.debug("update");
		String logid = request.getParameter("logid");
		String operatorid = request.getParameter("operatorid").trim();
		String code = request.getParameter("code").trim();
		String logtime = request.getParameter("logtime");
		String logtype = request.getParameter("logtype");
		String loginfo = request.getParameter("loginfo").trim();
		
		Date date = null;
		try {
			date = DateUtil.convertStringToDate(DateUtil.DATETIME_FORMAT,logtime);
		} catch (Exception e) {
			logger.info("日期转换出错。");
			e.printStackTrace();
		}
		
		Map<String, String> map = new HashMap<String, String>(); 
		
		SysLog sysLog = new SysLog();
		sysLog.setLogid(logid);
		sysLog.setLoginfo(loginfo);
		sysLog.setLogtype(CommUtil.parseInt(logtype));
		sysLog.setOperatorid(operatorid);
		sysLog.setCode(code);
		sysLog.setLogtime(date);
		
		int result = 0;
		try{
			result = sysLogServiceImpl.updateByPrimaryKeySelective(sysLog);
		}catch(Exception e){
			   map.put("success", "false");
			   map.put("errorMsg", "操作失败!");  
		}
		
		if(result > 0){
			   map.put("success", "true");
		}else{
			   map.put("success", "false");
			   map.put("errorMsg", "操作失败!");  
		}
		return map;
	}
	
	/**
	 * 删除日志
	 * @param request
	 * @return
	 */
	@ResponseBody 
	@RequestMapping("/delete")
	public Map<String, String> delete(HttpServletRequest request){
		logger.debug("delete");
		String logid = request.getParameter("logid");
		
		Map<String, String> map = new HashMap<String, String>(); 
		
		
		int result = 0;
		try{
			result = sysLogServiceImpl.deleteByPrimaryKey(logid);
		}catch(Exception e){
			   map.put("success", "false");
		}
		
		if(result > 0){
			   map.put("success", "true");
		}else{
			   map.put("success", "false");
			   map.put("errorMsg", "操作失败");  
		}
		
		return map;
	}
}

这里实现添删改查的功能,关于spring mvc,我这里不做详细介绍了,只是简要的说明下需要注意到地方。

easyui 1.5+spring 4.2+mybatis 3.4 Web示例(六) 单表操作之Controller层

syslog是访问路径

easyui 1.5+spring 4.2+mybatis 3.4 Web示例(六) 单表操作之Controller层

这个是跳转到某个页面

easyui 1.5+spring 4.2+mybatis 3.4 Web示例(六) 单表操作之Controller层

这是返回一个对象,一般用于ajax应用。


CommUtil.parseInt方法

/** 
     * 字符串转换为数字
     * @return 
     */  
    public static int parseInt(String str) {  
		int result = 0;
		if(str != null && !"".equals(str)){
			result = Integer.parseInt(str.trim());
		}
		return result;
	}  
DateUtil.convertStringToDate方法

/** 
     * 将日期字符串按指定格式转换成日期类型 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @param aMask 指定的日期格式,如:yyyy-MM-dd 
     * @param strDate 待转换的日期字符串 
     * @return 
     * @throws ParseException 
     */  
    public static final Date convertStringToDate(String aMask, String strDate)  
            throws ParseException {  
        SimpleDateFormat df = null;  
        Date date = new Date();  
        df = new SimpleDateFormat(aMask);  
  
        if (logger.isDebugEnabled()) {  
            logger.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");  
        }  
        try {  
            df.format(date);
        } catch (Exception pe) {  
            logger.error("Exception: " + pe);  
            try {
				throw pe;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
        }  
        return (date);  
    }  

public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";