配置文件mybatis-config.xml承載的配置信息過多根灯,可以將某些具體屬性值保存在properties屬性文件中,這樣也便于代碼的后期維護(hù)與管理掺栅。
示例如下:
在src中創(chuàng)建db.properties屬性文件烙肺,內(nèi)容如下:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useSSL=false
username=root
password=123456
更改mybatis-config.xml配置文件的部分代碼如下:
<configuration>
<environments default="development">
<environment id="development">
<!--事務(wù)管理配置-->
<transactionManager type="JDBC"/>
<!--數(shù)據(jù)源配置-->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
</configuration>
可以看到在mybatis-config.xml引用屬性值使用${屬性名}即可,
注意這里與SQL映射文件中#{屬性名}的區(qū)別氧卧。
#{屬性名}一般用于映射文件中取屬性值桃笙,
${}也可以用于映射文件,取變量值用來拼接SQL語句沙绝。
例如將SQL語句放入map中然后當(dāng)成變量傳入搏明,可以用${變量名}取出SQL語句,但是用#{屬性名}取值無效闪檬。
那么怎么讓mybatis-config.xml文件與db.properties文件關(guān)聯(lián)起來呢星著?
一、使用Java代碼關(guān)聯(lián)
修改Test.java代碼如下:
package test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import bean.Userinfo;
import mapping.UserinfoMapper;
public class Test {
//模擬加密算法
public static String encode(String password) {
return password="_"+password;
}
//模擬解密算法
public static String decode(String password) {
return password.substring(1);
}
public static void main(String[] args) throws IOException {
String configFile="mybatis-config.xml";
String dbInfoFile="db.properties";
InputStream configStream=Resources.getResourceAsStream(configFile);
InputStream dbinfoStream=Resources.getResourceAsStream(dbInfoFile);
Properties prop=new Properties();
prop.load(dbinfoStream);
String password=decode(prop.getProperty("password"));
prop.setProperty("password", password);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
//在這里將mybatis-config.xml的InputStream與加載了db.properties的Properties進(jìn)行關(guān)聯(lián)
SqlSessionFactory factory=builder.build(configStream,prop);
SqlSession session=factory.openSession();
UserinfoMapper mapper=session.getMapper(UserinfoMapper.class);
System.out.println("數(shù)據(jù)庫信息來自于properties文件粗悯!");
Userinfo userinfo=mapper.selectByPrimaryKey(1);
System.out.println(userinfo.getId()+" "+userinfo.getUsername()+" "+userinfo.getPassword()+" "+userinfo.getAge());
session.commit();
session.close();
}
}
可以看到這里還加入了模擬加密解密虚循,所以我們這里將db.properties屬性文件修改如下:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useSSL=false
username=root
password=_123456
二、在mybatis-config.xml中進(jìn)行關(guān)聯(lián)
在配置文件mybatis-config.xml中添加<properties resource="db.properties"></properties>
代碼如下:
<?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 resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<!--事務(wù)管理配置-->
<transactionManager type="JDBC"/>
<!--數(shù)據(jù)源配置-->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--這里的resource對(duì)應(yīng)上一步自動(dòng)生成的mapping/UserinfoMapper.xml文件-->
<mapper resource="mapping/UserinfoMapper.xml"/>
</mappers>
</configuration>
Test.java測試代碼中保留了模擬加密解密部分代碼,因?yàn)樵趍ybatis-config.xml中進(jìn)行了關(guān)聯(lián)横缔,將
SqlSessionFactory factory=builder.build(configStream,prop);
修改為
SqlSessionFactory factory=builder.build(configStream);
完整代碼如下:
package test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import bean.Userinfo;
import mapping.UserinfoMapper;
public class Test {
//模擬加密算法
public static String encode(String password) {
return password="_"+password;
}
//模擬解密算法
public static String decode(String password) {
return password.substring(1);
}
public static void main(String[] args) throws IOException {
String configFile="mybatis-config.xml";
String dbInfoFile="db.properties";
InputStream configStream=Resources.getResourceAsStream(configFile);
InputStream dbinfoStream=Resources.getResourceAsStream(dbInfoFile);
Properties prop=new Properties();
prop.load(dbinfoStream);
String password=decode(prop.getProperty("password"));
prop.setProperty("password", password);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
//
SqlSessionFactory factory=builder.build(configStream);
SqlSession session=factory.openSession();
UserinfoMapper mapper=session.getMapper(UserinfoMapper.class);
System.out.println("數(shù)據(jù)庫信息來自于properties文件铺遂!");
Userinfo userinfo=mapper.selectByPrimaryKey(1);
System.out.println(userinfo.getId()+" "+userinfo.getUsername()+" "+userinfo.getPassword()+" "+userinfo.getAge());
session.commit();
session.close();
}
}
OutPut:
數(shù)據(jù)庫信息來自于properties文件!
1 usernameValue passwordValue 80
相關(guān)文章:配置多個(gè)environment環(huán)境