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

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

Introduction

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

Prototype

Connection getConnection();

Source Link

Document

Retrieves inner database connection.

Usage

From source file:cn.com.git.udmp.test.mybatis.cursor.UdmpCursorSimpleTest.java

License:Apache License

@BeforeClass
public static void setUp() throws Exception {
    // create a SqlSessionFactory
    Reader reader = Resources.getResourceAsReader("cn/com/git/udmp/test/mybatis/cursor/mybatis-config.xml");
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    reader.close();//w ww  .  j  a v a 2 s. c  o m

    // populate in-memory database
    SqlSession session = sqlSessionFactory.openSession();
    Connection conn = session.getConnection();
    reader.close();
    session.close();
}

From source file:com.gf.components.mybatis.SqlSessionInInvoke.java

License:Apache License

@Override
public void invoke(Action<?, ?> action, FilterChain chain) throws Exception {

    SqlSessionSettings settings = findSettingsInNextObjects();
    SqlSession session = createSession(settings);
    try {//from   w w  w  .  ja v a2s. c  o m

        Connection connection = session.getConnection();

        addToInvocationContext(session);
        addToInvocationContext(connection);
        chain.doNext();

        session.flushStatements();
        session.commit();

    } catch (Exception e) {
        session.rollback();
        throw e;
    } finally {
        try {
            session.close();
        } catch (Exception ex) {
            log.error("can't close session", ex);
        }
    }

}

From source file:com.google.enterprise.connector.db.DBClient.java

License:Apache License

private void checkDBConnection(SqlSession session, Exception e) throws SnapshotRepositoryRuntimeException {
    /*// ww  w .j a  v  a  2 s  .co m
     * Below code is added to handle scenarios when table is deleted or
     * connectivity with database is lost. In this scenario first check
     * the SQLState of the supplied Exception.  If it has no SQLState,
     * check the connectivity with database and if there is no connectivity,
     * throw a SnapshotRepositoryRuntimeException, otherwise if the SQLState
     * indicates a syntax error (which a dropped table or changed primary
     * key will show up as) allow the connector to continue as if there was
     * no data available.  If the SQLState is anything other than a syntax
     * error, throw a SnapshotRepositoryRuntimeException.
     */
    SQLException sqlException;
    if (e instanceof SQLException) {
        sqlException = (SQLException) e;
    } else if (e.getCause() != null && e.getCause() instanceof SQLException) {
        sqlException = (SQLException) e.getCause();
    } else {
        // It is not even a SQLException. Something else is wrong, so propagate
        // the error.
        throw new SnapshotRepositoryRuntimeException(e.getMessage(), e);
    }
    String sqlState = sqlException.getSQLState();
    if (sqlState != null) {
        // Look for SQL syntax errors, both ISO style and XOpen style.
        if (sqlState.startsWith("42") || sqlState.startsWith("S0") || sqlState.startsWith("37")) {
            LOG.log(Level.WARNING, "Could not execute SQL query on the database.", sqlException);
            // Swallow the exception.
        } else if (sqlState.startsWith("08")) { // Connection errors.
            LOG.log(Level.WARNING, "Unable to connect to the database.", sqlException);
            throw new SnapshotRepositoryRuntimeException("Unable to connect to the database.", sqlException);
        } else {
            throw new SnapshotRepositoryRuntimeException(sqlException.getMessage(), sqlException);
        }
    } else {
        // No SQLState to consider. Check connectivity with DB.
        Connection conn = null;
        try {
            conn = session.getConnection();
            LOG.log(Level.WARNING, "Could not execute SQL query on the database.", e);
            // Swallow the exception.
        } catch (RuntimeException e1) {
            Throwable cause = (e1.getCause() != null && e1.getCause() instanceof SQLException) ? e1.getCause()
                    : e1;
            LOG.log(Level.WARNING, "Unable to connect to the database", cause);
            throw new SnapshotRepositoryRuntimeException("Unable to connect to the database.", cause);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e1) {
                    LOG.fine("Could not close database connection: " + e1.toString());
                }
            }
        }
    }
}

From source file:com.google.enterprise.connector.db.DBClient.java

License:Apache License

/**
 * Returns information derived from the DatabaseMetaData.
 *
 * @param metaDataHandler a Function that takes a DatabaseMetaData as input
 *        and returns a value/*w ww  .j  a v a2 s.c  om*/
 * @return the value returned by the metaDataHandler Function, or null if
 *        there was an error
 */
public <T> T getDatabaseMetaData(SqlFunction<DatabaseMetaData, T> metaDataHandler) {
    try {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            Connection conn = session.getConnection();
            try {
                DatabaseMetaData meta = conn.getMetaData();
                if (meta != null) {
                    return metaDataHandler.apply(meta);
                }
            } finally {
                conn.close();
            }
        } finally {
            session.close();
        }
    } catch (SQLException e) {
        LOG.warning("Caught SQLException while fetching database details: " + e);
    } catch (Exception e) {
        LOG.warning("Caught Exception while fetching database details: " + e);
    }
    return null;
}

From source file:com.google.enterprise.connector.db.DBConnectorTypeTest.java

License:Apache License

/**
 * Executes the given query and applies the given function to the
 * result set.//from  w w w. j a  v  a2  s . c  om
 *
 * @return the value of the function applied to the {@code ResultSet}
 */
private <T> T applyResultSet(String sqlQuery, DBClient.SqlFunction<ResultSet, T> function) throws SQLException {
    SqlSession sqlSession = getDbClient().getSqlSession();
    try {
        Connection dbConnection = sqlSession.getConnection();
        try {
            Statement stmt = dbConnection.createStatement();
            try {
                ResultSet resultSet = stmt.executeQuery(sqlQuery);
                try {
                    return function.apply(resultSet);
                } finally {
                    resultSet.close();
                }
            } finally {
                stmt.close();
            }
        } finally {
            dbConnection.close();
        }
    } finally {
        sqlSession.close();
    }
}

From source file:com.itfsw.mybatis.generator.plugins.BugFixedTest.java

License:Apache License

/**
 * ?SelectiveEnhancedPlugintypeHandler/*  w w w.java  2  s . c  om*/
 * @throws Exception
 */
@Test
public void bug0003() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BugFixedTest/bug-0003.xml");
    tool.generate(() -> DBHelper.createDB("scripts/BugFixedTest/bug-0003.sql"), new AbstractShellCallback() {
        @Override
        public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
            ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));

            ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");

            tb.set("id", 1L);
            tb.set("field1", new SimpleDateFormat("yyyy-MM-dd").parse("2019-07-08"));

            tbMapper.invoke("updateByPrimaryKey", tb.getObject());
            ResultSet rs = DBHelper.execute(sqlSession.getConnection(), "select field1 from tb where id = 1");
            rs.first();
            Assert.assertEquals(rs.getString("field1"), "2019:07:08");

            tb.set("field1", new SimpleDateFormat("yyyy-MM-dd").parse("2019-07-09"));
            ObjectUtil tbColumnField1 = new ObjectUtil(loader, packagz + ".Tb$Column#field1");
            Object columns = Array.newInstance(tbColumnField1.getCls(), 1);
            Array.set(columns, 0, tbColumnField1.getObject());

            tbMapper.invoke("updateByPrimaryKeySelective", tb.getObject(), columns);
            rs = DBHelper.execute(sqlSession.getConnection(), "select field1 from tb where id = 1");
            rs.first();
            Assert.assertEquals(rs.getString("field1"), "2019:07:09");
        }
    });
}

From source file:com.itfsw.mybatis.generator.plugins.BugFixedTest.java

License:Apache License

/**
 * ?????SelectiveEnhancedPlugin?selective?
 * https://github.com/itfsw/mybatis-generator-plugin/issues/69
 * @throws Exception//from  w  w  w .  j a  va 2  s  .  c  o m
 */
@Test
public void issues69() throws Exception {
    for (int i = 0; i < 2; i++) {
        MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BugFixedTest/issues-69-" + i + ".xml");
        tool.generate(() -> DBHelper.createDB("scripts/BugFixedTest/issues-69.sql"),
                new AbstractShellCallback() {
                    @Override
                    public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz)
                            throws Exception {
                        ObjectUtil tbMapper = new ObjectUtil(
                                sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));

                        ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
                        ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
                        criteria.invoke("andIdEqualTo", 1l);

                        ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");
                        tb.set("id", 1L);
                        tb.set("version", 152L);
                        tb.set("incF2", 10L);
                        tb.set("incF3", 5L);

                        // selective
                        ObjectUtil TbColumnId = new ObjectUtil(loader, packagz + ".Tb$Column#id");
                        ObjectUtil TbColumnVersion = new ObjectUtil(loader, packagz + ".Tb$Column#version");
                        ObjectUtil TbColumnIncF2 = new ObjectUtil(loader, packagz + ".Tb$Column#incF2");
                        Object columns = Array.newInstance(TbColumnId.getCls(), 3);
                        Array.set(columns, 0, TbColumnId.getObject());
                        Array.set(columns, 1, TbColumnVersion.getObject());
                        Array.set(columns, 2, TbColumnIncF2.getObject());

                        // sql
                        String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(),
                                "updateWithVersionByExampleSelective", 100L, tb.getObject(),
                                tbExample.getObject(), columns);
                        Assert.assertEquals(sql,
                                "update tb SET version = version + 1, id = 1 , inc_f2 = 10 WHERE version = 100 and ( ( id = '1' ) )");

                        // ?100?0
                        Object result = tbMapper.invoke("updateWithVersionByExampleSelective", 100L,
                                tb.getObject(), tbExample.getObject(), columns);
                        Assert.assertEquals(result, 0);

                        // id = 1 ?0
                        result = tbMapper.invoke("updateWithVersionByExampleSelective", 0L, tb.getObject(),
                                tbExample.getObject(), columns);
                        Assert.assertEquals(result, 1);

                        // ???1
                        ResultSet rs = DBHelper.execute(sqlSession.getConnection(),
                                "select * from tb where id = 1");
                        rs.first();
                        Assert.assertEquals(rs.getInt("version"), 1);
                    }
                });
    }
}

From source file:com.itfsw.mybatis.generator.plugins.IncrementPluginTest.java

License:Apache License

/**
 * ?sql//from   w ww .  ja v a2 s.  c  o m
 */
@Test
public void testSqlAndExecute() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementPlugin/mybatis-generator.xml");
    tool.generate(() -> DBHelper.createDB("scripts/IncrementPlugin/init.sql"), new AbstractShellCallback() {
        @Override
        public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {

            // 1. updateByExample?updateByExampleSelective
            ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));

            ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
            ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
            criteria.invoke("andIdEqualTo", 3l);

            ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");
            ObjectUtil tbColumnIncF1 = new ObjectUtil(loader, packagz + ".Tb$Column#incF1");
            Object tbR = tb.invoke("increment", tbColumnIncF1.invoke("inc", 100L));

            Assert.assertEquals(tb.getObject().getClass(), tbR.getClass());

            // sql
            String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "updateByExample", tb.getObject(),
                    tbExample.getObject());
            Assert.assertEquals(sql,
                    "update tb set id = null, field1 = 'null', inc_f1 = inc_f1 + 100, inc_f2 = null, inc_f3 = null WHERE ( id = '3' )");
            sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "updateByExampleSelective", tb.getObject(),
                    tbExample.getObject());
            Assert.assertEquals(sql, "update tb SET inc_f1 = inc_f1 + 100 WHERE ( id = '3' )");
            // 
            // inc_f1 100
            Object result = tbMapper.invoke("updateByExampleSelective", tb.getObject(), tbExample.getObject());
            Assert.assertEquals(result, 1);
            ResultSet rs = DBHelper.execute(sqlSession.getConnection(), "select inc_f1 from tb where id = 3");
            rs.first();
            Assert.assertEquals(rs.getInt("inc_f1"), 103);

            // inc_f1 ??50
            tb.invoke("increment", tbColumnIncF1.invoke("dec", 50L));
            result = tbMapper.invoke("updateByExampleSelective", tb.getObject(), tbExample.getObject());
            Assert.assertEquals(result, 1);
            // ?
            rs = DBHelper.execute(sqlSession.getConnection(), "select inc_f1 from tb where id = 3");
            rs.first();
            Assert.assertEquals(rs.getInt("inc_f1"), 53);

            // 2. updateByPrimaryKey?updateByPrimaryKeySelective
            ObjectUtil tbKeysMapper = new ObjectUtil(
                    sqlSession.getMapper(loader.loadClass(packagz + ".TbKeysMapper")));

            ObjectUtil tbKeysColumnIncF1 = new ObjectUtil(loader, packagz + ".TbKeys$Column#incF1");
            ObjectUtil tbKeysColumnIncF3 = new ObjectUtil(loader, packagz + ".TbKeys$Column#incF3");

            ObjectUtil tbKeys = new ObjectUtil(loader, packagz + ".TbKeys");
            tbKeys.set("key1", 1l);
            tbKeys.set("key2", "k1");
            tbKeys.invoke("increment", tbKeysColumnIncF1.invoke("inc", 10L));
            tbKeys.invoke("increment", tbKeysColumnIncF3.invoke("inc", 30L));

            // sql
            sql = SqlHelper.getFormatMapperSql(tbKeysMapper.getObject(), "updateByPrimaryKey",
                    tbKeys.getObject());
            Assert.assertEquals(sql,
                    "update tb_keys set field1 = 'null', field2 = null, inc_f1 = inc_f1 + 10, inc_f2 = null, inc_f3 = inc_f3 + 30 where key1 = 1 and key2 = 'k1'");
            sql = SqlHelper.getFormatMapperSql(tbKeysMapper.getObject(), "updateByPrimaryKeySelective",
                    tbKeys.getObject());
            Assert.assertEquals(sql,
                    "update tb_keys SET inc_f1 = inc_f1 + 10, inc_f3 = inc_f3 + 30 where key1 = 1 and key2 = 'k1'");
            // 
            result = tbKeysMapper.invoke("updateByPrimaryKeySelective", tbKeys.getObject());
            Assert.assertEquals(result, 1);
            // ?
            rs = DBHelper.execute(sqlSession.getConnection(),
                    "select inc_f1, inc_f3 from tb_keys where key1 = 1 and key2 = 'k1'");
            rs.first();
            Assert.assertEquals(rs.getInt("inc_f1"), 11);
            Assert.assertEquals(rs.getInt("inc_f3"), 33);

            // 3. updateByExampleWithBLOBs?updateByPrimaryKeyWithBLOBs
            ObjectUtil tbBlobsMapper = new ObjectUtil(
                    sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper")));

            ObjectUtil tbBlobsExample = new ObjectUtil(loader, packagz + ".TbBlobsExample");
            ObjectUtil tbBlobsExampleCriteria = new ObjectUtil(tbBlobsExample.invoke("createCriteria"));
            tbBlobsExampleCriteria.invoke("andIdEqualTo", 3l);

            ObjectUtil tbBlobsWithBLOBs = new ObjectUtil(loader, packagz + ".TbBlobsWithBLOBs");
            ObjectUtil tbBlobsWithBLOBsIncF1 = new ObjectUtil(loader,
                    packagz + ".TbBlobsWithBLOBs$Column#incF1");
            tbBlobsWithBLOBs.invoke("increment", tbBlobsWithBLOBsIncF1.invoke("inc", 100L));

            tbBlobsWithBLOBs.set("incF2", 50L);
            tbBlobsWithBLOBs.set("field3", "blob");

            // sql
            sql = SqlHelper.getFormatMapperSql(tbBlobsMapper.getObject(), "updateByExampleWithBLOBs",
                    tbBlobsWithBLOBs.getObject(), tbBlobsExample.getObject());
            Assert.assertEquals(sql,
                    "update tb_blobs set id = null, field1 = 'null', inc_f1 = inc_f1 + 100, inc_f2 = 50, inc_f3 = null, field2 = 'null', field3 = 'blob' WHERE ( id = '3' )");

            tbBlobsWithBLOBs.set("id", 3l);
            sql = SqlHelper.getFormatMapperSql(tbBlobsMapper.getObject(), "updateByPrimaryKeyWithBLOBs",
                    tbBlobsWithBLOBs.getObject());
            Assert.assertEquals(sql,
                    "update tb_blobs set field1 = 'null', inc_f1 = inc_f1 + 100, inc_f2 = 50, inc_f3 = null, field2 = 'null', field3 = 'blob' where id = 3");

            // 
            tbBlobsWithBLOBs.set("incF3", 10l);
            // ??
            sql = SqlHelper.getFormatMapperSql(tbBlobsMapper.getObject(), "updateByPrimaryKeyWithBLOBs",
                    tbBlobsWithBLOBs.getObject());
            Assert.assertEquals(sql,
                    "update tb_blobs set field1 = 'null', inc_f1 = inc_f1 + 100, inc_f2 = 50, inc_f3 = 10, field2 = 'null', field3 = 'blob' where id = 3");
            result = tbBlobsMapper.invoke("updateByPrimaryKeyWithBLOBs", tbBlobsWithBLOBs.getObject());
            Assert.assertEquals(result, 1);
        }
    });
}

From source file:com.itfsw.mybatis.generator.plugins.IncrementPluginTest.java

License:Apache License

/**
 * ? SelectiveEnhancedPlugin ?/*from   www  .j a  v  a  2s . c  om*/
 */
@Test
public void testWithSelectiveEnhancedPlugin() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool
            .create("scripts/IncrementPlugin/mybatis-generator-with-selective-enhanced-plugin.xml");
    tool.generate(() -> DBHelper.createDB("scripts/IncrementPlugin/init.sql"), new AbstractShellCallback() {
        @Override
        public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
            // 1. updateByExampleSelective
            ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));

            ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
            ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
            criteria.invoke("andIdEqualTo", 3l);

            ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");
            ObjectUtil tbColumnIncF1 = new ObjectUtil(loader, packagz + ".Tb$Column#incF1");

            tb.invoke("increment", tbColumnIncF1.invoke("inc", 100L));
            tb.set("incF2", 200l);

            // selective
            ObjectUtil TbColumnField1 = new ObjectUtil(loader, packagz + ".Tb$Column#field1");
            ObjectUtil TbColumnIncF2 = new ObjectUtil(loader, packagz + ".Tb$Column#incF2");
            Object columns = Array.newInstance(TbColumnField1.getCls(), 3);
            Array.set(columns, 0, TbColumnField1.getObject());
            Array.set(columns, 1, tbColumnIncF1.getObject());
            Array.set(columns, 2, TbColumnIncF2.getObject());

            // sql
            // ?
            String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "updateByExampleSelective",
                    tb.getObject(), tbExample.getObject());
            Assert.assertEquals(sql, "update tb SET inc_f1 = inc_f1 + 100, inc_f2 = 200 WHERE ( id = '3' )");
            // selective 
            sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "updateByExampleSelective", tb.getObject(),
                    tbExample.getObject(), columns);
            Assert.assertEquals(sql,
                    "update tb SET field1 = 'null' , inc_f1 = inc_f1 + 100 , inc_f2 = 200 WHERE ( id = '3' )");
            // 
            // inc_f1 100
            Object result = tbMapper.invoke("updateByExampleSelective", tb.getObject(), tbExample.getObject(),
                    columns);
            Assert.assertEquals(result, 1);
            ResultSet rs = DBHelper.execute(sqlSession.getConnection(), "select inc_f1 from tb where id = 3");
            rs.first();
            Assert.assertEquals(rs.getInt("inc_f1"), 103);

            // inc_f1 ??50
            tb.invoke("increment", tbColumnIncF1.invoke("dec", 50L));
            result = tbMapper.invoke("updateByExampleSelective", tb.getObject(), tbExample.getObject(),
                    Array.newInstance(TbColumnField1.getCls(), 0));
            Assert.assertEquals(result, 1);
            // ?
            rs = DBHelper.execute(sqlSession.getConnection(), "select inc_f1 from tb where id = 3");
            rs.first();
            Assert.assertEquals(rs.getInt("inc_f1"), 53);

            // 2. updateByPrimaryKeySelective
            ObjectUtil tbKeysMapper = new ObjectUtil(
                    sqlSession.getMapper(loader.loadClass(packagz + ".TbKeysMapper")));

            ObjectUtil tbKeys = new ObjectUtil(loader, packagz + ".TbKeys");
            ObjectUtil tbKeysColumnIncF1 = new ObjectUtil(loader, packagz + ".TbKeys$Column#incF1");
            ObjectUtil tbKeysColumnIncF3 = new ObjectUtil(loader, packagz + ".TbKeys$Column#incF3");
            tbKeys.set("key1", 1l);
            tbKeys.set("key2", "k1");
            tbKeys.invoke("increment", tbKeysColumnIncF1.invoke("inc", 10L));
            tbKeys.invoke("increment", tbKeysColumnIncF3.invoke("inc", 30L));

            // selective
            ObjectUtil TbColumnKey1 = new ObjectUtil(loader, packagz + ".TbKeys$Column#key1");
            tbColumnIncF1 = new ObjectUtil(loader, packagz + ".TbKeys$Column#incF1");
            columns = Array.newInstance(TbColumnKey1.getCls(), 2);
            Array.set(columns, 0, TbColumnKey1.getObject());
            Array.set(columns, 1, tbColumnIncF1.getObject());

            // sql
            // ?
            sql = SqlHelper.getFormatMapperSql(tbKeysMapper.getObject(), "updateByPrimaryKeySelective",
                    tbKeys.getObject());
            Assert.assertEquals(sql,
                    "update tb_keys SET inc_f1 = inc_f1 + 10, inc_f3 = inc_f3 + 30 where key1 = 1 and key2 = 'k1'");
            // selective 
            sql = SqlHelper.getFormatMapperSql(tbKeysMapper.getObject(), "updateByPrimaryKeySelective",
                    tbKeys.getObject(), columns);
            Assert.assertEquals(sql,
                    "update tb_keys SET key1 = 1 , inc_f1 = inc_f1 + 10 where key1 = 1 and key2 = 'k1'");
            // 
            result = tbKeysMapper.invoke("updateByPrimaryKeySelective", tbKeys.getObject(), columns);
            Assert.assertEquals(result, 1);
            // ?
            rs = DBHelper.execute(sqlSession.getConnection(),
                    "select inc_f1, inc_f3 from tb_keys where key1 = 1 and key2 = 'k1'");
            rs.first();
            Assert.assertEquals(rs.getInt("inc_f1"), 11);
        }
    });
}

From source file:com.itfsw.mybatis.generator.plugins.IncrementPluginTest.java

License:Apache License

/**
 *  autoDelimitKeywords//from   w w  w. jav a  2  s  .c om
 */
@Test
public void testWithAutoDelimitKeywords() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool
            .create("scripts/IncrementPlugin/mybatis-generator-with-autoDelimitKeywords.xml");
    tool.generate(() -> DBHelper.createDB("scripts/IncrementPlugin/init.sql"), new AbstractShellCallback() {
        @Override
        public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
            // 1. updateByExample?updateByExampleSelective
            ObjectUtil tbKeyWordMapper = new ObjectUtil(
                    sqlSession.getMapper(loader.loadClass(packagz + ".TbKeyWordMapper")));

            ObjectUtil TbKeyWordExample = new ObjectUtil(loader, packagz + ".TbKeyWordExample");
            ObjectUtil criteria = new ObjectUtil(TbKeyWordExample.invoke("createCriteria"));
            criteria.invoke("andIdEqualTo", 1l);

            ObjectUtil tbKeyWord = new ObjectUtil(loader, packagz + ".TbKeyWord");
            ObjectUtil tbKeyWordColumnUpdate = new ObjectUtil(loader, packagz + ".TbKeyWord$Column#update");
            tbKeyWord.invoke("increment", tbKeyWordColumnUpdate.invoke("inc", 100L));

            // 
            // inc_f1 100
            Object result = tbKeyWordMapper.invoke("updateByExampleSelective", tbKeyWord.getObject(),
                    TbKeyWordExample.getObject());
            Assert.assertEquals(result, 1);
            // ?
            ResultSet rs = DBHelper.execute(sqlSession.getConnection(),
                    "select * from tb_key_word where id = 1");
            rs.first();
            Assert.assertEquals(rs.getLong("update"), 101);
        }
    });
}