使用MyBatis框架进行实际开发,只会简单的配置是不行的,我们还需要对框架中的核心对象、核心配置文件以及映射文件有更深入的了解。本章将针对MyBatis核心对象、核心配置文件和映射文件进行解析。
- Mybatis官方文档
Mybatis官方学习文档https://mybatis.org/mybatis-3/zh/index.html
org.mybatis
mybatis
x.x.x
2.从xml中构建SqlSesionFactor
每一个基于Mybatis的应用都是以一个SqlSessionFactory的实例为核心的。SqlSessionFactory的实例可以通过SqlSessionFactoryBuilder获得。而SqlSessionFactoryBuilder则可以从xml配置文件或一预先配置的Configuration实例来构建出SqlSessionFactory实例,例子如下:
//使用MyBatis提供的Rescources类加载MyBatis的配置文件
Reader reader =
Resources.getResourceAsReader("mybatis-config.xml");
//构建SqlSessionFactory工厂
sqlSessionFactory =
new SqlSessionFactoryBuilder().build(reader);
3.从xml文件中构建SqlSessionFactory
mybatis-config.xml代码示例如下:
SqlSessionFactoryBuilder有5个build方法,每一种都允许你从不同的资源中创建一个SqlSessionFactory实例。
SqlSessionFactory build(InputStream inputStream, String environment)
SqlSessionFactory build(InputStream inputStream, Properties properties)
SqlSessionFactory build(InputStream inputStream, String env, Properties props)
可选的参数是 environment 和 properties。environment 决定加载哪种环境,包括数据源和事务管理器,参考文档中有详细说明。
4.SqlSessionFactoryBuilder构建Builde方法的形式。
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
由于Build方法中的参数environment和properties都可以为null,所以SqlSessionFactoryBuild构建SqlSessionFactory对象的build()方法按照配置信息的传入方式,为3种:
4.1SqlSessionFactoryBuilder构建build方法
build(InputStream inputStream,String environment,Properties properties);
上述build方法种,参数inputStream是字节流,它它封装了XML文件形式的配置信息;参数environment和参数properties为可选参数。其中,参数environment决定将要加载的环境,包括数据源和事务管理器;参数properties决定将要加载的properties文件
4.2SqlSessionFactoryBuilder构建build方法
build(Reader reader,String environment,Properties properties)
由上述build()方法可知,第二种形式的build()方法参数作用与第一种形式大体一致,唯一不同的是,第一种形式的build()方法使用InputStream字节流封装了XML文件形式的配置信息,而第二种形式的build()方法使用Reader字符流封装了xml文件形式的配置信息。
4.3SqlSessionFactoryBuilder构建build()方法
build(Configuration config)
通过以上代码可知,配置信息可以通过InputStream(字节流)、Reader(字符流)、Configuration(类)三种形式提供给SqlSessionFactoryBuilder的build()方法。
5.使用什么模式创建SqlSessionFactory对象
SqlSessionFactory对象是线程安全的,它一旦被创建,在整个应用程序执行期间都会存在。如果我们多次创建同一个数据库的SqlSessionFactory对象,那么该数据库的资源将很容易被耗尽。通常每一个数据库都只创建一个SqlSessionFactory对象,所以在构建SqlSessionFactory对象时,建议使用单例模式。
方法名称 |
描述 |
SqlSession openSession() |
开启一个事务。 |
SqlSession openSession(Boolean autoCommit) |
参数autoCommit可设置是否开启事务。 |
SqlSession openSession(Connection connection) |
参数connection可提供自定义连接。 |
SqlSession openSession(TransactionIsolationLevel level) |
参数level可设置隔离级别。 |
SqlSession openSession(ExecutorType execType) |
参数execType有三个可选值。 |
SqlSession openSession(ExecutorType execType, Boolean autoCommit) |
参数execType有三个可选值。 |
SqlSession openSession(ExecutorType execType, Connection connection) |
参数ExecutorType有三个可选值。 |
openSession(ExecutorType execType)参数值
参数execType有三个可选值:
openSession(ExecutorType execType,Boolean autoCommit)参数值
openSession(ExecutorType execType,Connection connection)参数值
SqlSession对象的作用
SqlSession是MyBatis框架中另一个重要的对象,它是应用程序与持久层之间执行交互操作的一个单线程对象,主要作用是执行持久化操作,类似于JDBC中的Connection。SqlSession对象包含了执行SQL操作的方法,由于其底层封装了JDBC连接,所以可以直接使用SqlSession对象来执行已映射的SQL语句。
方法名称 |
描述 |
|
查询方法。参数statement是在配置文件中定义的 |
|
查询方法。parameter是查询语句所需的参数。 |
|
查询方法。参数statement是在配置文件中定义的 |
|
查询方法。parameter是查询语句所需的参数。 |
|
查询方法。rowBounds是用于分页的参数对象。 |
void select(String statement, Object parameter, ResultHandler handler) |
查询方法。,handler对象用于处理查询语句返回的复杂结果集。 |
int insert(String statement) |
插入方法。参数statement是在配置文件中定义的 |
int insert(String statement, Object parameter) |
插入方法。parameter是插入语句所需的参数。 |
int update(String statement) |
更新方法。参数statement是在配置文件中定义的 |
int update(String statement, Object parameter) |
更新方法。parameter是更新语句所需的参数。 |
int delete(String statement) |
删除方法。参数statement是在配置文件中定义的 |
void commit() |
提交事务的方法。 |
void rollback() |
回滚事务的方法。 |
void close() |
关闭SqlSession对象。 |
|
该方法会返回Mapper接口的代理对象。参数type是Mapper的接口类型。 |
Connection getConnection() |
获取JDBC数据库连接对象的方法。 |
补充:使用Mapper接口
/**
* MyBatis基于Mapper接口的使用,需要遵守相关的约定*
*1.声明的接口方法名必须要和映射文件中的SQL语句id一致
*2.映射文件中namespace必须是接口的全类路径名称
*3.接口的名称必须要和映射文件的名称相同**/
public interface UserMapper
{
public User findById(Integer id);
}
/**
* 基于Mapper接口的实现*/
@Test
public void query() throws Exception
{
InputStream in = Resources.getResourceAsStream("mybatis-config.xmL");
SqLSessionFactory factory = new SqLSessionFactoryBuilder().build(in);
SqLSession sqLSession = factory.openSession();
UserMapper mappelr = sqLSession.getMapper(UserMapper.cLass);
User user = mapper.findById(2);
System.out.println(user.getUname());
session.close();
}
总结
以上就是对Mybatis核心对象(SqlSessionFactory、SqlSessionFactoryBuilder、SqlSession)作用的理解,参考于人民邮电出版社JavaEE企业级应用开发教程。