一,测试对User表的增删查改
1、 新建测试类tesUser.java,代码如下:
package first;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import vo.Users;
import dao.IUserDao;
import dao.impl.UserDao;
public class testUser {
ApplicationContext ctx;
IUserDao userdao;
public testUser(){
ctx= new ClassPathXmlApplicationContext( "applicationContext.xml" );
userdao=(UserDao)ctx.getBean( "userDao" );
}
public void listUsers(){
List users= this .userdao.allUsers();
if (users.size()!= 0 ){
System.out.println( "User表的所有用户:" );
Users user;
for ( int i= 0 ;i<users.size();i++){
user=(Users)users.get(i);
System.out.println(user.getUserName());
}
}
}
public Users getUserByName(String name){
List us= this .userdao.selectUserByName(name);
Users u=(Users)us.get( 0 );
return u;
}
public static void main(String[] args) {
testUser ts= new testUser();
Users user= new Users();
//添加用户
System.out.println( "添加用户sun:" );
user.setUserName( "sun" );
user.setUserPasswd( "123" );
user.setGender( "w" );
user.setTel( 123 );
System.out.println( "未添加用户之前" );
ts.listUsers();
System.out.println( "添加用户之后" );
ts.userdao.addUser(user);
ts.listUsers();
查询用户、删除用户
System.out.println();
System.out.println( "查询用户、删除用户:" );
System.out.println( "现在查找用户名为yajin的记录" );
Users yajin=ts.getUserByName( "yajin" );
System.out.println( "yajin 的id为" +yajin.getUserId());
System.out.println( "现在删除这条记录,删除之后:" );
ts.userdao.deleteUser(yajin.getUserId());
ts.listUsers();
//修改用户密码
System.out.println();
System.out.println();
System.out.println( "修改用户密码:" );
Users haha=ts.getUserByName( "haha" );
System.out.println( "haha 的密码为" +haha.getUserPasswd());
System.out.println( "现在修改为000,修改之后:" );
ts.userdao.UpdateUserPasswd(haha.getUserId(), "000" );
haha=ts.getUserByName( "haha" );
System.out.println( "haha 的密码为" +haha.getUserPasswd());
}
}
2、 运行测试类得到如下结果,表明User表的增删查改无问题。
二、商品表增删查改测试
1、 新建测试类testGoods.java,代码如下:
package first;
import vo.Goods;
import java.util.List;
import dao.IGoodsDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testGoods {
ApplicationContext ac;
IGoodsDao goodsdao;
public testGoods(){
ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
goodsdao=(IGoodsDao)ac.getBean("goodsDao");
}
public void listGoods(){
List goods=this.goodsdao.allGoods();
if(goods.size()!=0){
System.out.println("Goods表的所有商品:");
Goods gd;
for(int i=0;i<goods.size();i++){
gd=(Goods)goods.get(i);
System.out.println("ID:"+gd.getGoodsId()+",商品名:"+gd.getGoodsName());
}
}
}
public static void main(String[] args) {
testGoods tg=new testGoods();
//添加商品
System.out.println("添加商品巧克力:");
Goods gd=new Goods("巧克力",12,1,32);
System.out.println("未添加商品之前:");
tg.listGoods();
tg.goodsdao.addGoods(gd);
System.out.println("添加商品之后:");
tg.listGoods();
//删除商品
System.out.println("删除商品:");
tg.goodsdao.deleteGoods(2);
System.out.println("删除商品ID为2的商品之后:");
tg.listGoods();
//查询并修改商品库存数
Goods goods=tg.goodsdao.getGoodsQty(2);
System.out.println("查询商品ID为2的商品库存:"+goods.getInventory());
System.out.println("修改库存数为100");
tg.goodsdao.updateGoodsQty(2,100);
goods=tg.goodsdao.getGoodsQty(2);
System.out.println("修改后商品库存为:"+goods.getInventory());
}
}
2、 运行测试类,结果如下,表面对表Goods的增删查改操作无问题。
三、Type表增删查改测试
1、 新建测试类testType.java,代码如下:
package first;
import java.util.List;
import dao.ITypeDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import vo.Type;
public class testType {
ApplicationContext ac;
ITypeDao typedao;
public testType(){
ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
typedao=(ITypeDao)ac.getBean("typeDao");
}
2、 运行测试类,结果如下,表明Type的增删查改操作无问题。