八,项目实训实现步骤
(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 ;
public class StudentDaoImpl implements StudentDao {
@Override
public int insert ( Student student) {
int count= 0 ;
Connection conn = ConnectionManager . getConnection ( ) ;
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. setString ( 5 , student. getDepartment ( ) ) ;
pstmt. setString ( 6 , student. getClazz ( ) ) ;
pstmt. setString ( 7 , student. getTelephone ( ) ) ;
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) ;
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 ( 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 ( 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 ;
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;
}
@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;
}
@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;
}
@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;
}
@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()
@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!更新失败!!!" ) ;
}
}