前面,已经发个工程。现在讲下里面代码的具体功能和作用。
我也是半桶水,只是了解一点,原来这个工程也是另一个同事搞的这个框架。
这几个包的用途就不说了。反正全打上。呵呵。
1:com.kingter.groupapp.business 应用功能包
2:com.kingter.groupapp.domain 程序中用到的数据类包
3:com.kingter.groupapp.persistence ibatis 程序映射包
4:com.kingter.groupapp.persistence.iface ibatis 程序映射 接口类包
5:com.kingter.groupapp.persistence.sqlmapdao ibatis 程序映射 接口实现类包
6:com/kingter/groupapp/properties/ 配置文件包
7:com.kingter.groupapp.service ibatis 对外实现包
下面介绍代码:
- 先写数据类
package com.kingter.groupapp.domain; import java.io.*; /** * 数据类 继承 Serializable ,要写构造函数,get set * @author Administrator * */ public class AreaInfo implements Serializable { private String areaID; private String areaName; private String areaDepict; public AreaInfo() { } public String getAreaDepict() { return areaDepict; } public String getAreaID() { return areaID; } public String getAreaName() { return areaName; } public void setAreaID(String areaID) { this.areaID = areaID; } public void setAreaName(String areaName) { this.areaName = areaName; } public void setAreaDepict(String areaDepict) { this.areaDepict = areaDepict; } }
2。 数据库及其它配置文件
com/kingter/groupapp/properties/groupappdatabase.properties
#################################### # Database Connectivity Properties #################################### #²ÊÐÅÓªÏúƽ̨ groupapp_driver=oracle.jdbc.driver.OracleDriver groupapp_url=jdbc:oracle:thin:@localhost:8512:ora9 groupapp_username=data_markmms groupapp_password=data_mmsmark
其它应用数据配置文件 com/kingter/groupapp/properties/groupappconfig.properties
怎么用,自己在网上找吧,g.cn 上多的很。
####################################
# Properties
####################################
resourceWeb=http://localhost:8080/
3: DAO配置
package com.kingter.groupapp.persistence;
import java.io.Reader;
import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* <p>Title: DAO配置</p>
* <p>Description: 用于获得DAO管理对象</p>
* <p>Copyright: Copyright (c) 2010</p>
* <p>Company: </p>
* @author
* @version 1.0
*/
public class DaoConfig {
private static Log log = LogFactory.getLog(DaoConfig.class);
private static final DaoManager daoManager;
static {
try {
String resource = "com/kingter/groupapp/persistence/dao.xml";
Reader reader = Resources.getResourceAsReader(resource);
daoManager = DaoManagerBuilder.buildDaoManager(reader);
//log.info(daoManager.getClass().getName());
}
catch (Exception e) {
throw new RuntimeException("Could not initialize DaoConfig. Cause: " + e);
}
}
public static DaoManager getDaomanager() {
return daoManager;
}
}
接口与实现类映射配置。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE daoConfig
PUBLIC "-//iBATIS.com//DTD DAO Configuration 2.0//EN"
"http://www.ibatis.com/dtd/dao-2.dtd">
<daoConfig>
<context>
<transactionManager type="SQLMAP">
<property name="SqlMapConfigResource"
value="com/kingter/groupapp/persistence/sqlmapdao/sql/sql-map-config.xml"/>
</transactionManager>
<dao interface="com.kingter.groupapp.persistence.iface.AreaInfoDao"
implementation="com.kingter.groupapp.persistence.sqlmapdao.AreaInfoSqlMapDao"/>
</context>
</daoConfig>
4: ibatis 接口及实现
接口
package com.kingter.groupapp.persistence.iface;
import java.util.*;
import com.kingter.groupapp.domain.AreaInfo;
public interface AreaInfoDao {
public List findAllAreaInfo();
public AreaInfo findAreaInfoByAreaID(String AreaID);
}
实现基础类
这里其实代码只要几句,不知为什么,到了 MyEclipse6.0 就要我实现SqlMapDaoTemplate 所有的方法
package com.kingter.groupapp.persistence.sqlmapdao;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.common.util.PaginatedList;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;
import com.ibatis.sqlmap.client.event.RowHandler;
import com.ibatis.sqlmap.engine.execution.BatchException;
public class BaseSqlMapDao extends SqlMapDaoTemplate {
@Override
public int delete(String arg0) throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public List executeBatchDetailed() throws SQLException, BatchException {
// TODO Auto-generated method stub
return null;
}
@Override
public Object insert(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public List queryForList(String arg0, int arg1, int arg2)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public List queryForList(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Object queryForObject(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PaginatedList queryForPaginatedList(String arg0, int arg1)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void queryWithRowHandler(String arg0, RowHandler arg1)
throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int update(String arg0) throws SQLException {
// TODO Auto-generated method stub
return 0;
}
protected static final int PAGE_SIZE = 15;
public BaseSqlMapDao(DaoManager daoManager) {
super(daoManager);
}
}
实现类
package com.kingter.groupapp.persistence.sqlmapdao;
import java.sql.*;
import java.util.*;
import org.apache.commons.logging.*;
import com.ibatis.dao.client.*;
import com.kingter.groupapp.domain.AreaInfo;
import com.kingter.groupapp.persistence.iface.AreaInfoDao;
public class AreaInfoSqlMapDao extends BaseSqlMapDao implements AreaInfoDao {
private static Log log = LogFactory.getLog(AreaInfoSqlMapDao.class);
public AreaInfoSqlMapDao(DaoManager daoManager) {
super(daoManager);
}
public List findAllAreaInfo() {
List list = null;
try {
//SqlMapDaoTemplate 都有 sql 具体操作对应的方法。
//如: queryForList 查询多条数据。
list = queryForList("getAllAreaInfo", null);
}
catch (Exception ex) {
log.error(ex);
}
return list;
}
public AreaInfo findAreaInfoByAreaID(String AreaID){
AreaInfo areaInfo = null;
try{
areaInfo = (AreaInfo)queryForObject("getAreaInfoByAreaID",AreaID);
}
catch(Exception ex){
log.error(ex);
}
return areaInfo;
}
}
5: SQL 映射 XML 文件
数据源及SQL文件配置
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!---->
<properties resource="com/kingter/groupapp/properties/groupappdatabase.properties"/>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
maxSessions="64"
maxTransactions="8"
maxRequests="128"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property value="${groupapp_driver}" name="JDBC.Driver"/>
<property value="${groupapp_url}" name="JDBC.ConnectionURL"/>
<property value="${groupapp_username}" name="JDBC.Username"/>
<property value="${groupapp_password}" name="JDBC.Password"/>
<property value="15" name="Pool.MaximumActiveConnections"/>
<property value="15" name="Pool.MaximumIdleConnections"/>
<property value="1000" name="Pool.MaximumWait"/>
</dataSource>
<!--<property name="UserTransaction" value="UserTransaction" />
<dataSource type="JNDI">
<property name="DataSource" value="java:/mmssale" />
</dataSource>-->
</transactionManager>
<sqlMap resource="com/kingter/groupapp/persistence/sqlmapdao/sql/AreaInfo.xml"/>
</sqlMapConfig>
SQL语句配置
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="AreaInfo">
<typeAlias alias="areaInfo" type="com.kingter.groupapp.domain.AreaInfo"/>
<resultMap id="areaInfoResult" class="areaInfo">
<result property="areaID" column="area_id" nullValue=""/>
<result property="areaName" column="area_name" nullValue=""/>
<result property="areaDepict" column="area_depict" nullValue=""/>
</resultMap>
<select id="getAllAreaInfo" resultMap="areaInfoResult" parameterClass="string">
select area_id,area_name,area_depict
from t_area_info
</select>
<select id="getAreaInfoByAreaID" resultMap="areaInfoResult" parameterClass="string">
select area_id,area_name,area_depict
from t_area_info
where area_id = #value#
</select>
</sqlMap>
***********************************
ibatis SQL 语句的写的技巧,可以参考网上专门的示例。
parameterClass 参数类型,可以是 map,string ,object.
resultMap 返回结果集
id="getSiteInfoByMore 这个是名称,在整个项目中要是唯一的。
<dynamic prepend="where"> 动态增加 where 语句
<isNotEmpty prepend="and" property="siteID">
site_id = #siteID#
</isNotEmpty>
如果 siteID 为空,则不会生成这个条件,反之则生成。
<isNotEmpty prepend="and" property="siteName">
<![CDATA[
( site_name LIKE '%$siteName$%')
]]>
</isNotEmpty>
这个就是 like 语句
<statement id="getSiteInfoByMore" resultMap="siteInfoResult" parameterClass="java.util.Map"> select site_id,company_id,logo_url,site_name,site_url,site_title,site_key,site_meta,copyright from t_site_info <dynamic prepend="where"> <isNotEmpty prepend="and" property="siteID"> site_id = #siteID# </isNotEmpty> <isNotEmpty prepend="and" property="siteName"> <![CDATA[ ( site_name LIKE '%$siteName$%') ]]> </isNotEmpty> <isNotEmpty prepend="and" property="companyID"> company_id = #companyID# </isNotEmpty> </dynamic> </statement>
select site_id,company_id,logo_url,site_name,site_url,site_title,site_key,site_meta,copyright
from t_site_info#sqlnewcode#
#sqlnewcode#
你还可以从前台动态带入SQL 语句。但我个人不提议用,以后不好维护。
***********************************
7: ibatiS SQL 功能实现
用于客户端(表示层)来统一访问,业务层调用DAO将数据持久化
package com.kingter.groupapp.service;
/**
* <p>Title: 业务服务接口(业务层)</p>
* <p>Description: 用于客户端(表示层)来统一访问,业务层调用DAO将数据持久化</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author
* @version 1.0
*/
public interface ServiceBean {
}
实现代码
package com.kingter.groupapp.service;
import java.util.*;
import org.apache.commons.logging.*;
import com.ibatis.dao.client.*;
import com.kingter.groupapp.domain.AreaInfo;
import com.kingter.groupapp.persistence.*;
import com.kingter.groupapp.persistence.iface.AreaInfoDao;
public class AreaInfoService implements ServiceBean {
private static Log log = LogFactory.getLog(AreaInfoService.class);
private static AreaInfoService instance = null;
private DaoManager daoManager = null;
private AreaInfoDao areaInfoDao;
private AreaInfoService() {
daoManager = DaoConfig.getDaomanager();
areaInfoDao = (AreaInfoDao) daoManager.getDao(AreaInfoDao.class);
}
public static AreaInfoService getInstance() {
if (instance == null) {
instance = new AreaInfoService();
}
return instance;
}
public List findAllAreaInfo() {
return areaInfoDao.findAllAreaInfo();
}
public AreaInfo findAreaInfoByAreaID(String AreaID){
return areaInfoDao.findAreaInfoByAreaID(AreaID);
}
}
9: 外部应用代码
package com.kingter.groupapp.business;
import com.kingter.groupapp.domain.*;
import com.kingter.groupapp.service.*;
import java.util.*;
public class AreaInfoBean {
public AreaInfoBean() {
}
public List findAllAreaInfo() {
return AreaInfoService.getInstance().findAllAreaInfo();
}
public AreaInfo findAreaInfoByAreaID(String AreaID){
return AreaInfoService.getInstance().findAreaInfoByAreaID(AreaID);
}
}