mybatis入门实战之CRUD

最近由于项目中用到了mybatis,通过自己的实践,希望将学到的东西分享给初学者

Mybatis官网:http://www.mybatis.org/mybatis-3/zh/index.html

推荐书籍: 深入浅出MyBatis技术原理与实战.pdf

希望读者在学习mybatis之前,最好先把官网与相关书籍浏览一下,加深个人的理解。

下面进入实战:

先看看工程目录

本工程为maven工程,所需的jar包都在pom.xml文件中配置了,建议读者本地将maven配置文件换成阿里云镜像,jar包下载速度有明显提升

     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  请自行下载运行



猜你喜欢

转载自blog.csdn.net/linpe/article/details/77465736