2

我正在尝试使用 MyBatis 将小型 Java 应用程序与数据库连接起来。

XML 文件:

<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
                <property name="url" value="jdbc:oracle:thin:@xxxx:xxxx:xxxx"/>
                <property name="username" value="xxxxx"/>
                <property name="password" value="xxxxx"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="xml/Mapper.xml"/>
    </mappers>
</configuration>

之前,我得到的会话如下

String resource = "Configuration.xml";
SqlSession session = null;
try{
    Reader reader = Resources.getResourceAsReader(resource);
    SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader); 
    session = sqlMapper.openSession();

我想在不使用 xml 文件的情况下连接到数据库。任何帮助将不胜感激。

4

3 回答 3

1

你检查过 MyBatis 3 用户指南吗?有一个部分称为“在没有 XML 的情况下构建 SqlSessionFactory”。也不需要使用基于 XML 的映射,您可以使用基于注释的语句映射并完全避免 XML 配置。

于 2013-06-21T12:35:19.943 回答
0

这对我有用:

import javax.sql.DataSource;

import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;


String user = "xxxxx";
String password = "xxxxx";
String databasenameURL = "jdbc:oracle:thin:@xxxx:xxxx:xxxx";
String dbDriver = "oracle.jdbc.driver.OracleDriver";


DataSource dataSource = new org.apache.ibatis.datasource.pooled.PooledDataSource(
        dbDriver, databasenameURL, user, password);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development",
        transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
        .build(configuration);
于 2016-03-09T13:25:37.600 回答
-1

也许为时已晚,但对于未来的读者来说。

您可以将 xml 解析为字符串并稍后修改您想要的任何值。:

这对我有用:

    import org.apache.commons.io.IOUtils;
    import org.apache.commons.io.input.CharSequenceReader;
    import org.apache.commons.lang3.StringUtils;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.apache.log4j.Logger;

    import java.io.IOException;
    import java.io.Reader;

    public class MySqlSessionFactory
    {
        private static Logger log = Logger.getLogger(MySqlSessionFactory.class);
        private static SqlSessionFactory sessionFactory;

        public static SqlSessionFactory getSqlSessionFactory()
        {
            return sessionFactory;
        }

        /**
         * Initialize SqlSessionFactory instance, to be used later in all the project
         *
         * @return boolean if sqlSessionFactory is build
         */
        public static boolean initializeMySqlSessionFactory() throws IOException
        {
            log.trace("Enter method initializeMySqlSessionFactory.");
            String resource = "mybatis-config.xml";
            boolean result;
            Reader reader = null;
            Reader parsedReader = null;
            try
            {
                reader = Resources.getResourceAsReader(resource);
                String parsedXMLConfig = IOUtils.toString(reader);

                parsedXMLConfig = StringUtils.replace(parsedXMLConfig, "${jdbcUrl}", "databaseURL");
                parsedXMLConfig = StringUtils.replace(parsedXMLConfig, "${username}", "databaseUser");
                parsedXMLConfig = StringUtils.replace(parsedXMLConfig, "${password}", "databasePassword");
                parsedReader = new CharSequenceReader(parsedXMLConfig);

                sessionFactory = new SqlSessionFactoryBuilder().build(parsedReader);
                result = true;
            }
            catch(IOException e)
            {
                result = false;
                log.error("Error calling initializeMySqlSessionFactory.", e);
                throw new IOException(e);
            }
            finally
            {

                if(reader != null )
                {
                    reader.close();
                }
                if(parsedReader != null )
                {
                    parsedReader.close();
                }

            }
            log.trace("Exit method initializeMySqlSessionFactory. Method result: " + result);
            return result;
        }

    }

同样通过这种方式,您可以更改任何您想要的指定字段...并注意在 xml 文件中放置相同的占位符(${jdbcUrl}、${username}、${password})

于 2018-10-19T09:14:00.330 回答