java实训日志6

八,项目实训实现步骤

(7)实现数据接口访问

  • 学生数据访问接口实现类StudentDaoImpl
package net.zhao.student.dao.impl;

import net.zhao.student.bean.Student;
import net.zhao.student.dao.StudentDao;
import net.zhao.student.dbutil.ConnectionManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * 功能:学生数据访问接口实现类
 * 作者:zhao
 * 日期:2023年06月2023/6/14日
 */
public class StudentDaoImpl implements StudentDao {
    /**
     * 插入学生信息
     *
     * @param student
     * @return
     */
    @Override
    public int insert(Student student) {
        //定义插入数据
        int count=0;
        Connection conn = ConnectionManager.getConnection();
        //定义SQL字符串


        String strSQL="insert into t_student (id,name,sex,age,department,class,telephone)"
        +",values(?,?,?,?,?,?,?)";
        try {
            PreparedStatement pstmt =conn.prepareStatement(strSQL);
            //设置占位符
            pstmt.setString(1,student.getId());
            pstmt.setString(2,student.getName());
            pstmt.setString(3,student.getSex());
//            pstmt.setInt(4,student.getAge());
            pstmt.setString(5,student.getDepartment());
            pstmt.setString(6,student.getClazz());
            pstmt.setString(7,student.getTelephone());
            //执行SQL返回预备语句对象
            count = pstmt.executeUpdate();
            //关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();

        } finally {
            ConnectionManager.closeConnection(conn);
        }

        return count;
    }

    @Override
    public int deleteById(String id) {
        int count = 0;
        //获取数据链接
        Connection conn = ConnectionManager.getConnection();
        String strSQL="delete from t_student where id =?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            //设置占位符
            pstmt.setString(1,id);
            //执行SQL,返回删除记录
            count=pstmt.executeUpdate();
            //关闭数据库链接
            pstmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        return count;
    }

    @Override
    public int deleteByClass(String clazz) {
        int count =0;
        Connection conn =ConnectionManager.getConnection();
        String strSQL="delect from t_student where class=?";
        try {
            PreparedStatement pstmt =conn.prepareStatement(strSQL);
            pstmt.setString(1,clazz);
            count= pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        return count;
    }

    @Override
    public int deleteByDepartment(String department) {
        int count=0;
        Connection conn=ConnectionManager.getConnection();
        String strSQL="delete  form t_student where department=?";
        try {
            PreparedStatement pstmt =conn.prepareStatement(strSQL);
            pstmt.setString(1,department);
            count=pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }
        return count;
    }

    @Override
    public int update(Student student) {
        int count=0;
        Connection conn=ConnectionManager.getConnection();
        String strSQL="update t_student set name=?,sex=?,age=?,department=?,class=?,telephone=? where id=?";
        try {
            PreparedStatement pstem=conn.prepareStatement(strSQL);
            pstem.setString(1,student.getName());
            pstem.setString(2,student.getSex());
            pstem.setString(3,student.getAge());
            pstem.setString(4,student.getDepartment());
            pstem.setString(5,student.getClazz());
            pstem.setString(6,student.getTelephone());
            pstem.setString(7,student.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }
        return count;
    }

    @Override
    public Student findById(String id) {
        Student student=null;
        Connection conn =ConnectionManager.getConnection();
        String strSQL="select*from t_student where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(strSQL);
            pstmt.setString(1,id);
            ResultSet rs =pstmt.executeQuery();
            if(rs.next()) {
                student=new Student();
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getString("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("clazz"));
                student.setTelephone(rs.getString("telephone"));
                
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        return student;
    }

    @Override
    public List<Student> findByName(String name) {
        List<Student> students=new ArrayList< >();
        Connection conn=ConnectionManager.getConnection();
        String strSQL="select*from t_student where name like?";
        try {
            PreparedStatement pstmt =conn.prepareStatement(strSQL);
            pstmt.setString(1,name+"%");
            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                Student student=new Student();
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getString("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("clazz"));
                student.setTelephone(rs.getString("telephone"));
                students.add(student);
            }
            rs.close();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        return students;
    }

    @Override
    public List<Student> findByClass(String clazz) {
        List<Student> students=new ArrayList< >();
        Connection conn=ConnectionManager.getConnection();
        String strSQL="select*from t_student where class like?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(strSQL);
            pstmt.setString(1,clazz+"%");
            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                Student student=new Student();
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getString("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("clazz"));
                student.setTelephone(rs.getString("telephone"));
                students.add(student);
            }
            rs.close();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }
        return students;
    }

    @Override
    public List<Student> findByDepartment(String department) {
        List<Student> students=new ArrayList< >();
        Connection conn=ConnectionManager.getConnection();
        String strSQL="select*from t_student where department like?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(strSQL);
            pstmt.setString(1,department+"%");
            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                Student student=new Student();
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getString("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("clazz"));
                student.setTelephone(rs.getString("telephone"));
                students.add(student);
            }
            rs.close();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        return students;
    }

    @Override
    public List<Student> findAll() {
        List<Student> students=new ArrayList< >();
        Connection conn=ConnectionManager.getConnection();
        String strSQL="select*from t_student ";
        try {
            Statement stmt=conn.createStatement();
            ResultSet rs =stmt.executeQuery(strSQL);
            while (rs.next()){
                Student student=new Student();
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getString("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("clazz"));
                student.setTelephone(rs.getString("telephone"));
                students.add(student);
            }
            rs.close();
            stmt.close();
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }
        return students;
    }

    @Override
    public Vector findRowsBySex() {
        Vector rows=new Vector();
        Connection conn=ConnectionManager.getConnection();
        String strSQL="select sex as '性别',count(*) as '人数' "
        +"from t_student group by sex order by sex desc";
        try {
            Statement stmt=conn.createStatement();
            ResultSet rs =stmt.executeQuery(strSQL);
            while (rs.next()){
                Vector<String> currentRow =new Vector();
                currentRow.addElement(rs.getString("性别"));
                currentRow.addElement(rs.getInt("人数")+"");
                rows.addElement(currentRow);
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }


        return rows;
    }

    @Override
    public Vector findRowsByClass() {
        Vector rows=new Vector();
        Connection conn=ConnectionManager.getConnection();
        String strSQL="select  class '班级',count(*) as '人数' "
                +"from t_student group by class order by class desc";
        try {
            Statement stmt=conn.createStatement();
            ResultSet rs =stmt.executeQuery(strSQL);
            while (rs.next()){
                Vector<String> currentRow =new Vector();
                currentRow.addElement(rs.getString("班级"));
                currentRow.addElement(rs.getInt("人数")+"");
                rows.addElement(currentRow);
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        return rows;
    }

    @Override
    public Vector findRowsByDepartment() {
        Vector rows=new Vector();
        Connection conn=ConnectionManager.getConnection();
        String strSQL="select  department '系部',count(*) as '人数' "
                +"from t_student group by department order by department desc";
        try {
            Statement stmt=conn.createStatement();
            ResultSet rs =stmt.executeQuery(strSQL);
            while (rs.next()){
                Vector<String> currentRow =new Vector();
                currentRow.addElement(rs.getString("系部"));
                currentRow.addElement(rs.getInt("人数")+"");
                rows.addElement(currentRow);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }
        return rows;
    }
}

对StudentDaoImpl进行单元测试

  • 在net.zhao.student.bean包的子包dao.impl里创建测试类TestStudentDaoImpl
    在这里插入图片描述
(1)编写测试方法testInsert()
public class TestStudentDaoImpl {
    StudentDao dao = new StudentDaoImpl();
    @Test
    public void testInsert(){
        Student student=new Student();
        student.setId("19242094");
        student.setName("张晓惠");
        student.setSex("女");
        student.setAge("19");
        student.setDepartment("小教学院");
        student.setClazz("2022文学3班");
        student.setTelephone("12234567890");
        //调用学生数据访问对象的插入方法
        int count =dao.insert(student);
        //判断学生记录是否插入成功
        if(count>0){
            System.out.println("恭喜数据插入成功");
            System.out.println(dao.findById(student.getId()));
        }else {
            System.out.println("遗憾,学生数据插入失败");

        }
    }
}

  • testInsert()运行
    在这里插入图片描述
(2)编写测试方法testDeleteById()
 @Test
    public void testDeleteById(){
        String id="192420101";
        int count =dao.deleteById(id);
        if(count>0){
            System.out.println("恭喜,学生记录删除成功");
        }else {
            System.out.println("遗憾学生信息删除信息失败");
        }
    }
  • 运行结果在这里插入图片描述
(3)编写测试方法testDeleteByClass()
 @Test
    public void testDeleteByClass(){
        String clazz ="2022计应3班";
        int count =dao.deleteByClass(clazz);
        if (count>0){
            System.out.println("恭喜,["+clazz+"]学生记录删除成功");
            
        }else {
            System.out.println("遗憾,["+clazz+"]学生记录删除失败");
        }
(4) 编写测试方法testFindByName()
  • 代码块
@Test
    public void testFindByName(){
        String name ="张三丰";
        List<Student> students=dao.findByName(name);
        if (students.size()>0){
            for (Student student:students){
                System.out.println("student");
            }
        }else {
            System.out.println("温馨提示查无此人");
        }
    }
  • 测试结果
    在这里插入图片描述
  • 修改待查学生的姓名
@Test
    public void testFindByName(){
        String name ="李玉山";
        List<Student> students=dao.findByName(name);
        if (students.size()>0){
            for (Student student:students){
                System.out.println("student");
            }
        }else {
            System.out.println("温馨提示查无此人");
        }
    }

}
  • 查找所有姓“李”的学生记录,修改查找目标
@Test
    public void testFindByName(){
        String name ="李";
        List<Student> students=dao.findByName(name);
        if (students.size()>0){
            for (Student student:students){
                System.out.println("student");
            }
        }else {
            System.out.println("温馨提示查无此人");
        }
    }

}
(5)编写测试方法testFindAll()
  • 代码块
@Test
    public void testFindAll(){
        List<Student >students=dao.findAll();
        for (Student student:students){
            System.out.println(student);
        }
    }

  • 运行结果
  • 在这里插入图片描述
(6)编写测试方法testFindRowsBySex()
  • 代码块
@Test
    public void testUpdate(){
        Student student=new Student();
        student.setName("小妹");
        student.setSex("女");
        student.setAge("18");
        student.setDepartment("教育学院");
        student.setClazz("小教1班");
        student.setTelephone("123456777774");
        student.setId("泸职院");
        int count =dao.update(student);
        if (count>0){
            System.out.println("恭喜,学生记录更新成功");
            System.out.println(dao.findById(student.getId()));

        }else {
            System.out.println("遗憾,学生记录更新失败");
        }
    }
(7)编写测试方法testDeleteByDepartment()
  • 代码块`在这里插入代码片
@Test
    public void testDeleteByDepartment(){
        String department ="人文学院";
        int count =dao.deleteByDepartment(department);
        if (count>0){
            System.out.println("恭喜,["+department+"]学生记录删除成功");

        }else {
            System.out.println("遗憾,["+department+"]学生记录删除失败");
        }
    }
(8)编写测试方法testUpdate()
  • 代码块
@Test
public void testUpdate(){
    //创建学生对象属性
    Student student = new Student();
    //设置学生对象属性
    student.setId("19232308");
    student.setName("张媛");
    student.setSex("女");
    student.setAge("19");
    student.setDepartment("人工智能与大数据学院");
    student.setClazz("22软件3班");
    student.setTelephone("15236353523");
    //调用学生数据访问对象的修改对象
    int count = dao.update(student);
    //判断是否成功
    if(count>0){
        System.out.println("成功");
        System.out.println(dao.findById(student.getId()));
    } else {
        System.out.println("遗憾,失败");
    }
}
(9)编写测试方法testFindById()
  • 代码块
@Test
    public void testFindById(){
    StudentDao dao = new StudentDaoImpl();
        String id="19204091";
        //按学号查询
        Student student=dao.findById(id);
        if(student!=null){
            System.out.println(student);

        }else {
            System.out.println("学号为:["+id+"]的学生未找到");
        }

}

}
(10)编写测试方法findRowsByDepartment()
@Test
    public void findRowsByDepartment(){
    StudentDao dao = new StudentDaoImpl();
    String department="信息工程学院";
    if(department!=null){
        System.out.println(department);

    }else {
        System.out.println("学号为:["+department+"]的学生未找到");
    }


}
  • 运行截图
    在这里插入图片描述
(11)编写测试方法findRowsByClass()
@Test
    public void findRowsByClass(){
        StudentDao dao = new StudentDaoImpl();
        String clazz="22软件三班";
        if(clazz!=null){
            System.out.println(clazz);

        }else {
            System.out.println("班级为:["+clazz+"]的学生未找到");
        }


}

在这里插入图片描述

(12)编写测试方法findByClass(String clazz)
@Test
    public void testFindByClass(){
        String clazz = "2022电商3班";
        //调用
        List<Student> students = dao.findByClass(clazz);
        //判断是否有元素
        if(students.size()>0){
            //for循环遍历
            for (Student student : students){
                System.out.println(student);
            }
        } else {
            System.out.println("温馨提示,没有此班级");
        }
    }

  • 运行结果
    在这里插入图片描述
(13)编写测试方法findByDepartment
 @Test
    public void testFindByDepartment(){
        String department = "商学院";
        //调用
        List<Student> students = dao.findByDepartment(department);
        //判断是否有元素
        if(students.size()>0){
            //for循环遍历
            for (Student student : students){
                System.out.println(student);
            }
        } else {
            System.out.println("温馨提示,没有此学院");
        }
    }


  • 运行结果

在这里插入图片描述

( 8)创建用户数据访问接口实现类

  • 创建用户数据访问接口实现类UserDaoImpl
    在这里插入图片描述
  • 代码块
package net.zhao.student.dao.impl;

import net.zhao.student.bean.User;
import net.zhao.student.dao.UserDao;
import net.zhao.student.dbutil.ConnectionManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能:用户数据访问接口
 * 作者:zhao
 * 日期:2023年06月2023/6/15日
 */
public class UserDaoImpl implements UserDao {

    @Override
    public int inser(User user) {
        int count=0;
        Connection conn= ConnectionManager.getConnection();
        String strSQL="insert into t_user (username,password,telephone,register_time)"
                +"values(?,?,?,?)";
        if(!isUsernameExisted(user.getUsername())){
            try {
                PreparedStatement pstmt=conn.prepareStatement(strSQL);
                pstmt.setString(1,user.getUsername());
                pstmt.setString(2,user.getPassword());
                pstmt.setString(3,user.getTelephone());
                pstmt.setTimestamp(4,new Timestamp(user.getRegisterTime().getTime()));
                count=pstmt.executeUpdate();
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                ConnectionManager.closeConnection(conn);
            }
        }
        return count;
    }

    /**
     * 更新用户记录
     * @param id
     * @return
     */
    @Override
    public int deleteById(int id) {
        int count =0;
        Connection conn= ConnectionManager.getConnection();
        String strSQL="delete from t_user where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(strSQL);
            pstmt.setInt(1,id);
            count =pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }


        return count;
    }

    /**
     * 插入记录数
     * @param user
     * @return
     */
    @Override
    public int update(User user) {
        int count=0;
        Connection conn =ConnectionManager.getConnection();
        String strSQL="update t_user set username=?,password=?,telephone=?,register_time=? where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(strSQL);
            pstmt.setString(1,user.getUsername());
            pstmt.setString(2,user.getPassword());
            pstmt.setString(3,user.getTelephone());
            pstmt.setTimestamp(4,new Timestamp(user.getRegisterTime().getTime()));
            pstmt.setInt(5,user.getId());
            count=pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }
        return count;
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public User findById(int id) {
        User user=null;
        Connection conn=ConnectionManager.getConnection();
        String strSQL ="select*from t_user where id=?";
        try {
            PreparedStatement pstmt= conn.prepareStatement(strSQL);
            pstmt.setInt(1,id);
            ResultSet rs=pstmt.executeQuery();
            if (rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("id"));
                user.setTelephone(rs.getString("id"));
                user.setRegisterTime(rs.getTimestamp("register_time"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {ConnectionManager.closeConnection(conn);
        }
        return user;
    }

    @Override
    public List<User> findAll() {
        List<User> users=new ArrayList<User>();
        Connection conn=ConnectionManager.getConnection();
        String strSQL ="select*from t_user ";
        try {
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery(strSQL);
            while (rs.next()){
                User user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setTelephone(rs.getString("password"));
                user.setRegisterTime(rs.getTimestamp("register_time"));
                users.add(user);
            }
            rs.close();
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }


        return users;
    }

    /**
     *
     * @param username
     * @param password
     * @return 用户登录实体
     */
    @Override
    public User login(String username, String password) {
        User user=null;
        Connection conn=ConnectionManager.getConnection();
        String strSQL ="select*from t_user where username=? and password=?";
        try {
            PreparedStatement pstmt= conn.prepareStatement(strSQL);
            pstmt.setString(1,username);
            pstmt.setString(2,password);
            ResultSet rs =pstmt.executeQuery();

            if(rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setTelephone(rs.getString("password"));
                user.setRegisterTime(rs.getTimestamp("register_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        return user;
    }

    @Override
    public boolean isUsernameExisted(String username) {
        boolean existed=false;
        Connection conn=ConnectionManager.getConnection();
        String strSQL ="select*from t_user where username=?";
        try {
            PreparedStatement pstmt= conn.prepareStatement(strSQL);
            pstmt.setString(1,username);
            ResultSet rs=pstmt.executeQuery();
            if(rs.next()){
                existed=true;

            }
            pstmt.close();
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }


        return existed;
    }
}

对UserDaoImpl进行单元测试

  • 在net.zhao.student.bean包的dao.impl里创建测试类TestUserDaoImpl
    在这里插入图片描述
(1)编写测试方法testFindById()
  • testFindById()代码
  @Test
    public void testFindById(){
        User user = dao.findById(1);
        System.out.println("用户名"+user.getUsername());
        System.out.println("密码"+user.getPassword());
        System.out.println("电话"+user.getTelephone());
        System.out.println("注册时间"+user.getRegisterTime());
    }

  • 运行结果
    在这里插入图片描述
( 2)编写测试方法testLogin()
  • 代码块
@Test
    public void testLogin(){
        String username,password;
        username="howard";
        password ="111111";
        User user=dao.login(username,password);
        if (user!=null){
            System.out.println("恭喜,用户登录成功");

        }else {
            System.out.println("遗憾登录失败");
        }
    }

  • 运行结果
    在这里插入图片描述
    在这里插入图片描述
(3)编写测试方法testIsUsernameExisted()
  • 代码块
@Test
    public void testIsUsernameExisted(){
        String username="张三丰";
        boolean result=dao.isUsernameExisted(username);
        if(result){
            System.out.println("温馨提示:["+username+"]已存在,不可再次注册");

        }else {
            System.out.println("温馨提示:["+username+"]不存在,可以注册");
        }
    }
  • 运行结果
    在这里插入图片描述
  • 修改用户名,改成用户表里有的用户名【王霞】
    在这里插入图片描述
(4)编写测试方法testInsert()
  • 代码块
@Test
    public void testInsert(){
        User user =new User();
        user.setUsername("吴彦文");
        user.setPassword("345679");
        user.setTelephone("123456754334");
        user.setRegisterTime(new Date());
        int count=dao.inser(user);
        if(count>0){
            System.out.println("恭喜用户,用户记录插入成功");
            System.out.println(dao.findById(dao.findAll().size()));
        }else {
            System.out.println("遗憾,用户信息记录插入失败");
        }
    }

(5)编写测试方法testDeleteById()
  • 代码块
 @Test
    public void testDeleteById() {
        int id = 1;
        int count = dao.deleteById(id);
        if (count != 0) {
            System.out.println("删除用户记录成功!!!");
        } else {
            System.out.println("What a pity,删除记录失败!!!");
        }
}
  • 运行及如果
    在这里插入图片描述
(6)编写测试方法testFindAll()
 @Test
    public void testFindAll() {
    List<User> users=dao.findAll();
        for (User user:users){
        System.out.println(users);
    }
}
  • 运行结果
    在这里插入图片描述
(7)编写测试方法testUpdate()
 @Test
    public void testUpdate() {
        UserDao userdao = new UserDaoImpl();
        User user = userdao.findById(1);
        System.out.println("更新前:" + user);
        user.setTelephone("11111000111");
        user.setUsername("只因");
        int count = userdao.update(user);
        if (count > 0) {
            System.out.println("更新成功!!!");
            System.out.println("更新后:" + userdao.findById(1));
        } else {
            System.out.println("What a pity!更新失败!!!");
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值