构造一个工厂:
package com.laoxu.mybatis.executor;
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 start {
public static void main(String[] args) throws IOException {
// user.dir指定了当前的路径
String userDir = System.getProperty("user.dir");
System.out.println(userDir);
// 获取类路径
String classPath = start.class.getResource("/").getPath();
System.out.println(classPath);
// 根据当前类路径下的相对路径 (classPath + {path})
String resource = "mapper/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
}
读取核心配置文件mybatis-config.xml作为流进行创建工厂SqlSessionFactory
创建工厂构造器,返回一个默认的工厂。
/**
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.session;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import java.io.InputStream;
import java.util.Properties;
/**
*构建{@link SqlSession}实例。
*
* @作者克林顿·贝京
*/
public class SqlSessionFactoryBuilder {
public SqlSessionFactory build(InputStream inputStream) {
return build(inputStream, null, null);
}
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
return new DefaultSqlSessionFactory();
}
}
默认工厂类实现工厂接口:
/**
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.session.defaults;
import org.apache.ibatis.session.SqlSessionFactory;
/**
* @作者克林顿·贝京
*/
public class DefaultSqlSessionFactory implements SqlSessionFactory {
}
/**
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.session;
/**
*从一个连接或数据源创建一个{@link SqlSession}
*
* @作者克林顿·贝京
*/
public interface SqlSessionFactory {
}