Example usage for org.apache.ibatis.session SqlSession getConfiguration

List of usage examples for org.apache.ibatis.session SqlSession getConfiguration

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession getConfiguration.

Prototype

Configuration getConfiguration();

Source Link

Document

Retrieves current configuration.

Usage

From source file:cn.org.awcp.core.mybatis.mapper.MapperHelper.java

License:Open Source License

/**
 * Spring?Spring??init-method="initMapper"
 *//* ww w .j a v a 2s . co  m*/
public void initMapper() {
    // ?Spring,Spring?,???Spring
    // Spring,???
    // Spring4?,???Mapper
    initSpringVersion();
    for (SqlSession sqlSession : sqlSessions) {
        processConfiguration(sqlSession.getConfiguration());
    }
}

From source file:com.bluesky.iplatform.commons.db.mybatis.SqlHelper.java

License:Open Source License

/**
 * ????sql//from   w ww .j  a  v  a  2  s  .com
 *
 * @param session
 * @param namespace
 * @param params
 * @return
 */
public static String getNamespaceSql(SqlSession session, String namespace, Object params) {
    params = wrapCollection(params);
    Configuration configuration = session.getConfiguration();
    MappedStatement mappedStatement = configuration.getMappedStatement(namespace);
    TypeHandlerRegistry typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
    BoundSql boundSql = mappedStatement.getBoundSql(params);
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    String sql = boundSql.getSql();
    if (parameterMappings != null) {
        for (int i = 0; i < parameterMappings.size(); i++) {
            ParameterMapping parameterMapping = parameterMappings.get(i);
            if (parameterMapping.getMode() != ParameterMode.OUT) {
                Object value;
                String propertyName = parameterMapping.getProperty();
                if (boundSql.hasAdditionalParameter(propertyName)) {
                    value = boundSql.getAdditionalParameter(propertyName);
                } else if (params == null) {
                    value = null;
                } else if (typeHandlerRegistry.hasTypeHandler(params.getClass())) {
                    value = params;
                } else {
                    MetaObject metaObject = configuration.newMetaObject(params);
                    value = metaObject.getValue(propertyName);
                }
                JdbcType jdbcType = parameterMapping.getJdbcType();
                if (value == null && jdbcType == null)
                    jdbcType = configuration.getJdbcTypeForNull();
                sql = replaceParameter(sql, value, jdbcType, parameterMapping.getJavaType());
            }
        }
    }
    return sql;
}

From source file:com.eryansky.common.orm.mybatis.proxy.PaginationMapperMethod.java

License:Apache License

public PaginationMapperMethod(Class<?> declaringInterface, Method method, SqlSession sqlSession) {
    paramNames = new ArrayList<String>();
    paramPositions = new ArrayList<Integer>();
    this.sqlSession = sqlSession;
    this.method = method;
    this.config = sqlSession.getConfiguration();
    this.declaringInterface = declaringInterface;
    this.hasNamedParameters = false;
    setupFields();//from  w w  w.  j a va2s .c  o m
    setupMethodSignature();
    setupCommandType();
    validateStatement();
}

From source file:com.github.abel533.mapperhelper.MapperHelper.java

License:Open Source License

/**
 * Spring?Spring??init-method="initMapper"
 *///from  w w  w.ja va 2s . c  o  m
public void initMapper() {
    //?Spring,Spring?,???Spring
    //Spring,???
    //Spring4?,???Mapper
    initSpringVersion();
    for (SqlSession sqlSession : sqlSessions) {
        processConfiguration(sqlSession.getConfiguration());
    }
}

From source file:com.itfsw.mybatis.generator.plugins.tools.SqlHelper.java

License:Apache License

/**
 * ????sql/*from  w w  w.j av a 2  s .c  o m*/
 * @param session
 * @param namespace
 * @param params
 * @return
 */
public static String getNamespaceSql(SqlSession session, String namespace, Object params) {
    Configuration configuration = session.getConfiguration();
    MappedStatement mappedStatement = configuration.getMappedStatement(namespace);
    TypeHandlerRegistry typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
    BoundSql boundSql = mappedStatement.getBoundSql(params);
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    String sql = boundSql.getSql();
    if (parameterMappings != null) {
        for (int i = 0; i < parameterMappings.size(); i++) {
            ParameterMapping parameterMapping = parameterMappings.get(i);
            if (parameterMapping.getMode() != ParameterMode.OUT) {
                Object value;
                String propertyName = parameterMapping.getProperty();
                if (boundSql.hasAdditionalParameter(propertyName)) {
                    value = boundSql.getAdditionalParameter(propertyName);
                } else if (params == null) {
                    value = null;
                } else if (typeHandlerRegistry.hasTypeHandler(params.getClass())) {
                    value = params;
                } else {
                    MetaObject metaObject = configuration.newMetaObject(params);
                    value = metaObject.getValue(propertyName);
                }
                JdbcType jdbcType = parameterMapping.getJdbcType();
                if (value == null && jdbcType == null)
                    jdbcType = configuration.getJdbcTypeForNull();
                sql = replaceParameter(sql, value, jdbcType, parameterMapping.getJavaType());
            }
        }
    }
    return sql;
}

From source file:com.jwmoon.framework.sql.MyBatisQueryResolver.java

License:Open Source License

private String resolveQuery(SqlSession sqlSession, String queryId, Object sqlParam) {

    BoundSql boundSql = sqlSession.getConfiguration().getMappedStatement(queryId).getSqlSource()
            .getBoundSql(sqlParam);//from ww w.  j av  a 2  s  .c om

    String sql = boundSql.getSql();
    //System.out.println(" ? SQL : " + sql);

    Object paramObj = boundSql.getParameterObject();

    if (paramObj != null) {
        List<ParameterMapping> paramMapping = boundSql.getParameterMappings();

        Class<? extends Object> paramClass = paramObj.getClass();

        for (ParameterMapping mapping : paramMapping) {
            String propValue = mapping.getProperty();
            try {
                Field field = paramClass.getDeclaredField(propValue);
                field.setAccessible(true); //  private?   

                Class<?> javaType = mapping.getJavaType();

                if (String.class == javaType) {
                    sql = sql.replaceFirst("\\?", "'" + field.get(paramObj) + "'");
                } else {
                    sql = sql.replaceFirst("\\?", "'" + field.get(paramObj).toString() + "'");
                }

            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    return sql;
}

From source file:com.mirth.connect.server.util.DatabaseUtil.java

License:Open Source License

/**
 * Returns true if the statement exists in the SqlMap, false otherwise.
 * //from   w ww  . ja v  a2 s .  com
 * @param statement
 *            the statement, including the namespace
 * @param session
 *            the SqlMapSession to use
 * @return
 */
public static boolean statementExists(String statement, SqlSession session) {
    try {
        session.getConfiguration().getMappedStatement(statement);
    } catch (IllegalArgumentException e) {
        // The statement does not exist
        return false;
    }

    return true;
}

From source file:com.quancheng.mybatis.mapper.test.user.TestBasic.java

License:Open Source License

/**
 * //w  w  w  .  j a v  a  2  s  . c om
 */
@Test
public void testInsert() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("abel533");
        userInfo.setPassword("123456");
        userInfo.setUsertype("2");
        userInfo.setEmail("abel533@gmail.com");
        Collection collection = sqlSession.getConfiguration().getMappedStatements();
        for (Object o : collection) {
            if (o instanceof MappedStatement) {
                MappedStatement ms = (MappedStatement) o;
                if (ms.getId().contains("UserInfoMapper.insert")) {
                    System.out.println(ms.getId());
                }
            }
        }

        Assert.assertEquals(1, mapper.insert(userInfo));

        Assert.assertNotNull(userInfo.getId());
        Assert.assertTrue((int) userInfo.getId() >= 6);

        Assert.assertEquals(1, mapper.deleteByPrimaryKey(userInfo));
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.sycros.SCWebConsole.business.service.LicenseBzSvcImpl.java

License:Open Source License

@Override
public List<LicenseBzVo> retrieveLicenseList(LicenseBzDto licenseBzDto) throws Exception {
    // TODO Auto-generated method stub
    SqlSession sqlSession = sqlSessionFactory.openSession();
    List<LicenseBzVo> licensBzVoList = null;

    StringBuffer finalQuerySql = new StringBuffer();
    String productName = null;/*from  w  w w. ja v  a  2  s .  c  om*/
    String productCount = "productCount";
    String sqlLicenseProduct = "SUM(CASE WHEN p.PRODUCT_NAME = \'%1\' THEN COUNT ELSE 0 END ) AS %2,";
    ProductEnum[] productList = ProductEnum.values();

    try {
        licenseBzDto.setDatabase(sqlSession.getConfiguration().getDatabaseId());

        int postFix = 0;
        for (ProductEnum pEnum : productList) {

            productCount = String.format("pCount%d", postFix++);
            productName = pEnum.getString();
            finalQuerySql.append(sqlLicenseProduct.replace("%1", productName).replace("%2", productCount));
        }

        finalQuerySql.deleteCharAt(finalQuerySql.length() - 1);
        logger.info("Final sql : " + finalQuerySql.toString());

        licenseBzDto.setDynamicSql(finalQuerySql.toString());
        licensBzVoList = licenseBzDao.selectLicenseList(sqlSession, licenseBzDto);

        logger.info("Result : " + licensBzVoList.size());

        for (LicenseBzVo lvo : licensBzVoList) {
            logger.info("PublishedNumber    :" + lvo.getPublishedNumber());
            logger.info("CustomerName   :" + lvo.getCustomerName());
            logger.info("ExpiryUnlimited   :" + lvo.getExpiryUnlimited());
            logger.info("ExpiryDate      :" + lvo.getExpiryDate());
            logger.info("License      :" + lvo.getLicense());
            logger.info("CreateDate      :" + lvo.getCreateDate());
            logger.info("Description   :" + lvo.getDescription());
            logger.info("ProductCountList   :" + lvo.getProductCountList());
            for (ProductEnum pEnum : productList) {
                logger.info(pEnum.getString() + " : " + lvo.getProductCountMap().get(pEnum.name()));
            }
        }
    } catch (Exception e) {
        throw e;
    } finally {
        sqlSession.close();
    }

    return licensBzVoList;
}

From source file:com.sycros.SCWebConsole.business.service.LicenseBzSvcImpl.java

License:Open Source License

@Override
public LicenseBzVo retrieveLicenseInfo(LicenseBzDto licenseBzDto) throws Exception {
    // TODO Auto-generated method stub
    SqlSession sqlSession = sqlSessionFactory.openSession();
    LicenseBzVo licensBzVoData = null;/*w w  w.jav a 2  s.  c  om*/

    StringBuffer finalQuerySql = new StringBuffer();
    String productName = null;
    String productCount = "productCount";
    String sqlLicenseProduct = "SUM(CASE WHEN p.PRODUCT_NAME = \'%1\' THEN COUNT ELSE 0 END ) AS %2,";
    ProductEnum[] productList = ProductEnum.values();

    try {

        licenseBzDto.setDatabase(sqlSession.getConfiguration().getDatabaseId());

        int postFix = 0;
        for (ProductEnum pEnum : productList) {

            productCount = String.format("pCount%d", postFix++);
            productName = pEnum.getString();
            finalQuerySql.append(sqlLicenseProduct.replace("%1", productName).replace("%2", productCount));
        }

        finalQuerySql.deleteCharAt(finalQuerySql.length() - 1);
        logger.info("Final sql : " + finalQuerySql.toString());

        licenseBzDto.setDynamicSql(finalQuerySql.toString());
        licensBzVoData = licenseBzDao.selectLicenseInfo(sqlSession, licenseBzDto);
        if (null == licensBzVoData) {
            return null;
        }

        logger.info("PublishedNumber    :" + licensBzVoData.getPublishedNumber());
        logger.info("CustomerName   :" + licensBzVoData.getCustomerName());
        logger.info("ExpiryUnlimited   :" + licensBzVoData.getExpiryUnlimited());
        logger.info("ExpiryDate      :" + licensBzVoData.getExpiryDate());
        logger.info("License      :" + licensBzVoData.getLicense());
        logger.info("CreateDate      :" + licensBzVoData.getCreateDate());
        logger.info("Description   :" + licensBzVoData.getDescription());
        logger.info("ProductCountList   :" + licensBzVoData.getProductCountList());
        for (ProductEnum pEnum : productList) {
            logger.info(pEnum.getString() + " : " + licensBzVoData.getProductCountMap().get(pEnum.name()));
        }

    } catch (Exception e) {
        throw e;
    } finally {
        sqlSession.close();
    }

    return licensBzVoData;
}