使用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://172.16.1.2:5432/sim0.9.0"/> <property name="username" value="postgres"/> <property name="password" value="765@#sy666"/> </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); } }
文件结构:
标签:xml,SqlSessionFactory,SqlSeesionFactory,ibatis,io,mybatis,import From: https://www.cnblogs.com/woniu123/p/16588743.html