mybatis源码环境搭建

前言

mybatis在众多的数据持久化框架中应该说是目前使用最多的,其优秀的设计和底层封装,值得很多开发人员进行学习,下面让我们先来构建一下mybatis的源码阅读环境吧

准备

1、github下载mybatis源码

https://github.com/mybatis/parent/tree/mybatis-parent-30

2、导入idea

将下载好的源码,通过idea导入,在开始编译之前,需要修改pom文件中的几处配置,避免构建过程中报错

找到ognl坐标依赖

将true标签的true修改为false或者直接注释调

		<dependency>
			<groupId>ognl</groupId>
			<artifactId>ognl</artifactId>
			<version>3.2.10</version>
			<!-- <scope>compile</scope> -->
			<!-- <optional>true</optional> -->
		</dependency>

找到pdf坐标依赖

直接注释调即可

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-pdf-plugin</artifactId>
</plugin>

执行打包命令

mvn clean install -Dmaven.test.skip=true

首次编译安装,需要下载大量的依赖,比较花费时间,经过漫长的等待,看到如下的build success之后,就可以开始编写代码了

mybatis源码环境搭建

demo导入本地mybatis

下面我们来快速搭建一个mybatis的工程,并引用上面使用源码构建好的mybatis依赖包

mybatis源码环境搭建

项目结构非常简单,如图中所示

pom依赖

	<dependencies>

        <!-- mybatis依赖 -->
        <!--<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>-->

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.0-SNAPSHOT</version>
        </dependency>

        <!-- mysql依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>

        <!-- 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        
    </dependencies>

db.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://IP:3306/test?characterEncoding=utf-8
db.username=root
db.password=123456

SqlMapConfig.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 resource="demo1/db.properties"></properties>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${db.driver}" />
				<property name="url" value="${db.url}" />
				<property name="username" value="${db.username}" />
				<property name="password" value="${db.password}" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="demo1/UserMapper.xml" />
	</mappers>

</configuration>

UserMapper.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="test">

    <select id="findUserById" parameterType="int" resultType="com.congge.demo1.entity.User">
		SELECT * FROM user WHERE id = #{id}
	</select>
    
</mapper>

关于上面这几个配置文件的作用,想必使用过ssm框架或对mybatis框架了解的都知道了,然后再编写一个简单的接口和实现类

UserDao

User findUserById(int id);

UserDaoImpl

public class UserDaoImpl implements UserDao {

	private SqlSessionFactory sqlSessionFactory;

	// 注入sqlSessionFactory
	public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}

	@Override
	public User findUserById(int id) {
		// sqlsessionFactory工厂类去创建sqlsession会话
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// sqlsession接口,开发人员使用它对数据库进行增删改查操作
		User user = sqlSession.selectOne("test.findUserById", id);
		return user;
	}

}

测试类

public class DemoTest1 {

    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void init() throws Exception{
        // 加载全局配置文件(同时把映射文件也加载了)
        String resource = "demo1/SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // sqlsessionFactory需要通过sqlsessionFactoryBuilder读取全局配置文件信息之后
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void testFindUserById() {
        UserDao dao = new UserDaoImpl(sqlSessionFactory);
        User user = dao.findUserById(1);
        System.out.println(user);
    }
}

运行testFindUserById这个单元测试方法,可看到,从数据库查询到一条结果
mybatis源码环境搭建

到这里,我们完成了mybatis的源码安装与构建,并通过工程的形式导入源码包并完成了测试,下一步让我们通过源码的方式来逐步深入学习mybatis的底层架构吧

本篇到此结束,最后感谢观看!

上一篇:【MyBatis】Spring集成原理(二):创建 SqlSession


下一篇:Mybatis【3】-- Mybatis使用工具类读取配置文件以及从属性读取DB信息