配置文件mybatis-config.xml承载的配置信息过多,可以将某些具体属性值保存在properties属性文件中,这样也便于代码的后期维护与管理。
示例如下:
在src中创建db.properties属性文件,内容如下:
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">
<!--事务管理配置-->
<transactionManager type="JDBC"/>
<!--数据源配置-->
<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映射文件中#{属性名}的区别。
#{属性名}一般用于映射文件中取属性值,
${}也可以用于映射文件,取变量值用来拼接SQL语句。
例如将SQL语句放入map中然后当成变量传入,可以用${变量名}取出SQL语句,但是用#{属性名}取值无效。
那么怎么让mybatis-config.xml文件与db.properties文件关联起来呢?
一、使用Java代码关联
修改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进行关联
SqlSessionFactory factory=builder.build(configStream,prop);
SqlSession session=factory.openSession();
UserinfoMapper mapper=session.getMapper(UserinfoMapper.class);
System.out.println("数据库信息来自于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中进行关联
在配置文件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">
<!--事务管理配置-->
<transactionManager type="JDBC"/>
<!--数据源配置-->
<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对应上一步自动生成的mapping/UserinfoMapper.xml文件-->
<mapper resource="mapping/UserinfoMapper.xml"/>
</mappers>
</configuration>
Test.java测试代码中保留了模拟加密解密部分代码,因为在mybatis-config.xml中进行了关联,将
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("数据库信息来自于properties文件!");
Userinfo userinfo=mapper.selectByPrimaryKey(1);
System.out.println(userinfo.getId()+" "+userinfo.getUsername()+" "+userinfo.getPassword()+" "+userinfo.getAge());
session.commit();
session.close();
}
}
OutPut:
数据库信息来自于properties文件!
1 usernameValue passwordValue 80
相关文章:配置多个environment环境