<configuration>
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull"/>
<property name="username" value="root"/>
<property name="password" value="liu2126962"/>
properties>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="useGeneratedKeys" value="true"/>
<setting name="logImpl" value="STDOUT_LOGGING"/>
settings>
<typeAliases>
typeAliases>
<typeHandlers>
typeHandlers>
<environments default="dev">
<environment id="dev">
<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>
<mapper resource="mapper/supplier-label-mapper.xml"/>
mappers>
configuration>
<mapper namespace="com.liubin.study.mybatis.object.ISupplierLabelDAO">
<sql id="selectFields">
SELECT
label.id,
label.supplier_type,
label.supplier_id,
label.label_type,
label.label_id,
label.label_name,
label.gmt_created,
label.gmt_modified
FROM supplier_label label
sql>
<select id="selectById" resultType="com.liubin.study.mybatis.object.dataobject.SupplierLabelDO">
<include refid="selectFields"/>
<where>
label.id = #{id}
where>
select>
mapper>
public class SimpleTest {
private final static Logger LOGGER = LoggerFactory.getLogger(SimpleTest.class);
public SqlSessionFactory buildFactory() {
try {
InputStream inputStream = Resources.getResourceAsStream("config/mybatis.xml");
return new SqlSessionFactoryBuilder().build(inputStream,"dev");
} catch (Exception e) {
LOGGER.error("构建SqlSessionFactory失败", e);
}
return null;
}
@Test
public void testMapper(){
SqlSessionFactory factory = buildFactory();
SqlSession session = factory.openSession();
try{
ISupplierLabelDAO supplierLabelDAO = session.getMapper(ISupplierLabelDAO.class);
SupplierLabelDO labelDO = supplierLabelDAO.selectById(1L);
System.out.println(labelDO);
}catch (Exception e){
LOGGER.error("执行查询SQL失败",e);
}finally {
session.close();
}
}
}
从上面的测试代码中,我们可以看到,首先mybatis.xml文件加载为流,然后根据流来构建mybatis的SqlSessionFactory。再根据SqlSessionFactory来来构建SqlSession。
public class SqlSessionFactoryBuilder {
public SqlSessionFactory build(Reader reader) {
return build(reader, null, null);
}
public SqlSessionFactory build(Reader reader, String environment) {
return build(reader, environment, null);
}
public SqlSessionFactory build(Reader reader, Properties properties) {
return build(reader, null, properties);
}
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
reader.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}
public SqlSessionFactory build(InputStream inputStream) {
return build(inputStream, null, null);
}
public SqlSessionFactory build(InputStream inputStream, String environment) {
return build(inputStream, environment, null);
}
public SqlSessionFactory build(InputStream inputStream, Properties properties) {
return build(inputStream, null, properties);
}
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
inputStream.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}
/**
* 最根本的build方法,上方的所有build方法最终都会调用此方法
* 需要一个Configuration作为参数
**/
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
}
public class DefaultSqlSessionFactory implements SqlSessionFactory {
private final Configuration configuration;
public DefaultSqlSessionFactory(Configuration configuration) {
this.configuration = configuration;
}
....
}
public class DefaultSqlSession implements SqlSession {
private Configuration configuration;
private Executor executor;
private boolean autoCommit;
private boolean dirty;
private List> cursorList;
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
this.configuration = configuration;
this.executor = executor;
this.dirty = false;
this.autoCommit = autoCommit;
}
}