Example usage for org.apache.ibatis.session Configuration Configuration

List of usage examples for org.apache.ibatis.session Configuration Configuration

Introduction

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

Prototype

public Configuration(Environment environment) 

Source Link

Usage

From source file:org.mybatis.scripting.freemarker.CustomizedDataContextTest.java

License:Apache License

@BeforeAll
public static void setUp() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");

    JDBCDataSource dataSource = new JDBCDataSource();
    dataSource.setUrl("jdbc:hsqldb:mem:db4");
    dataSource.setUser("sa");
    dataSource.setPassword("");

    try (Connection conn = dataSource.getConnection()) {
        try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
            ScriptRunner runner = new ScriptRunner(conn);
            runner.setLogWriter(null);//from   ww w  . j  ava 2  s .  com
            runner.setErrorLogWriter(null);
            runner.runScript(reader);
            conn.commit();
        }
    }

    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, dataSource);

    // You can call configuration.setDefaultScriptingLanguage(FreeMarkerLanguageDriver.class)
    // after this to use FreeMarker driver by default.
    Configuration configuration = new Configuration(environment);

    configuration.addMapper(CustomizedDataContextMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}

From source file:org.mybatis.scripting.freemarker.FreeMarkerInAnnotationsTest.java

License:Apache License

@BeforeAll
public static void setUp() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");

    JDBCDataSource dataSource = new JDBCDataSource();
    dataSource.setUrl("jdbc:hsqldb:mem:db1");
    dataSource.setUser("sa");
    dataSource.setPassword("");

    try (Connection conn = dataSource.getConnection()) {
        try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
            ScriptRunner runner = new ScriptRunner(conn);
            runner.setLogWriter(null);/* w ww  . j  a va  2  s .  c o  m*/
            runner.setErrorLogWriter(null);
            runner.runScript(reader);
            conn.commit();
        }
    }

    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, dataSource);

    // You can call configuration.setDefaultScriptingLanguage(FreeMarkerLanguageDriver.class)
    // after this to use FreeMarker driver by default.
    Configuration configuration = new Configuration(environment);

    configuration.addMapper(NameMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}

From source file:org.mybatis.scripting.freemarker.PreparedParamsTest.java

License:Apache License

@BeforeAll
public static void setUp() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");

    JDBCDataSource dataSource = new JDBCDataSource();
    dataSource.setUrl("jdbc:hsqldb:mem:db3");
    dataSource.setUser("sa");
    dataSource.setPassword("");

    try (Connection conn = dataSource.getConnection()) {
        try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
            ScriptRunner runner = new ScriptRunner(conn);
            runner.setLogWriter(null);// ww w.  j  a  v a2 s  .  c  om
            runner.setErrorLogWriter(null);
            runner.runScript(reader);
            conn.commit();
        }
    }

    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, dataSource);

    // You can call configuration.setDefaultScriptingLanguage(FreeMarkerLanguageDriver.class)
    // after this to use FreeMarker driver by default.
    Configuration configuration = new Configuration(environment);

    configuration.addMapper(PreparedParamsMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}

From source file:org.neo4j.jdbc.example.mybatis.MybatisTest.java

License:Apache License

protected void buildMybatisConfiguration(String protocol, String host, int port) {
    DataSource dataSource = new UnpooledDataSource("org.neo4j.jdbc.Driver",
            "jdbc:neo4j:" + protocol + "://" + host + ":" + port + "?noSsl", null);
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, dataSource);

    Configuration configuration = new Configuration(environment);
    configuration.getMapperRegistry().addMapper(ActorMapper.class);
    configuration.addLoadedResource("org/neo4j/jdbc/example/mybatis/mapper/ActorMapper.xml");

    ConnectionFactory.getSqlSessionFactory(configuration);
}

From source file:org.snaker.engine.access.mybatis.MybatisHelper.java

License:Apache License

/**
 * DataSource?SqlSessionFactory/* w  w  w  .  j  a va 2  s  .  c o m*/
 * @param ds ??
 */
public static void initialize(DataSource ds) {
    TransactionFactory transactionFactory = new MybatisTransactionFactory();
    Environment environment = new Environment("snaker", transactionFactory, ds);
    Configuration configuration = new Configuration(environment);
    configuration.getTypeAliasRegistry().registerAliases(SCAN_PACKAGE, Object.class);
    if (log.isInfoEnabled()) {
        Map<String, Class<?>> typeAliases = configuration.getTypeAliasRegistry().getTypeAliases();
        for (Entry<String, Class<?>> entry : typeAliases.entrySet()) {
            log.info("Scanned class:[name=" + entry.getKey() + ",class=" + entry.getValue().getName() + "]");
        }
    }
    try {
        for (String resource : resources) {
            InputStream in = Resources.getResourceAsStream(resource);
            XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, resource,
                    configuration.getSqlFragments());
            xmlMapperBuilder.parse();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        ErrorContext.instance().reset();
    }
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}

From source file:org.workspace7.osgi.mybatis.extender.impl.MyBatisMapperRegistry.java

License:Apache License

public void registerBundle(Bundle bundle) {

    Map<String, List<String>> dsMappers = ExtenderUtil.getMapperClauses(bundle);

    for (String dsName : dsMappers.keySet()) {

        Environment environment = registeredEnvironments.get(dsName);

        Configuration configuration = new Configuration(environment);

        if (configuration != null) {

            List<String> packageNames = dsMappers.get(dsName);

            for (String packageName : packageNames) {

                String cleanPackageName = packageName.trim();

                logger.info("Adding mappers and xmls from  package {} to Configuration {}", cleanPackageName,
                        dsName);//from ww  w .  j a v  a 2 s. co m

                configuration.addMappers(cleanPackageName);

                Enumeration<URL> mapperXmls = ExtenderUtil.findFilesFromBundle(bundle, packageName, "*.xml");

                if (mapperXmls != null) {
                    while (mapperXmls.hasMoreElements()) {

                        URL mapperxmlUrl = mapperXmls.nextElement();

                        try {

                            String mappingFile = ExtenderUtil.mappingFileName(mapperxmlUrl.getFile());

                            logger.info("Adding Mapper XML {} to Config {} ", mappingFile, dsName);

                            InputStream in = mapperxmlUrl.openStream();

                            XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration,
                                    mappingFile, configuration.getSqlFragments());
                            xmlMapperBuilder.parse();

                            in.close();

                            Collection<String> mappedStatements = configuration.getMappedStatementNames();
                            for (String mappedStmt : mappedStatements) {
                                logger.info(" Added Mapped Statement: {}", mappedStmt);
                            }

                            unregisterSqlSessionFactory(dsName);

                            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                                    .build(configuration);
                            Dictionary<String, String> props = new Hashtable<>();
                            props.put("dataSourceName", dsName);
                            ServiceRegistration serviceRegistration = bundleContext.registerService(
                                    SqlSessionFactory.class.getName(), sqlSessionFactory, props);

                            registeredSqlSessionFactories.put(dsName, serviceRegistration);

                        } catch (IOException e) {
                            logger.error("Unable to add mapper {} ", mapperxmlUrl.toString(), e);

                        }
                    }
                }
            }
        }
    }

    logger.info("Registering Mapper Bundle {} ", bundle.getSymbolicName());

    activeBundles.add(bundle);

}