MyBatis—传统 Dao开发方式、分析

本文介绍了使用MyBatis进行传统Dao开发的基本步骤,包括导入Maven依赖、创建Dao接口、编写mapper文件、配置MyBatis主配置文件以及测试类的编写。在分析中提到,尽管Dao实现类看似没有实际操作,但MyBatis通过动态代理方式直接定位到mapper中的SQL语句,实现了对数据库的操作。这种方式简化了Dao的实现,提高了开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搭建MyBatis框架基本步骤

1.导入Maven依赖
2. 创建Dao接口:定义操作数据的方法
3. 创建mapper文件:也叫sql映射文件:用来写sql语句,与接口中的方法相对应的sql语句
4. 创建MyBatis的一个主配置文件:1). 连接数据库 2).指定mapper文件的位置
5. 使用MyBatis的对象SqlSession,通过SqlSession的方法执行sql语句
在这里插入图片描述

MyBatis使用传统 Dao 开发方式

使用 Dao 的实现类,操作数据库

创建Maven工程使用MyBatis框架

在这里插入图片描述
导入依赖
主要是MyBatis依赖和MySQL驱动

<?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>work01_mybatis_dao</groupId>
  <artifactId>learn</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

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

    <!--MyBatis依赖-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.9</version>
    </dependency>
    <!--MySQL驱动-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.1</version>
    </dependency>

  </dependencies>

  <build>
    <resources>
    <resource>
      <directory>src/main/java</directory><!--所在的目录-->
      <includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
        <include>**/*.properties</include>
        <include>**/*.xml</include>
      </includes>
      <filtering>false</filtering>
    </resource>
  </resources>
  </build>

</project>

App主要使用MyBatis技术对数据库进行操作,不需要App类,可删除
在这里插入图片描述
使用idea创建自己的xml模板
在这里插入图片描述
自定义一个xml文件模板
在这里插入图片描述
根据自己的文件结构设定namespace、id、resultType

<mapper namespace="work01_mybatis_dao.content.domain.dao.StudentDao">
    <select id="selectStudents" resultType="work01_mybatis_dao.content.domain.Student">

    </select>

namespace:抽象类StudentDao的Reference id:抽象类StudentDao里的方法名
ResultType:方法的返回类型

数据处理的配置文件
在这里插入图片描述

主配置文件
在这里插入图片描述
数据处理的具体实现
在这里插入图片描述

实现—查询MySql数据

编写测试类

在这里插入图片描述

MySQL数据库表结构

在这里插入图片描述

测试结果

在这里插入图片描述

传统 Dao 开发方式的分析

在前面例子中自定义 Dao 接口实现类时发现一个问题:Dao 的实现类其实并没有干什么实质性的工作,它仅仅就是通过 SqlSession 的相关 API 定位到映射文件 mapper 中相应 id 的 SQL 语句,真正对 DB 进行操作的工作其实是由框架通过 mapper
中的 SQL 完成的。 所以,MyBatis 框架就抛开了 Dao 的实现类,直接定位到映射文件 mapper 中的相应 SQL
语句,对DB 进行操作。这种对 Dao 的实现方式称为 Mapper 的动态代理方式。 Mapper 动态代理方式无需程序员实现 Dao
接口。接口是由 MyBatis 结合映射文件自动生成的动态代理实现的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

之墨_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值