0x03 测试
1. 增删改查测试类编写
package com.shaonaiyi.dao; import com.shaonaiyi.domain.Student; import org.junit.Test; import java.util.List; /** * @Auther: shaonaiyi@163.com * @Date: 2021/1/13 15:34 * @Description: Student访问接口实现测试类 */ public class StudentDAOImplTest { @Test public void testQuery() { StudentDAO studentDAO = new StudentDAOImpl(); List<Student> studentList = studentDAO.query(); for (Student student : studentList) { System.out.println("---------------------------"); System.out.println("学生id:" + student.getId()); System.out.println("学生name:" + student.getName()); System.out.println("学生age:" + student.getAge()); } } @Test public void testInsert() { StudentDAO studentDAO = new StudentDAOImpl(); Student student = new Student(); student.setName("邵奈二"); student.setAge(18); Integer result = studentDAO.save(student); System.out.println("结果:" + result); } @Test public void testDelete() { StudentDAO studentDAO = new StudentDAOImpl(); Integer result = studentDAO.delete(2); System.out.println("结果:" + result); } @Test public void testUpdate() { StudentDAO studentDAO = new StudentDAOImpl(); Student student = new Student(); student.setName("邵奈一"); student.setAge(32); Integer result = studentDAO.update(student); System.out.println("结果:" + result); } // @Test // public void testSQL() { // Student student = new Student(); // student.setName("邵奈一"); // student.setAge(32); // String sql = "update student set age = " + student.getAge() + " where name = '" + student.getName() + "'"; // System.out.println(sql); // // } }
温馨提示:为了方便,这里把SQL语句直接写死在了参数
里,其实可以赋值给一个字符串,增加代码的可读性,并且方便打印出来看看,免得自己在拼接SQL语句的时候拼接错误。
当然,如果担心拼接错误,完全可以以下代码,通过设置坐标传值进去:
preparedStatement = connection.prepareStatement("insert into student(name,age) values (?,?)"); preparedStatement.setString(1, student.getName()); preparedStatement.setInt(2,student.getAge());
2. 测试结果
1、查
2、增
3、删
4、改
0xFF 总结
- 请关注本博客,以后提供更多的教程,谢谢。