mybatis入門實戰之CRUD

最近因爲項目中用到了mybatis,經過本身的實踐,但願將學到的東西分享給初學者html

Mybatis官網:http://www.mybatis.org/mybatis-3/zh/index.htmljava

推薦書籍: 深刻淺出MyBatis技術原理與實戰.pdfmysql

但願讀者在學習mybatis以前,最好先把官網與相關書籍瀏覽一下,加深我的的理解。sql

下面進入實戰:數據庫

先看看工程目錄mybatis

本工程爲maven工程,所需的jar包都在pom.xml文件中配置了,建議讀者本地將maven配置文件換成阿里雲鏡像,jar包下載速度有明顯提高app

     maven阿里雲鏡像配置maven

<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
  <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>       
    </mirror>
  </mirrors>工具

      pom文件:學習

<dependencies>
   <!-- Mybatis -->
   <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.4.4</version>
 </dependency>
 
 <!-- MySql -->
 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.43</version>
 </dependency>
 
 <!-- Log4j -->
 
 <!-- https://mvnrepository.com/artifact/log4j/log4j -->
 <dependency>
     <groupId>log4j</groupId>
     <artifactId>log4j</artifactId>
     <version>1.2.17</version>
 </dependency>
 
  </dependencies>

數據庫爲Mysql,db文件爲db.sql

簡單的介紹下工程:
首先看配置文件mybatis-config.xml

詳細配置參數官網已經介紹的很清楚,請讀者參照官網學習
另外工程中model包下爲實體類,做爲與數據庫表的映射
Util包下爲數據庫鏈接工具類
mapper包下爲持久化接口以及相應的sql實現文件

測試類爲Test.java

工程已經上傳,地址爲 http://download.csdn.net/download/linpe/9942459  請自行下載運行