zoukankan      html  css  js  c++  java
  • DAO层设计Junit测试

    DAO层的设计:

      在实际的开发中有一种项目的程序组织架构方案叫做MVC模式。

    MVC模式就是按照程序的功能将它们分成三层,分别是Modle层

    (模型层)、View(显示层)、Controller(控制层)。

      Modle层:Modle层又可以细分为两层,分别是dao层、service层。

    这两层的主要功能如下:

      service层:主要负责一些业务处理,比如取得连接、关闭数据库连接、事

    务回滚或者一些复杂的逻辑业务处理放到 service 。

    代码示例如下:

    package com.gojong.scc.service;
    
    import com.gojong.scc.vo.Emp;
    
    public interface IEmpService {
        /**
         * 实现数据的增加 调用dao层的insert() 方法
         * 
         * @param vo 包含了要插入的数据的vo对象
         * @return 成功返回 true 否则返回 false
         */
        public boolean addEmp(Emp vo) throws Exception;
    
        /**
         * 根据编号删除数据 调用dao层的 deleteById()方法
         * 
         * @param id 要删除的数据的编号
         * @return 成功返回 true 否则返回 false
         */
        public boolean removeEmpById(Integer id) throws Exception;
    
        /**
         * 修改数 调用dao层的 update()方法
         * 
         * @param vo 保存了要修改的数据的vo对象
         * @return 成功返回 true 否则返回 false
         */
        public boolean editEmp(Emp vo) throws Exception;
    
        /**
         * 根据编号查询的数据的编号
         * 
         * @param id 要查询的数据的编号
         * @return 有数据返回 Emp 对象 否则返回null
         */
        public Emp findEmpById(Integer id) throws Exception;
    
        /**
         * 实现模糊分页查询,调用的dao层的方法
         * <li>调用 selectSplitAll(),取得的 雇员信息的集合</li>
         * <li>调用 selectCount(),取得查询到的数据量
         * 
         * @param kw 模糊查询关键字
         * @param cp 当前页
         * @param ls 每页显示的数据量
         * @return 保存雇员集合与数据量的Map对象
         */
        public java.util.Map<String, Object> findAllSplit(String kw, Integer cp, Integer ls) throws Exception;
    }

      dao层:负责访问数据库进行数据的操作,取得结果集,之后将结果集中的数据取出封装到VO类对象之后返回给service 层。dao层需要自己的接口是为了了解耦合

    示例代码如下:

    package com.gojong.scc.dao.impl;
    
    import java.sql.Connection;
    import java.util.List;
    import java.util.Set;
    
    import com.gojong.scc.dao.IEmpDao;
    import com.gojong.scc.util.DBUtil;
    import com.gojong.scc.vo.Emp;
    
    public class EmpDaoImpl implements IEmpDao {
        private Connection conn;
    
        public EmpDaoImpl() {
    
        }
    
        public EmpDaoImpl(Connection conn) {
            this.conn = conn;
        }
    
        @Override
        public int selectCount(String kw) throws Exception {
            String sql = "SELECT COUNT(*)" + "FROM emp" + " WHERE ename LIKE ?";
            return DBUtil.selectCount(conn, sql, "%" + kw + "%");
        }
    
        @Override
        public int deleteBatch(Set<Integer> ids) throws Exception {
            StringBuffer sql = new StringBuffer("DELETE FROM emp WHERE empno IN(");
            return DBUtil.remove(conn, sql, ids);
    
        }
    
        @Override
        public int insert(Emp vo) throws Exception {
            // 准备sql 语句
            String sql = "INSERT INTO emp(ename,job,sal,comm,mgr,hiredate,deptno,empno) VALUES(?,?,?,?,?,?,?,?)";
            return DBUtil.save(conn, sql, vo, false);
        }
    
        @Override
        public int deleteById(Integer empno) throws Exception {
            // 准备sql 语句
            String sql = "DELETE FROM emp WHERE empno=?";
            return DBUtil.edit(conn, sql, empno);
        }
    
        @Override
        public int update(Emp vo) throws Exception {
            // 准备sql 语句
            String sql = " UPDATE emp SET ename=?,job=?,sal=?,comm=?,mgr=?,hiredate=?,deptno=? WHERE empno=?";
            return DBUtil.edit(conn, sql, vo);
        }
    
        @Override
        public Emp selectById(Integer id) throws Exception {
            String sql = "SELECT empno,ename,job,sal,hiredate,mgr,comm,deptno" + " FROM emp" + "WHERE empno=?";
            return DBUtil.selectOne(conn, sql, Emp.class, id);
        }
    
        @Override
        public List<Emp> selectSplitAll(String kw, Integer cp, Integer ls) throws Exception {
            String sql = " SELECT empno,ename,job,sal,hiredate,mgr,comm,deptno" + " FROM emp"
                    + " WHERE ename LIKE ? LIMIT ?,?";
            return DBUtil.selectList(conn, sql, Emp.class,"%"+kw+"%" , (cp-1)*ls, ls);
        }
    
    }

    dao层需要自己的接口是为了了解耦合

      Controller层:叫做控制层,主要的功能是处理用户发送的请求。

    示例代码如下:

    package com.gojong.scc.controller;
    
    import java.sql.Connection;
    
    import com.alibaba.druid.pool.DruidDataSource;
    
    public class Druidcontroller {
        //实例化一个数据源对象
        private static DruidDataSource dataSource=new DruidDataSource();
        static {
            //配置连接池
        dataSource.setUrl("jdbc:mysql://localhost:3306/deom?useSSL=true&useUnicode=true&characterEncoding=UTF-8");
        //用户名
        dataSource.setUsername("root");
        //密码
        dataSource.setPassword("1234");
        //驱动地址
        dataSource.setDriverClassName("com.mysql.Driver");
        //初始化连接池大小
        dataSource.setInitialSize(15);
        //连接池最大使用连接数量
        dataSource.setMaxActive(25);
        //获取连接最大等待时间
        dataSource.setMaxWait(3000);
        
        }
        /**
         * 此时的连接是从连接池中获取
         * @return
         */
        public static Connection getConnection() {
            try {
                return dataSource.getConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
            
        }
        /**
         * 关闭连接的方法
         * @param conn 要关闭的连接
         */
        public static void close(Connection conn) {
            if (conn!= null) {
                try {
                    //会将间接回收到连接池中
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            
        }
        
    }

      View层:叫做显示层,主要是负责事项数据。

    示例代码如下:

    package com.gojong.scc.vo;
    
    import java.io.Serializable;
    import java.util.Date;
    
    public class Emp implements Serializable{
        private Integer empno;
        private String ename;
        private String job;
        private Integer mgr;
        private Date hiredate;
        private Double sal;
        private Double comm;
        private Integer deptno;
        
        
        public Double getComm() {
            return comm;
        }
        public Integer getDeptno() {
            return deptno;
        }
        public Integer getEmpno() {
            return empno;
        }
        public String getEname() {
            return ename;
        }
        public Date getHiredate() {
            return hiredate;
        }
        public String getJob() {
            return job;
        }
        public Integer getMgr() {
            return mgr;
        }
        public Double getSal() {
            return sal;
        }
        public void setComm(Double comm) {
            this.comm = comm;
        }
        public void setDeptno(Integer deptno) {
            this.deptno = deptno;
        }
        public void setEmpno(Integer empno) {
            this.empno = empno;
        }
        public void setEname(String ename) {
            this.ename = ename;
        }
        public void setHiredate(Date hiredate) {
            this.hiredate = hiredate;
        }
        public void setJob(String job) {
            this.job = job;
        }
        public void setMgr(Integer mgr) {
            this.mgr = mgr;
        }
        public void setSal(Double sal) {
            this.sal = sal;
        }
        
        public Emp() {
            super();
        }
        public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, Double sal, Double comm,
                Integer deptno) {
            super();
            this.empno = empno;
            this.ename = ename;
            this.job = job;
            this.mgr = mgr;
            this.hiredate = hiredate;
            this.sal = sal;
            this.comm = comm;
            this.deptno = deptno;
        }
        
        @Override
        public String toString() {
            return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate
                    + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
        }
        
    }

    Junit测试:

      Junit测试又叫做单元测试,Junit测试的好处是能进行批量测试,而且如果

    方法出现了问题能立刻定位出出现问题的方法,还有一个好处就是感官效果

    很好,如果所有方法都通过了则显示绿条(Green bar),否则显示红条(Red bar)。

    package com.gojong.scc.controller;
    
    import java.sql.Connection;
    
    import com.alibaba.druid.pool.DruidDataSource;
    
    public class Druidcontroller {
        // 实例化一个数据源对象
        private static DruidDataSource dataSource = new DruidDataSource();
        static {
            // 配置连接池
            dataSource.setUrl("jdbc:mysql://localhost:3306/deom?useSSL=true&useUnicode=true&characterEncoding=UTF-8");
            // 用户名
            dataSource.setUsername("root");
            // 密码
            dataSource.setPassword("1234");
            // 驱动地址
            dataSource.setDriverClassName("com.mysql.Driver");
            // 初始化连接池大小
            dataSource.setInitialSize(15);
            // 连接池最大使用连接数量
            dataSource.setMaxActive(25);
            // 获取连接最大等待时间
            dataSource.setMaxWait(3000);
    
        }
    
        /**
         * 此时的连接是从连接池中获取
         * 
         * @return
         */
        public static Connection getConnection() {
            try {
                return dataSource.getConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
    
        }
    
        /**
         * 关闭连接的方法
         * 
         * @param conn 要关闭的连接
         */
        public static void close(Connection conn) {
            if (conn != null) {
                try {
                    // 会将间接回收到连接池中
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
        }
    
    }
  • 相关阅读:
    [atARC123F]Insert Addition
    3.1 概述
    2.5 信道的极限容量
    2.4 编码与调制
    2.3 传输方式
    tp6_004路由配置
    tp6_003多应用配置
    tp6_002规范和配置
    tp6_001安装和运行
    问题解决:tp6多应用无法获取controller 和 action怎么办
  • 原文地址:https://www.cnblogs.com/qinchangchuan/p/10732164.html
Copyright © 2011-2022 走看看