package com.crm.model;
import java.io.Reader;
import java.util.Iterator;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
/**
*最简单的形式!
*@authorq
*
*/
publicclass UserTest {
/**
*@paramargs
*/
publicstaticvoid main(String[] args) throws Exception{
//从配置文件中得到SqlMapClient对象
Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
SqlMapClient sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
//创建用户数据
for(int i=0; i<10; i++){
User user = new User();
user.setUsername("用户"+i);
user.setPassword("密码"+i);
sqlMapper.insert("insertUser", user);
}
//查询用户数据
List users = sqlMapper.queryForList("selectAllUsers");
for (Iterator iter = users.iterator(); iter.hasNext();) {
User user = (User) iter.next();
System.out.println(user.getUsername());
}
//查询特定用户的数据
User user = (User)sqlMapper.queryForObject("selectUser", 1);
System.out.println("用户【id="+1+"】的名称是:"+user.getUsername());
//更新用户的信息
user = new User();
user.setId(3);
user.setUsername("更改之后的用户名称");
user.setPassword("密码被更改");
sqlMapper.update("updateUser", user);
//删除用户的信息
sqlMapper.delete("deleteUser", 6);
}
}
|