zoukankan      html  css  js  c++  java
  • JavaWeb学习之Junit和Dao模式

    一、使用单元测试,测试代码
    1. 定义一个类, TestXXX , 里面定义方法 testXXX.
    public class TestUserInfoDaoImpl {
        @Test
        void testFindAll() {
    
            IUserInfoDao userInfoDao=new UserInfoDaoImpl();
            userInfoDao.findAll();
        }
    }

    2. 添加junit的支持。   

    ①、右键工程 --- add Library --- Junit --- JunitX

    ②、右键工程 => Build Path => Configure Build Path ...

      

    3. 在方法的上面加上注解 , 其实就是一个标记。   
        @Test
        void testFindAll() {
    
            IUserInfoDao userInfoDao=new UserInfoDaoImpl();
            userInfoDao.findAll();
        }

    4. 光标选中方法名字,然后右键执行单元测试。  或者是打开outline视图, 然后选择方法右键执行。

    二、Dao模式

      Data Access Object 数据访问对象

      面向接口编程有两个主要步骤

        1. 新建一个dao的接口, 里面声明数据库访问规则

        2. 新建一个dao的实现类,具体实现早前定义的规则

      具体结构如图:

         

         IUserInfoDao代码

    public interface IUserInfoDao {
        void findAll();
    }

        UserInfoDaoImpl代码

    public class UserInfoDaoImpl implements IUserInfoDao {
        @Override
        public void findAll() {
        Connection conn=null;
        Statement st=null;
        ResultSet rs=null;
        try {
            conn=JDBCUtil.getConnection();
            st=conn.createStatement();
            String sql="Select * from UserInfo";
            rs=st.executeQuery(sql);
            while(rs.next()) {
            System.out.println(rs.getInt("ID"));
            System.out.println(rs.getString("UName"));
            System.out.println(rs.getBoolean("DelFlag"));
            }
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        }
    }
  • 相关阅读:
    1451. Rearrange Words in a Sentence
    1450. Number of Students Doing Homework at a Given Time
    1452. People Whose List of Favorite Companies Is Not a Subset of Another List
    1447. Simplified Fractions
    1446. Consecutive Characters
    1448. Count Good Nodes in Binary Tree
    709. To Lower Case
    211. Add and Search Word
    918. Maximum Sum Circular Subarray
    lua 时间戳和时间互转
  • 原文地址:https://www.cnblogs.com/WarBlog/p/12560146.html
Copyright © 2011-2022 走看看