3. Test.java
/**
*
*/
package com.test;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import com.dingfei.config.SqlAppConfig;
import com.dingfei.vo.Employee;
import com.dingfei.vo.SiteConfig;
import com.ibatis.sqlmap.client.SqlMapClient;
/**
* @author Jeffy
*
*/
public class Test {
public static void showAllEmployee(SqlMapClient sqlmap) throws SQLException{
List list = sqlmap.queryForList("selectAllEmployee");
for (Iterator iter = list.iterator(); iter.hasNext();) {
Employee e = (Employee) iter.next();
System.out.println(e);
}
}
public static void showAllSiteConfig(SqlMapClient sqlmap) throws SQLException{
List siteList = sqlmap.queryForList("selectAllSiteConfig");
for (Iterator iter = siteList.iterator(); iter.hasNext();) {
SiteConfig e = (SiteConfig) iter.next();
System.out.println(e);
}
}
public static List multiSelect(SqlMapClient sqlmap, String sql, Object params) throws SQLException{
List list = sqlmap.queryForList(sql,params);
return list;
}
public static Object singleSelect(SqlMapClient sqlmap, String sql, Object params) throws SQLException{
return sqlmap.queryForObject(sql, params);
}
public static List queryAllForList(SqlMapClient sqlmap, String sql) throws SQLException{
List list = sqlmap.queryForList(sql);
return list;
}
/**
* <p>
* <insert id="insertSiteByMap" parameterClass="java.util.HashMap">
insert into siteConfig(SITEID,ITEMNAME,ITEMVALUE)
values(#siteid#,#itemname#,#itemvalue#)
</insert>
* </p>
* @param sqlmap
* @param sql
* @param params HashMap including parameters
* @throws SQLException
*/
public static void insertByMap(SqlMapClient sqlmap, String sql, Object params) throws SQLException{
sqlmap.insert(sql, params);
}
/**
* <p>
* <typeAlias alias="SiteConfig" type="com.dingfei.vo.SiteConfig" />
* </p>
*
* <p>
* <insert id="insertSiteByVO" parameterClass="SiteConfig">
insert into siteConfig(SITEID,ITEMNAME,ITEMVALUE)
values(#siteID#,#itemName#,#itemValue#)
</insert>
* </p>
* @param sqlmap
* @param sql
* @param params VO which is mapped with DB table
* @throws SQLException
*/
public static void insertByVOMap(SqlMapClient sqlmap, String sql, Object params) throws SQLException{
sqlmap.insert(sql, params);
}
/**
* <p>
* <parameterMap id="SiteConfigParamMap" class="SiteConfig">
<parameter property="siteID" />
<parameter property="itemName" />
<parameter property="itemValue" />
</parameterMap>
* </p>
* <p>
* <insert id="insertSiteByVOMap" parameterMap="SiteConfigParamMap">
insert into siteConfig(SITEID,ITEMNAME,ITEMVALUE)
values(?,?,?)
</insert>
* </p>
* @param sqlmap
* @param sql
* @param params VO which is mapped with DB table
* @throws SQLException
*/
public static void insertByParameterMap(SqlMapClient sqlmap, String sql, Object params) throws SQLException{
sqlmap.insert(sql, params);
}
/**
*
* @param sqlmap
* @param sql
* @param params condition parameters
* @throws SQLException
*/
public static void delete(SqlMapClient sqlmap, String sql, Object params) throws SQLException{
sqlmap.delete(sql, params);
}
/**
* @param args
*/
public static void main(String[] args) {
SqlMapClient sqlmap = SqlAppConfig.getSqlMapInstance();
try {
HashMap params = new HashMap();
// params.put("siteid", "10008");
params.put("itemname", "supportOther");
params.put("itemvalue", "1");
// sqlmap.insert("insertSiteByMap", params);
Test.insertByMap(sqlmap, "insertSiteByMapWithoutSiteID", params);
Test.showAllSiteConfig(sqlmap);
// SiteConfig site = new SiteConfig();
// site.setSiteID(10008);
// site.setItemName("supportTSP");
// site.setItemValue("0");
// Object result = sqlmap.insert("insertSiteByVOMap",site);
// System.out.println(result);
//
// Test.showAllSiteConfig(sqlmap);
// sqlmap.delete("deleteSiteBySiteID", 10008);
// Test.showAllSiteConfig(sqlmap);
} catch (SQLException e) {
e.printStackTrace();
}
}
}