mybatis_3_使用xml构建SqlSeesionFactory
使用xml构建SqlSeesionFactory,分为两步:
第一步:创建一个XML配置文件;
第二步:创建SqlSessionFactory实例;
示例:
第一步:创建mybatis-config2.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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="org.postgresql.Driver"/> <property name="url" value="jdbc:postgresql://xx.xx.xx.xx:5432/database_name"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> </configuration>
文件结构:
第二步:创建SqlSessionFactory实例
package tk.mybatis.simple.model; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class SqlSessionFactoryTest { public static void main(String[] args) throws Exception { String resource = "mybatis-config2.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } }
文件结构: