Mybatis源码学习(1)-源码环境搭建

一、下载源码

  源码官方下载地址:https://github.com/mybatis/mybatis-3。当前使用的版本是:mybatis-3.4.6。

二、导入Mybatis到eclipse中。

  普通的maven项目导入方式,不做详细描述。

三、修改pom文件,增加mysql-connector-java依赖

  在pom文件的dependencies元素中增加mysql依赖jar。如下所示:

    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.29</version>
    </dependency>

四、配置文件

  在项目中增加相关配置文件,保证可以运行一个简单查询数据库的demo。需要增加的文件(resources目录需要自行创建)如下图所示:
在这里插入图片描述

1、 Mybatis配置文件

   Mybatis配置文件,即上图中的myBatis-temp.xml文件,该配置文件包含了会深深影响 MyBatis 行为的设置和属性信息等。详细信息可以参考:http://www.mybatis.org/mybatis-3/zh/configuration.html#。本例中的配置如下(只做了最简单的配置):

<?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 url="ftp://F:/jdbc.properties"></properties> -->
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC" />
            <!-- 配置数据库连接信息 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>
    <mappers>    
    	<mapper resource="mapper/TestMapper.xml"/>  
    </mappers> 
</configuration>
2、XML 映射文件

   XML 映射文件,即上图中TestMapper.xml文件(根据实际需求进行命名,该处测试使用,未严格遵循开发规范),主要是映射SQL语句,详细内容可以参考:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html。本例中的配置如下:

<?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="TestMapper">
	<select id="queryList" resultType="map">
        SELECT * FROM test WHERE field1 = #{field1}
    </select>
</mapper>
3、jdbc配置文件

  jdbc配置文件,即上图中jdbc.properties文件,主要是数据库链接信息,本示例主要是测试使用,所以只包含了最基本的信息,配置如下所示(数据库test、测试需要的test表等请自行创建):

#mysql database setting
jdbc.type=mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

五、测试代码

  测试代码目录如下图所示:
在这里插入图片描述
  该例子中主要实现了一个简单查询的demo,为后续跟踪代码提供方便。其中MainTest代码源码如下所示:

package com.hsh.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MainTest {
	
	public static void main(String[] args) {
		try {
			InputStream input = Resources.getResourceAsStream("myBatis-temp.xml");
			//SqlSessionFactory mySqlSessionFactory = new SqlSessionFactoryBuilder().build(input,"mysql");
			SqlSessionFactory mySqlSessionFactory = new SqlSessionFactoryBuilder().build(input);
			SqlSession sqlSession = mySqlSessionFactory.openSession(true);//自动提交
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("field1", "a1");
			List<Map<String, Object>> list = sqlSession.selectList("TestMapper.queryList", param);
			System.out.println(list.toString());
//			Cursor<Map<String, Object>> cursor =  sqlSession.selectCursor("TestMapper.queryList", param);
//			Iterator<Map<String, Object>> iter = cursor.iterator();
//			while (iter.hasNext()) {
//				System.out.println(iter.next().toString());
//			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

六、参考文档、书籍

  1. 源码官方下载地址:

    https://github.com/mybatis/mybatis-3

  2. 官网中文教程

    http://www.mybatis.org/mybatis-3/zh/index.html

  3. 学习交流源码(在源码基础上增加了注释、测试方法等)

    https://gitee.com/hsh2015/mybatis_source_learning

  4. 书籍:《Mybatis技术内幕》

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姠惢荇者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值