Mybatis源码阅读1---让Mybatis运行起来

Mybatis是Java语言写的数据库到对象映射,所以需要些Java的知识,了解JDBC。

读源码肯定要debug,第一步就是把环境搭好。我使用的编译器是Idea,当然用其它编译器也ok,项目构建用了maven(也可以不用哦),jdk用1.8。首先把源码下载到本地: https://github.com/mybatis/mybatis-3 可以用git也可以直接下载zip包。下载完后我们需要的是src目录和pom.xml文件,准备完毕微笑

1:创建一个java工程:


2:把mybatis源码的org文件(位置:src->main->java)复制到新建工程的main目录下,把mybatis源码的pom.xml里面的dependency复制到工程的pom.xml的dependencies中。顺便在dependency加上mysql驱动,修改maven-compile-plugin为1.8,下图附上dependencies和build

  <dependencies>
    <dependency>
      <groupId>ognl</groupId>
      <artifactId>ognl</artifactId>
      <version>3.2.6</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.22.0-GA</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.25</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.25</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.11.0</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>3.2.6</version>
      <optional>true</optional>
    </dependency>

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

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

3:创建City类和CityDao, 简单一点,City类只有id属性,CityDao只有一个方法getById




4:资源文件添加mybatis.xml(用的是mysql及内置的world库)

<?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.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/world"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="CityMapper.xml"/>
    </mappers>
</configuration>

及CityMapper.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="com.hhg.jerry.dao.CityDao">
    <select id="getById"
            resultType="com.hhg.jerry.model.City"
            parameterType="java.lang.Long">
        SELECT * FROM city where id = #{id}
    </select>
</mapper>


5 : 找到App.java(maven给创建的类,若没有此类就随便添加一个类),添加代码运行mybatis

public class App {
    public static void main( String[] args ) throws Exception{
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        City city = sqlSession.selectOne("com.hhg.jerry.dao.CityDao.getById",1L);
        System.out.println(city);
    }
}

到此,mybatis源码就能debug了。

总结:这一篇主要目的是能debugmybatis,为了简化,mybatis的核心配置文件只加了environment和一个mapper,CityDao也只有一个方法,City也就有一个属性。下一篇会完善mybatis的配置及mapper对应的方法。

阅读更多
文章标签: Mybatis源码阅读
个人分类: Mybatis Java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭