Using Ibatis 3

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();
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值