『MyBatis技术内幕』源码调试前提

准备源代码包

  1. 下载源代码 3.4.6 版本
    https://github.com/mybatis/mybatis-3/releases?page=2
    源码
  2. 通过 idea 导入然后回自动下载所有依赖,根据 3.4.6 版本的 pom.xml 找到依赖的 mybatis-parent 版本
  <parent>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-parent</artifactId>
    <version>29</version>
    <relativePath />
  </parent>
  1. 同样需要从 github 下载
    https://github.com/mybatis/parent/releases/tag/mybatis-parent-29

准备测试环境

  1. 代码结构
    代码结构
  2. 创建 Maven 项目,并添加依赖 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>org.example</groupId>
    <artifactId>mybatisjsnm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <junit.version>4.12</junit.version>
        <mybatis.version>3.4.6</mybatis.version>
        <mysql.version>8.0.16</mysql.version>
        <druid.version>1.0.9</druid.version>
    </properties>

    <dependencies>
        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- install 后源码缺包-->
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.26.0-GA</version>
        </dependency>

        <!-- install 后源码缺包-->
        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>3.0.6</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>mybatis</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  1. 创建 mapper 接口 CountryMapper
public interface CountryMapper {
    public List<Country> selectAll();
}
  1. 创建实体类 Country
public class Country {

    private Long id;
    private String countryname;
    private String countrycode;
	... // get/set 方法
}
  • 相应字段自行在数据库里面创建
create table `country` (
	`id` int (11),
	`countryname` varchar (765),
	`countrycode` varchar (765)
); 
  1. 创建 CountryMapper.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="org.example.mybatisjsnm.mapper.CountryMapper">
    <select id="selectAll" resultType="Country">
        select id,countryname,countrycode from country
    </select>
</mapper>
  1. 配置 log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR,stdout
# Mybatis logging configuration...
log4j.logger.com.simple.mapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.layout.ConversionPattern=%5p [%t] - %m%n
  1. 配置 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>
    <!--开启日志输出-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    <!--配置类别名,配置后在Mapper配置文件(通常我们将编写SQL语句的配置文件成为Mapper配置文件)中需要使用pojo包中的类时,使用简单类名即可-->
    <typeAliases>
        <package name="org.example.mybatisjsnm.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC">
                <property name="" value=""/>
            </transactionManager>

            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <package name="org.example.mybatisjsnm.mapper"/>
    </mappers>

</configuration>
  1. 创建运行主类 MyBatisTest
public class MyBatisTest {

    @Test
    public void testMyBatisBuild() throws IOException {
        InputStream input = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(input);
        SqlSession sqlSession = sessionFactory.openSession();
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        List<Country> countries = mapper.selectAll();
    }
}
  1. 运行程序
    运行程序

集合成源代码

  1. 代码结构
    代码结构
  2. 首先将 mybatis-3-mybatis-3.4.6parent-mybatis-parent-29 导入到刚刚测试的相同 project 中,导入方式 File -> Project Structure -> Modules -> +号
  3. 然后分别在 mybatis-3-mybatis-3.4.6parent-mybatis-parent-29 执行下面命令
mvn clean install -Dmaven.test.skip=true
  1. 如果 mybatis-3-mybatis-3.4.6 显示 maven-pdf-plugin 相关的失败的话,把这个插件注释掉
<!--      <plugin>-->
<!--        <groupId>org.apache.maven.plugins</groupId>-->
<!--        <artifactId>maven-pdf-plugin</artifactId>-->
<!--      </plugin>-->
  1. 然后再尝试 debug、运行代码,看看是否能跳转到你相应代码上
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值