mybatis介绍与环境搭建

2019-08-06  本文已影响0人  TiredHu
回顾JDBC
  1. 注冊驱动 (仅仅做一次)

2. 建立连接(Connection)

3. 创建运行SQL的语句(Statement)

4. 运行语句

5. 处理运行结果(ResultSet)

6. 释放资源

JDBC优缺点

优点:简单易学,上手快,非常灵活构建SQL,效率高
缺点:代码繁琐,难以写出高质量的代码(例如:资源的释放,SQL注入安全性等)
开发者既要写业务逻辑,又要写对象的创建和销毁,必须管底层具体数据库的语法
(例如:分页)。
适合于超大批量数据的操作,速度快

Mybatist特点

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。
iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)
jdbc/dbutils/springdao,hibernate/springorm,mybaits同属于ORM解决方案之一

创建mybatis项目

在IDEA中
File--New---Project

image.png

新建Maven项目

image.png

填写组织ID和工程ID

image.png

选择正确的maven环境

image.png

填写Project名字和存储路径

image.png

Finish
等待进度条完成点击import changes

image.png

检查自动生成的项目骨架是否完整

image.png

main--new--Directory

image.png

文件夹名称为java

image.png

同样的操作新建文件夹resources

image.png

java--Mark Directory as --Sources Root

image.png

resources--Mark Directory as --Resources Root

image.png

至此,maven项目创建完毕,接下来导入mybatis所需第三方jar包
打开pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.lianwei</groupId>
  <artifactId>com.lianwei</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>com.lianwei Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <mybatis.version>3.2.8</mybatis.version>
    <mybatis.spring.version>1.2.2</mybatis.spring.version>
    <mybatis-generator.version>1.3.5</mybatis-generator.version>
    <mysql.version>5.1.32</mysql.version>
    <druid.version>1.0.9</druid.version>
  </properties>

  <dependencies>

    <!-- Mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${mybatis.spring.version}</version>
    </dependency>
    <!-- MySql -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>
    <!-- 连接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>${druid.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <!--mybatis懒加载需要cglib-->
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>3.2.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>


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

   
  </dependencies>

  <build>
    <finalName>com.lianwei</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <!-- 解决配置文件不拷贝的问题 -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <excludes>
          <exclude>generatorConfig.xml</exclude>
        </excludes>
        <filtering>false</filtering>
      </resource>
    </resources>
  </build>
</project>

主要编辑三处内容,<properties>, <dependencies>,<resources>。
随后import changes等待maven引入jar包。

image.png

编译成功

上一篇下一篇

猜你喜欢

热点阅读