Mybatis 基础介绍与逆向工程的构建

本文详细介绍了Mybatis的基本概念、优势,并指导如何构建Mybatis工程,包括pom.xml配置、目录结构和核心配置文件。同时,探讨了逆向工程的使用,包括generatorConfig.xml配置和生成的代码类型。最后,文章提到了Mybatis中parameterType与parameterMap、resultType与resultMap的区别,以及#与$参数标记的差异。
摘要由CSDN通过智能技术生成

mybatis.png

Mybatis 基础介绍与逆向工程的构建

Mybatis 系列:
Mybatis 基础介绍与逆向工程的构建:https://blog.csdn.net/qq_34002221/article/details/86662766
Mybatis 源码分析(一)之 Mybatis 的Executor的初始化:https://blog.csdn.net/qq_34002221/article/details/86684386
Mybatis 源码分析(二)之 Mybatis 操作数据库的流程:https://blog.csdn.net/qq_34002221/article/details/86729385
Mybatis 源码分析(三)之 Mybatis 的一级缓存和二级缓存 :https://blog.csdn.net/qq_34002221/article/details/86726720

优秀博客:
http://www.mybatis.org/mybatis-3/zh/index.html
http://www.mybatis.org/generator/index.html


为什么要用Mybatis?

先看下我们传统JDBC连接数据库的弊端:

  • jdbc 底层没有用连接池、操作数据库需要频繁的创建和关联链接。消耗很大的资源

  • 写原生的 jdbc 代码在java中,一旦我们要修改sql的话,java需要整体编译,不利于系统维护

  • 使用 PreparedStatement 预编译的话对变量进行设置 123 数字,这样的序号不利于维护

  • 返回 result 结果集也需要硬编码。

什么是 MyBatis ?

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

这么看一下,是不是感觉mybatis全给解决了。

开始构建Mybatis工程
pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
<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.demo</groupId>
<artifactId>mybatis-test</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <!-- 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>

<!-- 逆向工程插件 -->
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.29</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>
</build>


</project>
项目目录结构

项目目录结构.png

mybatis-config.xml
<
属性名 作用
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值