准备工作
- 创建一个maven项目
https://www.jianshu.com/p/e2dc268172be - pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.buddy.study</groupId>
<artifactId>xxx</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name> Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<java.version>1.8</java.version>
<lombok.version>1.18.2</lombok.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>xxx</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
- 创建在src/test/resources/创建mybatis-config.xml配置文件
<?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>
<properties>
</properties>
<typeAliases>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://{ip}:{port}/{database}?useSSL=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="User.xml"></mapper>
</mappers>
</configuration>
- 创建mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="mapper.UserMapper">
<select id="selectByIdd" parameterType="int" resultType="entity.User" >
SELECT * FROM user WHERE ID = #{id}
</select>
</mapper>
- 实体
public class User {
private long id;
private String name;
private String psword;
}
- 创建user表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`psword` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- 测试类
public class MybatisTest {
@Test
public void parseTest() throws IOException {
//获取mybatis配置文件流
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
//初始化mybatis整体配置信息以及所有mapper
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取Mapper
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User room1 = mapper.selectByIdd(-1);
System.out.println(room1);
}
}
- 初始化流程
- 具体代码
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
-
build过程
主要过程为通过inputStream流创建XMLConfigBuilder对象,再使用parse()方法解析dom结构转换为Configuration对象,然后初始化DefaultSqlSessionFactory对象。重点在parse(),下面看看具体做了什么
Configuration对象有一个成员变量parsed布尔类型 ,主要负责标记是否解析过,也就是说每个Configuration对象只能解析dom元素一次。解析dom使用的是sax,解析之前对事物,缓存等做了别名注册操作。在解析的过程中初始化了mybatis所有属性。
- properties:
http://www.mybatis.org/mybatis-3/zh/configuration.html#properties - setting:
http://www.mybatis.org/mybatis-3/zh/configuration.html#settings
- typeAliases:
http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases - plugins:
http://www.mybatis.org/mybatis-3/zh/configuration.html#plugins
支持:
Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
ParameterHandler (getParameterObject, setParameters)
ResultSetHandler (handleResultSets, handleOutputParameters)
StatementHandler (prepare, parameterize, batch, update, query) - objectFactory:
http://www.mybatis.org/mybatis-3/zh/configuration.html#objectFactory
默认实现 DefaultObjectFactory - objectWrapperFactory:
官方文档暂无描述
默认实现 DefaultObjectWrapperFactory - reflectorFactory:
官方文档暂无描述
默认实现 DefaultReflectorFactory - environments:
http://www.mybatis.org/mybatis-3/zh/configuration.html#environments
负责事务以及数据库连接信息的配置 - databaseIdProvider:
http://www.mybatis.org/mybatis-3/zh/configuration.html#databaseIdProvider - typeHandlers:
http://www.mybatis.org/mybatis-3/zh/configuration.html#typeHandlers - mappers:
http://www.mybatis.org/mybatis-3/zh/configuration.html#mappers
映射器支持大致两种方式:XML文件,java文件
XML文件又分为类路径以及本地路径
java文件分为类路径以及包扫描