通过属性配置,可以将mybatis-config.xml中部分信息提取到专门的文件中存放。
所谓属性,其实就是一个key-value对。
展示3种配置属性的方法
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties">
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/>
properties>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${ur}" />
<property name="password" value="${pss}" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="org/itcheng/app/dao/NewsMapper.xml" />
mappers>
configuration>
driver=itcheng
ur=root
由于类SqlSessionFactoryBuilder的build方法,存在如下方法用于传入额外的属性
SqlSessionFactoryBuilder.build(InputStream inputStream, Properties properties)
package lee;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 org.itcheng.app.dao.NewsMapper;
import org.itcheng.app.domain.News;
public class NewsManager
{
// SqlSessionFactory应该是应用级别
private static SqlSessionFactory sqlSessionFactory;
public static void main(String[] args) throws IOException
{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
Properties props = new Properties();
props.setProperty("driver", "com.mysql.jdbc.Driver");
props.setProperty("pss", "1234");
// 1. 创建SqlSessionFactory, props用于传入额外的属性,这种方式的优先级是最高的
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, props);
// 2. 打开SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 查询消息
selectNews(sqlSession);
}
public static void selectNews(SqlSession sqlSession)
{
// 此处的NewsMapper只是一个接口
// MyBatis会使用JDK的动态代理为Mapper接口生成实现类
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
System.out.println(newsMapper.getClass());
List<?> list = newsMapper.findNews(-1);
System.out.println(list);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
}
}
${属性名}
即MyBatis的主配置文件的引用属性,有如下三个位置可以进行设置
主配置文件,同时遵循优先级的原则。
主配置文件
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${ur}" />
<property name="password" value="${pss}" />
1.主配置文件${url}
<properties resource="db.properties">
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/>
properties>
2.属性文件db.properties的 d r i v e r 和 {driver}和 driver和{ur}
driver=itcheng
ur=root
3.主类 d r i v e r 和 {driver}和 driver和{pss}
Properties props = new Properties();
props.setProperty("driver", "com.mysql.jdbc.Driver");
props.setProperty("pss", "1234");
// 1. 创建SqlSessionFactory, props用于传入额外的属性,这种方式的优先级是最高的
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, props);
${属性名: 默认值}
指定默认值默认是关闭,必须通过如下元素来开启:
<property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/>
MyBatis默认使用“: ”作为分隔符,也可通过如下元素来改变属性名与默认属性值之间的分隔符,即变更value值即可。
<property name="org.apache.ibatis.parsing.PropertyParser.default-value-separator" value="--"/>
注意:优先级是先调用属性配置,当属性配置没有值时调用默认值。
主配置文件
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties">
<property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/>
<property name="org.apache.ibatis.parsing.PropertyParser.default-value-separator" value="--"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/>
properties>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver--com.mysql.jdbc.Driver}" />
<property name="url" value="${url}" />
<property name="username" value="${ur--root}" />
<property name="password" value="${pss--root}" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="org/itcheng/app/dao/NewsMapper.xml" />
mappers>
configuration>
结果如下
class com.sun.proxy.$Proxy17
DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews ==> Preparing: select news_id id, news_title title, news_content content from news_inf where news_id > ?
DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews ==> Parameters: -1(Integer)
DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews <== Total: 1
[org.itcheng.app.domain.News@3829ac1]