mysql:
//127.0.0.1/ibatis"/>
packagecom.bjgx.model;
importjava.io.IOException;
importjava.io.Reader;
importjava.sql.SQLException;
importjava.util.Iterator;
importjava.util.List;
importjunit.framework.TestCase;
importmon.resources.Resources;
importcom.ibatis.sqlmap.client.SqlMapClient;
importcom.ibatis.sqlmap.client.SqlMapClientBuilder;
publicclassUserTestextendsTestCase{
privatestaticSqlMapClientsqlMapClient=null;
privatestaticReaderreader=null;
static{
//从配置文件中得到SqmMapClient对象
try{
reader=Resources.getResourceAsReader("sqlMapConfig.xml");
sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
}catch(Exceptione){
e.printStackTrace();
}
}
publicvoidtestCreateUser()throwsException{
//创建用户数据
for(inti=0;i<10;i++){
Useruser=newUser();
user.setUsername("用户"+i);
user.setPassword("密码"+i);
Integerii=(Integer)sqlMapClient.insert("insertUser",user);
System.out.println(ii);
}
}
publicvoidtestQueryForList()throwsException{
Listusers=sqlMapClient.queryForList("selectAllUser");
for(Iteratoriterator=users.iterator();iterator.hasNext();){
Useruser=(User)iterator.next();
System.out.println(user.getUsername());
}
System.out.println("ok");
}
publicvoidtestSearchUsers()throwsException{
Listusers=sqlMapClient.queryForList("selectAllUser");
for(Iteratoriterator=users.iterator();iterator.hasNext();){
Useruser=(User)iterator.next();
System.out.println(user.getUsername());
}
System.out.println("ok");
}
publicvoidtestFindUserById()throwsException{
Useruser=(User)sqlMapClient.queryForObject("findUserById",newInteger
(2));
System.out.println(user.getUsername());
}
publicvoidtestUpdateUser()throwsException{
//更新用户的信息
Useruser=newUser();
user.setId(3);
user.setUsername("aaaaaaaaa");
user.setPassword("bbbbbbbbbb");
sqlMapClient.update("updateUser",user);
}
publicvoidtestDeleteUser()throwsException{
sqlMapClient.delete("deleteUser",3);
}
}
2parameterClass
可以使用这个属性来限制传入参数的类型
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEsqlMap
PUBLIC"-//ibatis.apache.org//DTDSQLMap2.0//EN"
"http:
//ibatis.apache.org/dtd/sql-map-2.dtd">
--Usetypealiasestoavoidtypingthefullclassnameeverytime.-->
--
定义parameterClass="User"表示限制调用这条SQL语句的时候,传入的参数类型必须是User类型的对象
-->
insertintot_uservalues(null,#username#,#password#)
--
如果没有定义parameterClass属性,则表示可以传入任何一种类型的对象,只要这个对象有username和password属性即可
-->
insertintot_uservalues(null,#username#,#password#
packagecom.bjgx.model;
publicclassJustAnObject{
privateintid;
privateStringusername;
privateStringpassword;
privateStringotherproperty;
}
packagecom.bjgx.model;
importjava.io.Reader;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Random;
importjunit.framework.TestCase;
importmon.resources.Resources;
importcom.ibatis.sqlmap.client.SqlMapClient;
importcom.ibatis.sqlmap.client.SqlMapClientBuilder;
publicclassUserTestextendsTestCase{
privatestaticSqlMapClientsqlMapClient=null;
privatestaticReaderreader=null;
static{
//从配置文件中得到SqmMapClient对象
try{
reader=Resources.getResourceAsReader("sqlMapConfig.xml");
sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
}catch(Exceptione){
e.printStackTrace();
}
}
publicvoidtestUser01()throwsException{
//创建用户数据
Useruser=newUser();
user.setUsername("用户"+newRandom().nextInt(9999));
user.setPassword("密码"+newRandom().nextInt(9999));
//可以执行
sqlMapClient.insert("insertUser01",user);
}
publicvoidtestUser02()throwsException{
//创建用户数据
JustAnObjectuser=newJustAnObject();
user.setUsername("用户"+newRandom().nextInt(9999));
user.setPassword("密码"+newRandom().nextInt(9999));
//不可以执行,因为要求传入参数必须是User类型
sqlMapClient.insert("insertUser01",user);
}
publicvoidtestUser03()throwsException{
//创建用户数据
Useruser=newUser();
user.setUsername("用户"+newRandom().nextInt(9999));
user.setPassword("密码"+newRandom().nextInt(9999));
//可以执行,因为不限制传入参数的类型
sqlMapClient.insert("insertUser02",user);
}
publicvoidtestUser04()throwsException{
//创建用户数据
JustAnObjectuser=newJustAnObject();
user.setUsername("用户"+newRandom().nextInt(9999));
user.setPassword("密码"+newRandom().nextInt(9999));
//可以执行,因为不限制传入参数的类型
sqlMapClient.insert("insertUser02",user);
}
}
3parameterMap
可以使用parameterMap来映射SQL语句中的?
参数
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEsqlMap
PUBLIC"-//ibatis.apache.org//DTDSQLMap2.0//EN"
"http:
//ibatis.apache.org/dtd/sql-map-2.dtd">
insertintot_uservalues(null,?
?
);
publicvoidtestUser()throwsException{
//从配置文件中得到SqlMapClient对象
Readerreader=Resources.getResourceAsReader("sqlMapConfig.xml");
SqlMapClientsqlMapper=SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
//创建用户数据
Useruser=newUser();
user.setUsername("用户"+newRandom().nextInt(9999));
user.setPassword("密码"+newRandom().nextInt(9999));
//可以执行
sqlMapper.insert("insertUser",user);
}
4resultClass属性的使用
如果对象的属性命名与数据库表字段的命名不一致,如何将查询结果映射到这个对象?
【resultClass属性的使用】
第一种做法:
在写SQL语句的时候,给字段命别名(别名与对象的属性名一致)即可。
publicclassUser{
privateintid;
privateStringusername;
privateStringpassword;
set..get..
}
publicclassOtherObject{
privateStringprop1;
privateStringprop2;
privateStringother;
set..get..
}
selectcount(*)fromt_user
selectusernameasprop1,passwordasprop2fromt_userwhereid=#value#;
publicclassUserTestextendsTestCase{
privateSqlMapClientsqlMapClient=null;
privateReaderreader=null;
@Override
protectedvoidsetUp()throwsException{
//从配置文件中得到SqmMapClient对象
try{
reader=Resources.getResourceAsReader("sqlMapConfig.xml");
sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
}catch(Exceptione){
}
System.out.println("setUp");
}
publicvoidtestUser()throwsException{
Objecto=(Object)sqlMapClient.queryForObject("selectCountForOtherObject");
System.out.println(o);
}
publicvoidtestUser01()throwsException{
OtherObjectoo=(OtherObject)sqlMapClient.queryForObject("selectForOtherObject",17);
System.out.println(oo.getProp1());
}
}
5resultMap属性的使用
如果对象的属性命名与数据库表字段的命名不一致,如何将查询结果映射到这个对象?
【resultMap属性的使用】
第二种做法:
在写SQL语句的时候,无需给字段命别名,而是通过定义一个resultMap来明确映射字段与属性
selectusername,passwordfromt_userwhereid=#id#
publicvoidtestUser01()throwsException{
OtherObjectoo=