mybatis调用sql语句过程
过程如图
利用mybatis源码执行sql语句
//配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource)
//根据配置文件生成 SqlSessionFactory ------------A ------ ----
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
//通过opensession()获得session ------------B ------ ----
SqlSession Session = sqlSessionFactory.openSession();
//通过Mapper接口找到映射MApper.xml语句 ------------C ------ ---
Student student = sqlSession.selectOne(
"pojo.StudentMapper.getStudents");
mybatis主要作用
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。
MyBatis 作用
1.用简单的 XML 或注解来配置和映射原生信息
2.将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
配置文件
<?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="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/taotao?serverTimezone=UTC&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="Changeme_123"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mybatis/StudentMapper.xml"/>
</mappers>
</configuration>
A-1 sqlSessionFactoryBuilder 创建sqlsessionFactory
public class SqlSessionFactoryBuilder {
//省略
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new