How to set database login information from a variable?

I am using MyBatis in a Java project. I know that database access information, such as host, database name, username and password, is stored in configuration.xml. But I would like to know if I can set this login data from my Java program.

This is because my application accesses different databases at different addresses, and I configure them in the application. Please help me.

+5
source share
6 answers

The xml configuration file supports the item <properties>. You can specify an external file (via an attribute resource) or define each <property>directly in xml.

mybatis mapper xml ${property}, ( ):

<properties resource="org/mybatis/example/config.properties"> 
  <property name="username" value="dev_user"/> 
  <property name="password" value="F2Fa3!33TYyg"/> 
</properties> 

<dataSource type="POOLED"> 
  <property name="driver" value="${driver}"/> 
  <property name="url" value="${url}"/> 
  <property name="username" value="${username}"/> 
  <property name="password" value="${password}"/> 
</dataSource> 

, , .

+4

, , . myBatis 3.2.1.

1. mybatis-config.xml

  • , Java, <property>.
  • <dataSource>, ${*property*} .
<?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>
    <properties>
        <property name="url" value=""/>
        <property name="username" value=""/>
        <property name="password" value=""/>
    </properties>            
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>  
            <dataSource type="POOLED">
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
    ...
    </mappers>
</configuration>

2. Java

Properties, , , build() SqlSessionFactory:

Properties properties = new Properties();
properties.setProperty("username", "myUser");
properties.setProperty("password", "myPwd");
properties.setProperty("url", "myConnectionURL");

InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, properties);
+8

, , . myBatis 3.0.3.

mybatis-config.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>
    <properties>
        <property name="url" value=""/>
        <property name="username" value=""/>
        <property name="password" value=""/>
    </properties>            
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>  
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
    ...
    </mappers>
</configuration>

Java:

Properties properties = new Properties();
properties.setProperty("username", userName);
properties.setProperty("password", password);
properties.setProperty("url", dbURL);

Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, properties);

.. myBatis 3.0.3 SqlSessionFactoryBuilder(). build() InputStream , 905686, Reader.

0

Spring 4 @Configuration Data Config

fetchSize iBatis

,

@Value("classpath:mybatis-config.xml")
private Resource myBatisResource ;
0

, BasicDataSource, .

package org.popkit.mydatasource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;

import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

public class MyDataSource extends BasicDataSource implements InitializingBean {

public Logger getParentLogger() throws SQLFeatureNotSupportedException {
    return null;
}

@Override
public void afterPropertiesSet() throws Exception {
    String url = this.getUrl();

    // following do as yourself
    if (StringUtils.isBlank(url)) {
        this.setUrl("#your jdbc.url");
    }
    String username = this.getUsername();
    if (StringUtils.isBlank(username)) {
        this.setUsername("#your jdbc.username");
    }
    String password = this.getPassword();
    if (StringUtils.isBlank(password)) {
        this.setPassword("#your jdbc.pwd");
    }
}

}

xml

<bean id="dataSource" class="org.popkit.mydatasource.MyDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value=""/>
    <property name="username" value="" />
    <property name="password" value=""/>
    <property name="initialSize" value="2" />
    <property name="maxActive" value="30" />
    <property name="maxIdle" value="10" />
    <property name="minIdle" value="3" />
    <property name="maxWait" value="30000" />
    <property name="removeAbandoned" value="true" />
    <property name="removeAbandonedTimeout" value="30" />
    <property name="validationQuery" value="SELECT 1" />
</bean>
0

First write the information about the driver, username, password in some file and read the contents of this file in your java application

Properties properties = new Properties ();

properties.load ("InputStream"); A.

-1
source

All Articles