私はJavaプロジェクトでMyBatisを使用しています。ホスト、データベース名、ユーザー名、パスワードなどのデータベースアクセス情報がに保存されていることを知っていますconfiguration.xml
。しかし、Javaプログラム内からこれらのログイン情報を設定できるかどうか知りたいです。
これは、アプリケーションがさまざまなアドレスのさまざまなデータベースにアクセスしていて、それらをアプリケーションで構成しているためです。私を助けてください。
既存の答えは同じ問題で私を助けるのに十分ではなかったので、ここに私の解決策があります。myBatis3.2.1で動作します。
mybatis-config.xml
<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>
オブジェクトを作成し、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);
構成xmlファイルは<properties>
要素をサポートします。(属性を介して)外部ファイルを指すか、xmlでそれぞれを直接resource
定義することができます。<property>
プロパティは、Mybatisのconfigファイルと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>
プロパティ要素は、ファイルで定義されているすべてのプロパティを上書きすることに注意してください。
既存の答えは同じ問題で私を助けるのに十分ではなかったので、ここに私の解決策があります。myBatis3.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の場合、user905686の回答で提案されているように、SqlSessionFactoryBuilder()。build()メソッドはInputStreamをパラメーターとして使用しないため、代わりにReaderを使用する必要があります。
DataConfigでSpring4と@Configurationを使用する場合
iBatisselectステートメントのfetchSizeを設定する方法
違いは、設定ファイルをロードする方法です
@Value("classpath:mybatis-config.xml")
private Resource myBatisResource ;
次のように、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>
まず、ドライバー、ユーザー名、パスワードに関する情報をファイルに書き込み、そのファイルの内容をJavaアプリケーションで読み取ります。
プロパティproperties=new Properties();
properties.load( "INPUTSTREAM");