Mybatis源碼學習(一)源碼下載和簡單Demo


1 源碼下載

到mybatis的官網進行源碼下載:https://mybatis.org/mybatis-3/

我們選擇Source code下載

下載后解壓,該項目是一個maven項目,我們用idea打開,目錄結構如下

 

 2 Demo項目

 我們在源碼工程下創建一個demo目錄,存放我們的demo代碼

               

MybatisMain.java
public class MybatisMain {
  public static void main(String[] args) throws IOException
  {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    Blog blog = sqlSession.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
    System.out.println(blog.getContext());
  }
}

Blog.java

public class Blog {
  private Integer id;
  private String username;
  private String context;
  //省去了getter和setter
}

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

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root

BlogMapper.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.mybatis.example.BlogMapper">
    <select id="selectBlog" resultType="org.apache.ibatis.demo.Blog">
        select * from Blog where id = #{id}
    </select>
</mapper>

最后需要注意的是我們要把pom文件中的如下部分注釋掉,要不然運行會報錯。

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.24</version>
      <!--<scope>test</scope>注釋掉-->
    </dependency>

同時,我們要有一個mysql實例,配置見jdbc.properties

mysql> use test
Database changed
mysql> select * from blog;
+------+----------+---------+
| id   | username | context |
+------+----------+---------+
|  101 | szj      | hello   |
+------+----------+---------+
1 row in set (0.00 sec)
mysql>

運行結果

"C:\Program Files (x86)\Java\jdk1.8.0_181\bin\java" ...
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
hello

Process finished with exit code 0

下一節我們將剖析以上示例的運行原理

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM