1. 程式人生 > >使用 IntelliJ IDEA建立MyBatis工程

使用 IntelliJ IDEA建立MyBatis工程

暑假的時候學習SSH三大框架,記得花了整整一個暑假才把他們學完,後來接觸了Mybatis,聽說易上手,所以就學習學習.,簡單總結一些兩者的區別吧:
1.兩者最大的區別:
針對簡單邏輯,Hibernate和MyBatis都有相應的程式碼生成工具,可以生成簡單基本的DAO層方法。
針對高階查詢,Mybatis需要手動編寫SQL語句,以及ResultMap。而Hibernate有良好的對映機制,開發者無需關心SQL的生成與結果對映,可以更專注於業務流程。
2.開發難度對比
Hibernate的開發難度要大於Mybatis。主要由於Hibernate比較複雜、龐大,學習週期較長。
而Mybatis則相對簡單一些,並且Mybatis主要依賴於sql的書寫,讓開發者感覺更熟悉。

下面是小編建立第一個Mybatis工程的過程:
1.命名過程
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述
2.配置過程
1)在pom.xml中配置需要的Jar包

        <!-- mybatis核心包 -->
         <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency
>
<!-- mysql驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> <!-- junit測試包 --> <dependency
>
<groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 日誌檔案管理包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

這裡寫圖片描述
注:Java目錄下存放實體類及以後的dao controller層
resources下存放各種配置檔案 mybatis-config.xml裡面的mappers路徑要寫對.
test下進行測試
3.相關配置的具體程式碼
mybatis-config.xml

User.xml

test(注意路徑)

User實體類

自此,讓我們踏上Mybatis的慢慢摸索之路吧!