spring mybatis memcached

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:property-placeholder location="classpath*:jdbc.properties" />

    <!-- proxool连接池 -->
    <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
        <property name="driver">
            <value>${jdbc.driverClassName}</value>
        </property>
        <property name="driverUrl">
            <value>${jdbc.url}</value>
        </property>
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="alias" value="Pool_dbname" />
        <property name="maximumActiveTime" value="600000" />
        <property name="prototypeCount" value="0" />
        <property name="maximumConnectionCount" value="50" />
        <property name="minimumConnectionCount" value="2" />
        <property name="simultaneousBuildThrottle" value="50" />
        <property name="houseKeepingTestSql" value="select CURRENT_DATE" />
    </bean>

    <!-- mybatis 配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:${jdbc.configLocation}" />
    </bean>

    <bean id="sqlMapClient"
        class="com.dc.appengine.oauth.dao.SqlMapClientTemplate">
        <property name="sqlSession" ref="sqlSession" />
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <!-- 事务管理器 -->
    <bean id="transactionmanager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 方法事务参数 -->
    <tx:advice id="serviceAdvice" transaction-manager="transactionmanager">
        <tx:attributes>
            <tx:method name="updateAppScalableStatus" propagation="REQUIRES_NEW"
                isolation="READ_UNCOMMITTED" rollback-for="Exception" />
            <tx:method name="updateDefaultProtocol" propagation="REQUIRES_NEW"
                isolation="READ_UNCOMMITTED" rollback-for="Exception" />
            <tx:method name="saveProtocol" propagation="REQUIRES_NEW"
                isolation="READ_UNCOMMITTED" rollback-for="Exception" />
            <tx:method name="saveNewNode" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="run*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="save*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="saveUpgrade" propagation="SUPPORTS"
                isolation="READ_COMMITTED" rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="start*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="stop*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="destroy*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="deploy*" propagation="REQUIRED" isolation="READ_COMMITTED"
                rollback-for="Exception" />
            <tx:method name="reapply" propagation="REQUIRED" isolation="READ_COMMITTED"
                rollback-for="Exception" />
            <tx:method name="redeploy*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="isolate*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="return*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="unisolate*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- 配置切面 -->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="servicePointcut"
            expression="execution(* com.dc.appengine.oauth.service.impl.*.*(..))" />
        <aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice" />
    </aop:config>

    <context:component-scan
        base-package="com.dc.appengine.oauth.dao.impl, com.dc.appengine.oauth.service.impl" />

</beans>

jdbc.properties

#oracle
#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:PAAS
#jdbc.username=PAAS
#jdbc.password=PAAS
#jdbc.configLocation=sql-map-oracle-config.xml

#mysql
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/kfpt?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
jdbc.configLocation=sql-map-mysql-config.xml

sql-map-mysql-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "ibatis-3-config.dtd">

<configuration>

    <settings>
        <setting name="lazyLoadingEnabled" value="false" />
        <setting name="cacheEnabled" value="true"/>
    </settings>

    <mappers>
        <mapper resource="db/mapping/mysql/User.xml"/>
        <mapper resource="db/mapping/mysql/AppInfo.xml"/>
        <mapper resource="db/mapping/mysql/Service.xml"/>
        <mapper resource="db/mapping/mysql/Resource.xml"/>
        <mapper resource="db/mapping/mysql/Role.xml"/>
        <mapper resource="db/mapping/mysql/Grant.xml"/>
    </mappers>

</configuration>

config.properties

#jetty
port=5022
contextPath=/oauth2
resourceBase=../web
descriptor=../web/WEB-INF/web.xml
threadPoolSize=10

#接口实现类
IUserImplClass=com.dc.appengine.oauth.interfaces.impl.UserImpl
IOAuthImplClass=com.dc.appengine.oauth.interfaces.impl.OAuthImpl

#超时时间
code=600
token=3600

#https
keystore=oauth-server.keystore
KeyPassword=111111

#memcached 配置
cache.open = true
cache.servers = 10.126.253.49:11211
cache.pool.size = 10

log4j.properties

log4j.rootLogger=info,console,debug,info,warn,error,fatal

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d [%-5p]  (%F.%M:%L) - %m%n

log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File=./log/oauth/oauth_debug
log4j.appender.debug.DatePattern=‘.‘yyyyMMdd‘.log‘
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%d [%-5p]  (%F.%M:%L) - %m%n
log4j.appender.debug.filter.F1=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.debug.filter.F1.LevelMin=debug
log4j.appender.debug.filter.F1.LevelMax=debug

log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.File=./log/oauth/oauth_info
log4j.appender.info.DatePattern=‘.‘yyyyMMdd‘.log‘
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%d [%-5p]  (%F.%M:%L) - %m%n
log4j.appender.info.filter.F1=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.info.filter.F1.LevelMin=info
log4j.appender.info.filter.F1.LevelMax=info

log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender
log4j.appender.warn.File=./log/oauth/oauth_warn
log4j.appender.warn.DatePattern=‘.‘yyyyMMdd‘.log‘
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern=%d [%-5p]  (%F.%M:%L) - %m%n
log4j.appender.warn.filter.F1=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.warn.filter.F1.LevelMin=warn
log4j.appender.warn.filter.F1.LevelMax=warn

log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File=./log/oauth/oauth_error
log4j.appender.error.DatePattern=‘.‘yyyyMMdd‘.log‘
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%d [%-5p]  (%F.%M:%L) - %m%n
log4j.appender.error.filter.F1=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.error.filter.F1.LevelMin=error
log4j.appender.error.filter.F1.LevelMax=error

log4j.appender.fatal=org.apache.log4j.DailyRollingFileAppender
log4j.appender.fatal.File=./log/oauth/oauth_fatal
log4j.appender.fatal.DatePattern=‘.‘yyyyMMdd‘.log‘
log4j.appender.fatal.layout=org.apache.log4j.PatternLayout
log4j.appender.fatal.layout.ConversionPattern=%d [%-5p]  (%F.%M:%L) - %m%n
log4j.appender.fatal.filter.F1=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.fatal.filter.F1.LevelMin=fatal
log4j.appender.fatal.filter.F1.LevelMax=fatal

#log4j.logger.com.dc.oauth.server=debug

#----------------------------------------------------Ibaits----------------------------------------------------
#log4j.logger.com.ibatis=debug
#log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
#log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
#log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
#log4j.logger.java.sql.Connection=debug
#log4j.logger.java.sql.Statement=debug

#log4j.logger.java.sql.PreparedStatement=debug,stdout
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.out
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601}[%-5p][%c][%t]:%m%n

appinfo.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "ibatis-3-mapper.dtd">
<mapper namespace="AppInfo">
    <cache eviction="LRU" type="com.dc.appengine.oauth.cache.MemcacheCacheHelper4Mybatis" />
    <select id="findByAppKey" parameterType="java.lang.String" resultType="java.util.HashMap">
        select *
        from appinfo
        where appkey = #{appKey}
    </select>
    <select id="findBySecondDomain" parameterType="java.lang.String" resultType="java.util.HashMap">
        select *
        from appinfo
        where second_domain = #{secondDomain}
    </select>
    <select id="findByAppId" parameterType="java.lang.String" resultType="java.util.HashMap">
        select *
        from appinfo
        where appid = #{appId}
    </select>
</mapper>

MemcacheCacheHelper4Mybatis.java

/**
 *
 */
package com.dc.appengine.oauth.cache;

import java.util.LinkedList;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.ibatis.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dc.appengine.oauth.util.ConfigUtil;

/**
 * @author Administrator
 *2014-10-20
 */
public class MemcacheCacheHelper4Mybatis implements Cache{

    private static Logger LOG = LoggerFactory.getLogger( MemcacheCacheHelper4Mybatis.class ); 

    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    private String id;

    private LinkedList<String> cacheKeys = new LinkedList<String>();

    private MemcachedManager cache;

    private boolean openCache = Boolean.valueOf(ConfigUtil.getInstance().getProperty("cache.open"));

    public MemcacheCacheHelper4Mybatis( String id ){
        this.id = id;
        this.cache = MemcachedManager.getInstance();
    }

    /* (non-Javadoc)
     * @see org.apache.ibatis.cache.Cache#clear()
     */
    @Override
    public void clear() {
        if( openCache ){
            if( cacheKeys != null && cacheKeys.size() > 0 ){
                for( String key : cacheKeys ){
                    try {
                        cache.getClient().delete( key );
                    } catch ( Exception e ) {
                        e.printStackTrace();
                        LOG.error( "delete " + key + " error! ", e );
                    }
                }
                cacheKeys.clear();
                LOG.debug("clear");
            }
        }
    }

    /* (non-Javadoc)
     * @see org.apache.ibatis.cache.Cache#getId()
     */
    @Override
    public String getId() {
        LOG.debug("getId:" + this.id);
        return this.id;
    }

    /* (non-Javadoc)
     * @see org.apache.ibatis.cache.Cache#getObject(java.lang.Object)
     */
    @Override
    public Object getObject( Object key ) {
        if( this.openCache ){
            String cacheKey = String.valueOf(key.hashCode());
            try {
                Object obj = cache.getClient().get( cacheKey );
                if( obj != null && !cacheKeys.contains( cacheKey ) ){
                    cacheKeys.add( cacheKey );
                } else if( obj == null && cacheKeys.contains( cacheKey ) ){
                    cacheKeys.remove( cacheKey );
                }
                LOG.debug("getObject:" + obj);
                return obj;
            } catch ( Exception e ) {
                e.printStackTrace();
                LOG.error( "get " + key.toString() + " error!", e );
            }
        }
        return null;
    }

    /* (non-Javadoc)
     * @see org.apache.ibatis.cache.Cache#getReadWriteLock()
     */
    @Override
    public ReadWriteLock getReadWriteLock() {
        LOG.debug("getReadWriteLock");
        return this.readWriteLock;
    }

    /* (non-Javadoc)
     * @see org.apache.ibatis.cache.Cache#getSize()
     */
    @Override
    public int getSize() {
        LOG.debug("getSize:" + cacheKeys.size());
        return cacheKeys.size();
    }

    /* (non-Javadoc)
     * @see org.apache.ibatis.cache.Cache#putObject(java.lang.Object, java.lang.Object)
     */
    @Override
    public void putObject(Object key, Object value) {
        if( this.openCache ){
            if( value != null && !"SERIALIZABLE_NULL_OBJECT".equals( value ) ){
                String cacheKey = String.valueOf(key.hashCode());
                cacheKeys.add( cacheKey );
                try {
                    cache.getClient().set( cacheKey, 0, value );
                    LOG.debug("set " + key + "=" + value);
                } catch ( Exception e ) {
                    e.printStackTrace();
                    LOG.error( "set " + cacheKey + " error!", e );
                }
            }

        }
    }

    /* (non-Javadoc)
     * @see org.apache.ibatis.cache.Cache#removeObject(java.lang.Object)
     */
    @Override
    public Object removeObject(Object key) {
        if( this.openCache ){
            String cacheKey = String.valueOf(key.hashCode());
            try {
                Object obj = cache.getClient().delete( cacheKey );
                cacheKeys.remove( cacheKey );
                LOG.debug("remove " + cacheKey);
                return obj;
            } catch ( Exception e ) {
                e.printStackTrace();
                LOG.error( "delete " + cacheKey + " error!", e);
            }
        }
        return null;
    }

}

MemcachedManager.java

/**
 *
 */
package com.dc.appengine.oauth.cache;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dc.appengine.oauth.util.ConfigUtil;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.command.BinaryCommandFactory;
import net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator;
import net.rubyeye.xmemcached.utils.AddrUtil;

/**
 * @author Administrator
 *2014-10-8
 */
public class MemcachedManager {

    private final Logger logger = LoggerFactory.getLogger( MemcachedManager.class ); 

    private String servers;

    private int poolSize;

    private boolean openCache = false;

    private MemcachedClient client;

    private static MemcachedManager instance;

    public static MemcachedManager getInstance(){
        if( instance == null ){
            synchronized( MemcachedManager.class ){
                if( instance == null ){
                    instance = new MemcachedManager();
                }
            }
        }
        return instance;
    }

    private MemcachedManager(){
        try {
            init();
        } catch (IOException e) {
            logger.error( "cache manager init failed!", e );
        }
    }

    public MemcachedClient getClient(){
        return client;
    }

    private void init() throws IOException{
//        openCache = Boolean.parseBoolean( ConfigHelper.getValue("cache.open") );
        openCache = Boolean.parseBoolean(ConfigUtil.getInstance().getProperty("cache.open"));
        if( openCache ){
//            servers = ConfigHelper.getValue("cache.servers");
            servers = ConfigUtil.getInstance().getProperty("cache.servers");
//            poolSize = Integer.parseInt( ConfigHelper.getValue("cache.pool.size") );
            poolSize = Integer.parseInt(ConfigUtil.getInstance().getProperty("cache.pool.size"));
            MemcachedClientBuilder builder = new XMemcachedClientBuilder( AddrUtil.getAddresses( servers ) );
            builder.setCommandFactory( new BinaryCommandFactory() );
            builder.setConnectionPoolSize( poolSize );
            builder.setSessionLocator( new KetamaMemcachedSessionLocator() );
            client = builder.build();
        }
    }

    public void closePool() throws IOException {
        client.shutdown();
        client = null;
        logger.info("Ibatis memcached pool closed");
    }

}

SqlMapClientDaoSupport.java

/**
 *
 */
package com.dc.appengine.oauth.dao;

import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * @author Administrator
 *2014-10-17
 */
public class SqlMapClientDaoSupport {

    private SqlMapClient sqlMapClient;

    public SqlMapClientTemplate getSqlMapClientTemplate() {
        return (SqlMapClientTemplate) sqlMapClient;
    }

    public void setSqlMapClient(SqlMapClient sqlMapClient) {
        this.sqlMapClient = sqlMapClient;
    }

}

SqlMapClientTemplate.java

/**
 *
 */
package com.dc.appengine.oauth.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.ibatis.common.util.PaginatedList;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapSession;
import com.ibatis.sqlmap.client.event.RowHandler;

/**
 * @author Administrator
 *2014-10-17
 */

@SuppressWarnings("deprecation")
public class SqlMapClientTemplate extends SqlSessionDaoSupport implements SqlMapClient{

    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
        super.setSqlSessionTemplate( sqlSession );
    }

    public SqlSessionTemplate getSqlSession() {
        return sqlSession;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapClient#flushDataCache()
     */
    @Override
    public void flushDataCache() {
        super.getSqlSession().clearCache();
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapClient#flushDataCache(java.lang.String)
     */
    @Override
    public void flushDataCache(String arg0) {

    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapClient#getSession()
     */
    @Override
    public SqlMapSession getSession() {
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapClient#openSession()
     */
    @Override
    public SqlMapSession openSession() {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapClient#openSession(java.sql.Connection)
     */
    @Override
    public SqlMapSession openSession(Connection arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#delete(java.lang.String)
     */
    @Override
    public int delete(String arg0) {
        return super.getSqlSession().delete(arg0);
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#delete(java.lang.String, java.lang.Object)
     */
    @Override
    public int delete(String arg0, Object arg1) {
        // TODO Auto-generated method stub
        return super.getSqlSession().delete(arg0, arg1);
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#executeBatch()
     */
    @Override
    public int executeBatch() throws SQLException {
        // TODO Auto-generated method stub
        List<BatchResult> list = super.getSqlSession().flushStatements();
        int i = 0;
        for( BatchResult br : list ){
            int[] counts = br.getUpdateCounts();
            for( int count : counts ){
                i += count;
            }
        }
        return i;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#executeBatchDetailed()
     */
    @Override
    public List executeBatchDetailed() {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#insert(java.lang.String)
     */
    @Override
    public Object insert(String statement) {
        return super.getSqlSession().insert( statement );
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#insert(java.lang.String, java.lang.Object)
     */
    @Override
    public Object insert(String statement, Object object) {
        return super.getSqlSession().insert( statement, object );
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryForList(java.lang.String)
     */
    @Override
    public List queryForList(String arg0) {
        // TODO Auto-generated method stub
        return super.getSqlSession().selectList(arg0);
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryForList(java.lang.String, java.lang.Object)
     */
    @Override
    public List queryForList(String statement, Object object){
        return super.getSqlSession().selectList( statement, object );
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryForList(java.lang.String, int, int)
     */
    @Override
    public List queryForList(String arg0, int arg1, int arg2)
            throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryForList(java.lang.String, java.lang.Object, int, int)
     */
    @Override
    public List queryForList(String arg0, Object arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        RowBounds rb = new RowBounds( arg2, arg3);
        return super.getSqlSession().selectList(arg0, arg1, rb);
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryForMap(java.lang.String, java.lang.Object, java.lang.String)
     */
    @Override
    public Map queryForMap(String arg0, Object arg1, String arg2)
            throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryForMap(java.lang.String, java.lang.Object, java.lang.String, java.lang.String)
     */
    @Override
    public Map queryForMap(String arg0, Object arg1, String arg2, String arg3)
            throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryForObject(java.lang.String)
     */
    @Override
    public Object queryForObject(String arg0) {
        // TODO Auto-generated method stub
        return super.getSqlSession().selectOne(arg0);
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryForObject(java.lang.String, java.lang.Object)
     */
    @Override
    public Object queryForObject(String arg0, Object arg1) {
        // TODO Auto-generated method stub
        return super.getSqlSession().selectOne(arg0, arg1);
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryForObject(java.lang.String, java.lang.Object, java.lang.Object)
     */
    @Override
    public Object queryForObject(String arg0, Object arg1, Object arg2)
            throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryForPaginatedList(java.lang.String, int)
     */
    @SuppressWarnings("deprecation")
    @Override
    public PaginatedList queryForPaginatedList(String arg0, int arg1)
            throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryForPaginatedList(java.lang.String, java.lang.Object, int)
     */
    @SuppressWarnings("deprecation")
    @Override
    public PaginatedList queryForPaginatedList(String arg0, Object arg1,
            int arg2) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#startBatch()
     */
    @Override
    public void startBatch() throws SQLException {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#update(java.lang.String)
     */
    @Override
    public int update(String arg0) throws SQLException {
        // TODO Auto-generated method stub
        return super.getSqlSession().update(arg0);
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#update(java.lang.String, java.lang.Object)
     */
    @Override
    public int update(String arg0, Object arg1) {
        // TODO Auto-generated method stub
        return super.getSqlSession().update(arg0, arg1);
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapTransactionManager#commitTransaction()
     */
    @Override
    public void commitTransaction() throws SQLException {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapTransactionManager#endTransaction()
     */
    @Override
    public void endTransaction() throws SQLException {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapTransactionManager#getCurrentConnection()
     */
    @Override
    public Connection getCurrentConnection() throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapTransactionManager#getDataSource()
     */
    @Override
    public DataSource getDataSource() {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapTransactionManager#getUserConnection()
     */
    @Override
    public Connection getUserConnection() throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapTransactionManager#setUserConnection(java.sql.Connection)
     */
    @Override
    public void setUserConnection(Connection arg0) throws SQLException {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapTransactionManager#startTransaction()
     */
    @Override
    public void startTransaction() throws SQLException {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapTransactionManager#startTransaction(int)
     */
    @Override
    public void startTransaction(int arg0) throws SQLException {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryWithRowHandler(java.lang.String, com.ibatis.sqlmap.client.event.RowHandler)
     */
    @Override
    public void queryWithRowHandler(String arg0, RowHandler arg1)
            throws SQLException {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see com.ibatis.sqlmap.client.SqlMapExecutor#queryWithRowHandler(java.lang.String, java.lang.Object, com.ibatis.sqlmap.client.event.RowHandler)
     */
    @Override
    public void queryWithRowHandler(String arg0, Object arg1, RowHandler arg2)
            throws SQLException {
        // TODO Auto-generated method stub

    }
}

AppInfoDAO.java

package com.dc.appengine.oauth.dao.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.dc.appengine.oauth.dao.SqlMapClientDaoSupport;
import com.ibatis.sqlmap.client.SqlMapClient;

@Component("AppInfoDAO")
public class AppInfoDAO extends SqlMapClientDaoSupport {

    @Autowired
    public AppInfoDAO(@Qualifier("sqlMapClient") SqlMapClient sqlMapClient) {
        super();
        super.setSqlMapClient(sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> findByAppKey(String appKey) {
        Object result = getSqlMapClientTemplate().queryForObject("AppInfo.findByAppKey", appKey);
        if (result == null) {
            return null;
        } else {
            return (Map<String, Object>) result;
        }
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> findBySecondDomain(String secondDomain) {
        Object result = getSqlMapClientTemplate().queryForObject("AppInfo.findBySecondDomain", secondDomain);
        if (result == null) {
            return null;
        } else {
            return (Map<String, Object>) result;
        }
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> findByAppId(String appId) {
        Object result = getSqlMapClientTemplate().queryForObject("AppInfo.findByAppId", appId);
        if (result == null) {
            return null;
        } else {
            return (Map<String, Object>) result;
        }
    }

}

AppInfoService.java

package com.dc.appengine.oauth.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.dc.appengine.oauth.dao.impl.AppInfoDAO;

@Service("AppInfoService")
public class AppInfoService {

    @Autowired
    @Qualifier("AppInfoDAO")
    private AppInfoDAO appInfoDAO;

    public Map<String, Object> findBySecondDomain(String secondDomain) {
        return appInfoDAO.findBySecondDomain(secondDomain);
    }

    public Map<String, Object> findByAppKey(String appKey) {
        return appInfoDAO.findByAppKey(appKey);
    }

    public Map<String, Object> findByAppId(String appId) {
        return appInfoDAO.findByAppId(appId);
    }

    public boolean hasApp(String appKey) {
        Map<String, Object> appInfo = findByAppKey(appKey);
        if (appInfo == null) {
            return false;
        } else {
            return true;
        }
    }

    public boolean checkApp(String appKey, String appSecret) {
        Map<String, Object> appInfo = findByAppKey(appKey);
        if (appInfo == null) {
            return false;
        } else {
            String secret = (String) appInfo.get("appSecretKey");
            if (appSecret != null && appSecret.equals(secret)) {
                return true;
            } else {
                return false;
            }
        }
    }

}
时间: 2024-10-27 04:49:59

spring mybatis memcached的相关文章

java高级软件架构师实战视频教程 Spring+Mybatis+SpringMVC+Ehcache+Memcached+Redis+Nginx+Varnish+ActiveMQ+Keepalived+MySQL+MongoDB

java高级软件架构师实战阶段1视频教程 迅雷下载  百度云 第001节:整体课程概览第002节:分模块.分工程管理第003节:多模块多Web应用合并War包第004节:Git基本原理和安装配置使用第005节:TortoiseGit的本地使用第006节:Egit的本地使用第007节:远程使用以及冲突解决第008节:基本业务功能和数据字典第009节:搭建基础的开发环境第010节:Spring+Mybatis实现DAO第011节:Mybatis的分页实现第012节:Service的实现以及模块化第0

大型分布式项目项目实战Springmvc+Spring+Mybatis+Maven+CMS+Redis+Solr+Linux+Nginx+单点登录、分布式缓存、负载均衡视频课程

* { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展.高性能.高并发.性能优化.Spring boot.Redis.ActiveMQ.Nginx.Mycat.Netty.Jvm大型分布 式项目实战视频教程 视频课程包含: 高级Java架构师包含:Spring boot.Spring  cloud.Dubbo.Redis.ActiveMQ.Nginx.Mycat

Java Web开发中Spring+MyBatis框架的简单搭建

这里使用的eclipse,首先创建一个动态web项目. 1.导入Spring IOC.AOP.DAO.dbcp.dbdrive.mybatis.jar . mybatis-spring.jar  本人使用的jar包和版本如下: aopalliance.jaraspectjweaver.jarcommons-dbcp-1.4.jarcommons-logging.jarcommons-pool-1.5.6.jarmybatis-3.2.5.jarmybatis-spring-1.2.2.jarmy

idea+springmvc+spring+mybatis+maven整合返回json数据webapi

首先看一张目录结构图: : 创建步骤: 1.创建maven  webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring-test, spring-orm, commons-dbcp, mybatis, mybatis-spring, mysql-connector-java, commons-fileupload, jstl,jackson-core, jackson-databind, jackson-mappe

SpringMVC+Spring+Mybatis -- 集成之旅

准备 首先介绍一下,我的工具使用的是STS, 需要的童鞋可以到官网下载:http://spring.io/tools/sts/all 使用STS是因为她集成了Maven进行 “包“ 管理以及自带 Web server 方便部署(不用配置啦~),个人感觉还是挺方便的.如果大家想要自己配置Tomcat,也未尝不可 :) 我下载的是压缩包的STS,下载完成后解压缩 -> 启动STS.exe ->指定 workspace ->创建Maven项目 -> 选择建立 webapp 项目. 等待片

Idea SpringMVC+Spring+MyBatis+Maven调整【转】

Idea SpringMVC+Spring+MyBatis+Maven整合 创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetype,然后选中下方列表中的webapp,然后点击Next 在GroupId和ArtifactId中填入指定内容,点击Next 直接点Next 输入项目名称,Finish Idea会自动开始下载所依赖的包,等待其完成. 项目结构 项目刚建好的时候是没有这些文件的,所以自己手动创建缺少的文件夹(包) 创建完后

SpringMVC+Spring+Mybatis(SSM~Demo) 【转】

SpringMVC+Spring+Mybatis 框架搭建 整个Demo的视图结构: JAR: 下载地址:http://download.csdn.net/detail/li1669852599/8546059 首先,我是使用MyEclipse工具做的这个例子,整合了Sping 3 .Spring MVC 3 .MyBatis框架,演示数据库采用MySQL数据库.例子中主要操作包括对数据的添加(C).查找(R).更新(U).删除(D).我在这里采用的数据库连接池是来自阿里巴巴的Druid,至于D

spring+mybatis利用interceptor(plugin)兑现数据库读写分离

使用spring的动态路由实现数据库负载均衡 系统中存在的多台服务器是“地位相当”的,不过,同一时间他们都处于活动(Active)状态,处于负载均衡等因素考虑,数据访问请求需要在这几台数据库服务器之间进行合理分配, 这个时候,通过统一的一个DataSource来屏蔽这种请求分配的需求,从而屏蔽数据访问类与具体DataSource的耦合: 系统中存在的多台数据库服务器现在地位可能相当也可能不相当,但数据访问类在系统启动时间无法明确到底应该使用哪一个数据源进行数据访问,而必须在系统运行期间通过某种条

Maven 搭建SpringMvc+Spring+Mybatis详细记录

总觉得,看比人写的总是那么好,每次搭建框架时都会找博客,找教程来跟着一步一步走,虽然很快搭建成功了,但是经常情况是我并不知道我干了什么,也不记得具体步骤,到底为什么要这么做,今天我详细记录了一下自己搭建的过程,并且尽量理解每一步干什么. SSM框架当下比较流行,我也是用这个框架来作为记录,尝试详细地记录下每一个步骤,学习,不要怕开头难. 一.创建一个新的Maven项目 1. new -> Maven -> Maven Project 选择webapp工程. 2.maven项目建好以后,工程目录