ibatis数据源的配置

如下代码:

最底层的dao接口:

public interface Dao {
    Object getObject(Class clazz, Object obj) throws DataAccessException;

    List getObjects(Class clazz, Object obj) throws DataAccessException;

    List getObjectsByPage(Class clazz, Object obj, int skipResults, int maxResults) throws DataAccessException;

    Object insert(Class clazz, Object obj) throws DataAccessIntegrityViolationException, DataAccessException;

    int update(Class clazz, Object obj) throws DataAccessIntegrityViolationException, DataAccessException;

    int delete(Class clazz, Object obj) throws DataAccessIntegrityViolationException, DataAccessException;

    int getCount(Class clazz, Object obj) throws DataAccessException;
    
    int getCount(String  statement, Object obj) throws DataAccessException;

    List getObjects(Object statement, Object obj) throws DataAccessException;

    Object insert(String statment, Object obj) throws DataAccessIntegrityViolationException, DataAccessException;

    int update(String statment, Object obj) throws DataAccessIntegrityViolationException, DataAccessException;

    int delete(String statment, Object obj) throws DataAccessIntegrityViolationException, DataAccessException;

    Object getObject(String statment, Object obj) throws DataAccessException;

    List getObjects(String statment, Object obj) throws DataAccessException;

    List getObjectsByPage(String statment, Object obj, int skipResults, int maxResults) throws DataAccessException;
    
    SqlMapClientTemplate getSqlMapTemplate() throws Exception;
}

最底层的 dao实现

public class XzBaseDao extends SqlMapClientDaoSupport implements Dao {   

    public SqlMapClientTemplate getSqlMapTemplate() throws Exception {
        return super.getSqlMapClientTemplate();
    }

    public Object getObject(String statment, Object obj) throws DataAccessException {
        try {
            return getSqlMapClientTemplate().queryForObject(statment, obj);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

	public List getObjects(String statement, Object obj) throws DataAccessException {
        try {
            return getSqlMapClientTemplate().queryForList(statement, obj);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }
 public Object getObject(Class clazz, Object obj) throws DataAccessException {
        try {
            return getSqlMapClientTemplate().queryForObject(getFindQuery(ClassUtils.getShortClassName(clazz)), obj);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    public List getObjects(Class clazz, Object obj) throws DataAccessException {
        try {
            return getSqlMapClientTemplate().queryForList(getSelectQuery(ClassUtils.getShortClassName(clazz)), obj);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }
。。。。。。。。。。
protected String getFindQuery(String className) {
        return className + ".get" + className;
    }

    protected String getSelectQuery(String className) {
        return className + ".get" + className + "s";
    }

    protected String getInsertQuery(String className) {
        return className + ".insert";
    }

    protected String getUpdateQuery(String className) {
        return className + ".update";
    }

    protected String getDeleteQuery(String className) {
        return className + ".delete";
    }

    protected String getCountQuery(String className) {
        return className + ".getCount";
    }
}

 分库曾继承dao实现类的子类

public class GlobalLoginSqlMapDao extends XzBaseDao {

}

 业务层的dao

public interface PlayerBaseInfoDao {
	public List<PlayerBaseInfoPo> selectPlayerBaseInfo(List<String> uids)throws Exception;
	public void insertPlayerBaseInfoPo(PlayerBaseInfoPo playerBaseInfoPo)throws Exception;
	public void updatePlayerBaseInfoPo(Map<String,Object> map)throws Exception;
	public PlayerBaseInfoPo getPlayerBaseInfo(String uid)throws Exception;
}

 业务层的dao实现

public class PlayerBaseInfoDaoImpl extends GlobalLoginSqlMapDao

 implements PlayerBaseInfoDao {

	@Override
	public void insertPlayerBaseInfoPo(PlayerBaseInfoPo playerBaseInfoPo) throws Exception {
		insert("global.insertPlayerBaseInfoPo", playerBaseInfoPo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PlayerBaseInfoPo> selectPlayerBaseInfo(List<String> uids) throws Exception {
		if(uids==null||uids.isEmpty()){
			return null;
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("uids", uids);
		return getObjects("global.selectPlayerBaseInfo", map);
	}
。。。。。。
}

注意: 业务层继承了分库曾的dao实现

配置文件applicationContext-datasource.xml


<bean id="globalLoginSqlMapDao" class="com.fiveagame.sns.xztx.persistence.sqlmapdao.GlobalLoginSqlMapDao">
	<property name="sqlMapClient" ref="sqlMapClientGlobalLogin

"/> 
</bean>

<bean id="sqlMapClientGlobalLogin" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
	<property name="configLocation">
		<value>classpath:ibatis/sql-map-global-login-config.xml

</value>
	</property>
	<property name="dataSource" ref="dataSourceGlobalLogin" />
</bean>

<!-- 全局login数据源 -->
	<bean id="dataSourceGlobalLogin" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${database.driverLogin}" />
		<property name="url" value="${database.urlLogin}" />
		<property name="username" value="${database.usernameLogin}" />
		<property name="password" value="${database.passwordLogin}" />
		<!-- 同一时间可以从池分配的最多连接数量。设置为0时表示无限制。  -->  
		<property name="maxActive" value="20" />
		<!-- 超时等待时间以毫秒为单位  -->  
		<property name="maxWait" value="500" />
		<!-- 池里不会被释放的最多空闲连接数量。设置为0时表示无限制。  -->  
		<property name="maxIdle" value="3" />
		<property name="defaultAutoCommit" value="true" />
		<!-- 设置自动回收超时连接 --> 
		<property name="removeAbandoned" value="true" />
		<!-- 自动回收超时时间(以秒数为单位) -->  
		<property name="removeAbandonedTimeout" value="60" />
	</bean>



 配置文件 sql-map-global-login-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
	<settings
		cacheModelsEnabled="true"
		enhancementEnabled="true"
		lazyLoadingEnabled="true"
		errorTracingEnabled="true"
		maxRequests="32"
		maxSessions="10"
		maxTransactions="5"
		useStatementNamespaces="true"/>

	<typeAlias alias="string" type="java.lang.String" />
	<typeAlias alias="int" type="java.lang.Integer" />
	<typeAlias alias="byte" type="java.lang.Byte" />
	<typeAlias alias="map" type="java.util.Map" />

	<sqlMap resource="ibatis/global.xml" />
</sqlMapConfig>


 ibatis配置文件global.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="global">
	<typeAlias alias="loginPo" type="com.xxx.LoginPo"/>
	<typeAlias alias="changeUcPo" type="com.xxx.global.entity.NinetravelChangeUcPo"/>
	<select id="getUcPo" parameterClass="string" resultClass="changeUcPo">
		select * from xx where ninetravelNumber=#uid#;
	</select>
............
</sqlMap>


 数据源配置文件:jdbc.properties

database.driverLogin=at.fpmedv.jdbc.LoggingDriver
database.urlLogin=jdbcdecorator:jdbc:mysql://localhost:3306/jian_login?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=round
database.usernameLogin=root
database.passwordLogin=jian

 

 

你可能感兴趣的:(ibatis)