zoukankan      html  css  js  c++  java
  • baseAction,baseDao,baseService

    SSH的

    baseAction

    简单叙述下作用

    1.通用方法如增删改,获取表格,获取树等等常用方法

    2.其他action继承baseAction达到基本不写代码或少写代码,使其他action具有baseAction的方法,同时可以自己特有的方法

    上代码

    @ParentPackage("default")
    @Namespace("/")
    public class BaseAction<T> extends ActionSupport {
    
        private static final Logger logger = Logger.getLogger(BaseAction.class);
    
        
        protected int page = 1;// 当前页
        protected int rows = 10;// 每页显示记录数
        protected String sort;// 排序字段
        protected String order = "asc";// asc/desc
        protected String q;// easyui的combo和其子类过滤时使用
    
        protected String id;// 主键
        protected String ids;// 主键集合,逗号分割
        protected T data;// 数据模型(与前台表单name相同,name="data.xxx")
    
        protected BaseServiceI<T> service;// 业务逻辑
      /**
         * 获得request
         *
         * @return
         */
        public HttpServletRequest getRequest() {
            return ServletActionContext.getRequest();
        }

        /**
         * 获得response
         *
         * @return
         */
        public HttpServletResponse getResponse() {
            return ServletActionContext.getResponse();
        }

        /**
         * 获得session
         *
         * @return
         */
        public HttpSession getSession() {
            return ServletActionContext.getRequest().getSession();
        }
        /**
         * 保存一个对象
         */
        public void save() {
            Json json = new Json();
            if (data != null) {
                service.save(data);
                json.setSuccess(true);
                json.setMsg("新建成功!");
            }
            writeJson(json);
        }

        /**
         * 更新一个对象
         */
        public void update() {
            
            Json json = new Json();
            String reflectId = null;
            
            try {
                if (data != null) {
                    reflectId = (String) FieldUtils.readField(data, "id", true);
                }
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if (!StringUtils.isBlank(reflectId)) {
                try {
                    T t = service.getById(reflectId);
                    BeanUtils.copyProperties(data,t);
                    service.update(t);
                    json.setSuccess(true);
                    json.setMsg("更新成功");
                    
                } catch (Exception e) {
                    json.setSuccess(false);
                    json.setMsg("更新失败");
                    logger.info(e.toString());
                }
            }
            writeJson(json);
        }

        /**
         * 删除一个对象
         */
        public void delete() {
            Json json = new Json();
            if (!StringUtils.isBlank(id)) {
                T t = service.getById(id);
                service.delete(t);
                json.setSuccess(true);
                json.setMsg("删除成功!");
            }
            writeJson(json);
        }
    }

    将BaseActiong写成泛型 这样前台写成data.xxx就可以自动被Struts的类型转换为data

    这样对每次操作自动匹配不同的Bean类,非常方便。

    service层

    serviceI接口定义不同的方法方法

    public interface BaseServiceI<T> {
    
        /**
         * 保存一个对象
         * 
         * @param o
         *            对象
         * @return 对象的ID
         */
        public Serializable save(T o);
    }

    方法太多 写个例子就好

    serviceimpl实现类

    @Service("BaseService")
    @Transactional
    public class BaseServiceImpl<T> implements BaseServiceI<T> {
    
        @Autowired
        private BaseDaoI<T> baseDao;
    
        @Override
        public Serializable save(T o) {
            return baseDao.save(o);
        }
    }

    注入baseDao具体实现方法

    下面是BaseDao

    BaseDaoI接口定义baseDao的方法

    public interface BaseDaoI<T> {
    
        /**
         * 保存一个对象
         * 
         * @param o
         *            对象
         * @return 对象的ID
         */
        public Serializable save(T o);
    }

    方法太多不再写

    BaseDaoImpl

    @Repository("baseDao")
    public class BaseDaoImpl<T> implements BaseDaoI<T> {
    
        @Autowired
        private SessionFactory sessionFactory;
    
        /**
         * 获得当前事物的session
         * 
         * @return org.hibernate.Session
         */
        public Session getCurrentSession() {
            return sessionFactory.getCurrentSession();
        }
    
        @Override
        public Serializable save(T o) {
            if (o != null) {
                return getCurrentSession().save(o);
            }
            return null;
        }
    }

    BaseDaoimpl
    需要注入sessionFactory(方法太多不再写)

    这样所有都以泛型写通用方法,使得每个继承类同事拥有父类的基础方法,也同时拥有自己特有的方法。如果某个方法要反复使用,就写成通用方法。大量的减少了代码

  • 相关阅读:
    Java代码打成jar后 classgetClassLoadergetResource("")返回为null
    springboot-yml内list、map组合写法
    rpc-java 生成代码路径设置
    Git操作 :从一个分支cherry-pick多个commit到其他分支
    使用maven插件生成grpc所需要的Java代码
    'Failed to import pydot. You must `pip install pydot` and install graphviz
    seasonal_decompose plot figsize
    Failed to install 'TwoSampleMR' from GitHub
    prophet Building wheel for fbprophet (setup.py) ... error
    python matplotlib 绘图线条类型和颜色选择
  • 原文地址:https://www.cnblogs.com/wsy123/p/4463161.html
Copyright © 2011-2022 走看看