发表于: 2018-03-05 20:59:29
1 579
今天完成
1.准备小课堂,看到了关于mybatis的SqlSessionFactory实例生成的过程的讲解
在我们通过mybaits连接数据库时有这么一个过程:

这个过程通过调用SqlSessionFactoryBuilder类的实例的build方法来完成,
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 props) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, props);
Configuration config = parser.parse();
return build(config);
} 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(Configuration config) {
return new DefaultSqlSessionFactory(config);
}
}
可以看出,SqlSessionFactoryBuilder类负责构建SqlSessionFactory,并且提供了多个build的重载方法。但其实很多都是在调同一签名的方法,例如:
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) ,只是由于方法参数environment和 propertiese都可以为null,
所以为了提供调用的便利性,才提供了下面的三个方法:
public SqlSessionFactory build(Reader reader)
public SqlSessionFactory build(Reader reader, Properties properties)
public SqlSessionFactory build(Reader reader, String environment)
public SqlSessionFactory build(Reader reader, String environment, Properties props)
public SqlSessionFactory build(Reader reader)
public SqlSessionFactory build(Reader reader, String environment)
public SqlSessionFactory build(Reader reader, Properties properties)public SqlSessionFactory build(Reader reader, String environment, Properties props)
public SqlSessionFactory build(Configuration config)
按照上述思路去除重复的,真正的重载方法只有如下两种:
public SqlSessionFactory build(Reader reader, String environment, Properties props)
public SqlSessionFactory build(Configuration config)
可以看出,配置信息可以以三种形式提供给SqlSessionFactory的build方法,分别是InputStream(字节流)、Reader(字符流)、Configuration(类),由于字节流与字符流都属于读取配置文件的方式,所以从配置信息的来源就很容易想到构建一个SqlSessionFactory有两种方式,大致代码如下:
(1) 读取xml文件构造方式
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream) ;
(2) 编程构造方式
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration) ;
使用XML文件构造方式的build方法的源码:
public SqlSessionFactory build(Reader reader, String environment, Properties props) {
try {
//基于XML文件的这种构造方式,通过从XML中读取信息
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, props);
//构造出Configuration对象
Configuration config = parser.parse();
return build(config);
} 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.
}
}
}//继续进行SqlSessionFactory的构建工作
public SqlSessionFactory build(Configuration config) {
//返回org.apache.ibatis.session.defaults.DefaultSqlSessionFactory
return new DefaultSqlSessionFactory(config);
}
}
2.学习springMVC
做了一些关于springMVC的练习
(1)@RequestMapping的属性
(2)Action请求处理方法
在插入图片的时候遇到问题
项目结构如下:
,
jsp文件如下
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="E" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>action5的视图</title>
</head>
<body>
<img alt="风景" src="<c:url value="/images/picture1.jpg"></c:url>">
<%----%>
</body>
</html>
运行代码后,不能正常显示,效果如下,参照网上建议用标签<c:url value="XXX/XX"></c:url>后结果不变
明天计划
1.完SSM框架下对数据库操作所需知识点的初步学习;
遇到问题
--------
收获
评论