撰写TextStudentDaoImpl
package qingjiabo.student.text;
import org.junit.Test;
import qingjiabo.student.bean.Student;
import qingjiabo.student.dao.StudentDao;
import qingjiabo.student.dao.impl.StudentDaoImpl;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class TextStudentDaoImpl {
@Test
public void testInser(){
// 创建学生对象
Student student = new Student()
; //设置学生对象属性
student.setId(“18101001”);
student.setClassname(“卿加波”);
student.setSex(“男”);
student.setAge(20);
student.setDepartment(“软件技术2班”);
student.setTelephone(“15881740794”);
StudentDao dao = new StudentDaoImpl();
int count = dao.insert(student);
if (count >0){
System.out.println("恭喜!插入成功!");
}else {
System.out.println("遗憾,插入失败!");
}
}
@Test
public void testDeleteById(){
StudentDao dao = new StudentDaoImpl();
String id = "18101001";
int count = dao.deleteById(id);
if (count > 0){
System.out.println("恭喜,学生记录删除成功!");
}else {
System.out.println("遗憾,学生记录删除失败");
}
}
@Test
public void testDeleteByClass(){
StudentDao dao = new StudentDaoImpl();
String classname = "11英教1班";
//调用学生数据访问对象的按班级删除的方法
int count = dao.deleteByClass(classname);
if (count > 0 ){
System.out.println("恭喜,["+ classname+"]学生记录更新成功!");
}else {
System.out.println("遗憾,["+classname+"学生记录更新失败!]");
}
}
@Test
public void testFindByNama(){
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 testFindByNama(){
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 textFindAll(){
List<Student> students = dao.findAll();
for (Student student : students){
System.out.println(student);
}
}
// @Test
// public void textFindRowsBySex(){
// Vector rows = dao.findRowsBySex();
// for (object row : rows){
// System.out.println(rows);
// }
// }
@Test
public void textFindRowsBySex(){
//调用学生数据访问对象的按性别统计人数的方法
Vector rows = dao.findRowsBySex();
//获取向量迭代器
Iterator iterator = rows.iterator();
//遍历迭代器
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
解决textStudentDaoImpl测试类的报错
在UserDao接口中添加防止重名用户属性
但随即UserDaoIpml报错
添加抽象方法isUsernameExisted后
报错消失
撰写TextUserDaoImpl
package qingjiabo.student.text;
import org.junit.Test;
import qingjiabo.student.bean.User;
import qingjiabo.student.dao.UserDao;
import qingjiabo.student.dao.impl.UserDaoImpl;
import java.util.Date;
public class TextUserDaoImpl {
//定义数据访问对象
UserDao dao = new UserDaoImpl();
@Test
public void textFindById(){
//调用用户数据访问对象的查找方法
User user =dao.findById(1);
//输出用户对象
System.out.println(user);
}
@Test
public void testLogin(){
String username ,password;
username = "admin";
password = "12345";
//调用用户数据访问对象登录方法
User user = dao.login(username, password);
//判断是否登录成功
if (user != null){
System.out.println("恭喜!用户与密码正确,登录成功!");
}else {
System.out.println("遗憾,用户与密码错误,登录失败!");
}
}
@Test
public void testInsert(){
//定义用户对象
User user = new User();
user.setUsername("吴彦祖");
user.setPassword("34567");
user.setTelephone("15823234590");
user.setRegisterTime(new Date());
//调用数据访问对象的插入方法
int count = dao.insert(user);
//判断
if (count >0){
System.out.println("恭喜,用户记录插入成功!");
System.out.println(dao.findById(dao.findAll().size()));
}else {
System.out.println("遗憾,用户记录插入失败!");
}
}
@Test
public void testIsUsernameExisted(){
String username ="李刚";
boolean result = dao.isUsernameExisted(username);
if (result){
System.out.println("温馨提示,["+username+"]已存在,不可用此名注册!");
}else {
System.out.println("温馨提示,["+username+"]不存在,可用此名注册!");
}
}
}
编写服务类接口
建包impl 撰写collegeServiceImpl
在qingjiabo.student.test包里创建测试类TestCollegeServiceImpl:
今天的文章java实训内容_java实训心得分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/67913.html