MyBatis快速入门使用心得

一、认识MyBatis

1.1 MyBatis的特性

MyBatis 是⼀款优秀的持久层框架,它⽀持⾃定义 SQL、存储过程以及⾼级映射。

MyBatis 基于JDBC实现但是去除了⼏乎所有的 JDBC 代码以及设置参数和获取结果集的⼯作。

’MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接⼝和 Java POJO(Plain Old Java Objects,普通⽼式 Java 对象)为数据库中的记录。简单来说 MyBatis 是更简单完成程序和数据库交互的⼯具,也就是更简单的操作和读取数据库⼯具。

MyBatis 也是⼀个 ORM 框架,ORM(Object Relational Mapping),即对象关系映射。在⾯向对象编程语⾔中,将关系型数据库中的数据与对象建⽴起映射关系,进⽽⾃动的完成数据与对象的互相转换:

1. 将输⼊数据(即传⼊对象)+SQL 映射成原⽣ SQL

2. 将结果集映射为返回对象,即输出对象

ORM把数据库映射为对象:

数据库表(table)->类(class)

记录(record,行数据)->对象(object)

字段(field)->对象的属性(attribute)

一般的ORM框架,会将数据库模型的每张表都映射成一个Java类,也就是使用MyBatis可以像操作对象一样操作数据库中的表,可以实现对象和数据库表之间的转换

框架交换流程图:

1.2 MyBatis的作用

现在的MyBatis作用就是将重要数据数据存入数据库和从数据库中获取数据

比如前后端交互DAO:

  1. 前端的一次提交携带的数据,大多数只是数据库的表的一行罢了,而这段数据要保存起来,否则本次提交仅当前页面有效

    • 例如,提交用户信息,提交博客信息,删除和修改博客…
    • 补充:session存储就不讲了,直接讲数据库存储

2.后端返回的数据需通过数据库来判断,或者返回数据库的数据

例如登录校验,博客列表

3.删除修改操作,也需要数据库操作

二、 回顾一下JDBC的操作流程

1.创建数据库连接池DataSource

2.通过DataSource 获取数据库连接 Connection

3.编写要执行 带 ?占位符的SQL语句

4.通过 Connection 以及 SQL创建操作命令对象 Statement

5.替换占位符:指定要替换的数据库字段类型,占位符索引以及要替换的值

6.使用 Statement 执行SQL语句

7.查询操作:返回结果集 ResulSet,

更新操作:返回更新的数量

8.处理结果集

9.释放资源

public class SimpleJdbcOperation {
    private final DataSource dataSource;

    public SimpleJdbcOperation(DataSource dataSource) {
        this.dataSource = dataSource;
    }
    //添加一本书
    public void addBook() {
        Connection connection = null;
        PreparedStatement stmt = null;
        try {
            //获取数据库连接
            connection = dataSource.getConnection();
            //创建语句
            stmt = connection.prepareStatement(
                    "insert into soft_bookrack(book_name,book_author,book_isbn)
                    values( ?, ?,?)"
                );
            //参数绑定
            stmt.setString(1, "Spring in Action");
            stmt.setString(2, "Craig Walls");
            stmt.setString(3, "9787");
            //执行语句
            stmt.execute();
        } catch (SQLException e) {
            //处理异常信息
        } finally {
            //清理资源
            try {
                if (stmt != null) {
                    stmt.close();
                }

                if (connection != null) {
                    connnection.close();
                }
            } catch (SQLException e) {//
            }

        }
    }


    /**
     * 查询⼀本书
     */
    public void queryBook() {
        Connection connection = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        Book book = null;
        try {
            //获取数据库连接
            connection = dataSource.getConnection();
            //创建语句
            stmt = connection.prepareStatement(
                    "select book_name,book_author,book_isbn from soft_bookrack  where book_isbn=?"
            );
            //参数绑定
            stmt.setString(1, "11");
            //执行语句
            rs = stmt.executeQuery();
            if (rs.next()) {
                book = new Book();
                book.setName(rs.getString("book_name"));
                book.setAutor(rs.getString("book_author"));
                book.setIsbn(rs.getString("book_isbn"));
            }
            System.out.println(book);
        } catch (SQLException e) {
            //处理异常信息
        } finally {
            //清理资源
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                //
            }
        }
    }
    public static class Book {
        private String name;
        private String author;
        private String isbn;
        //省略 setter getter ⽅法
    }
}

三、MyBatis的配置

3.1 创建数据库和表

用MyBatis的方式来读取用户表中的所有用户,我们使用客户的数据库和数据包,SQL代码如下

--创建数据库
drop database if exists mycnblog;
create database mycnblog DEFAULT CHARACTER SET utf8mb4;
--使用数据
use mycnblog;
--创建表[用户表]
drop table if exists userinfo;
create table userinfo(
    id int primary key auto_increment,
    username varchar(100) not null,
    password varchar(32) not null,
    photo varchar(500) default ' ',
    createtime datetime default now(),
    updatetime datetime default now(),
    'state' int default 1
) default charset 'utf8mb4';
--创建文章表
drop table if exists articleinfo;
create table articleinfo(
    id int primary key auto_increment,
    title varchar (100) not null,
    content text not null,
    createtime datetime default now(),
    updatetime datetime default now(),
    uid int not null,
    rcount int not null default 1,
    'state' int default 1
)default charset 'utf8mb4';
--创建视频表
drop table if exists videoinfo;
create table videoinfo(
    vid int primary key,
    'title' varchar(250),
    'url' varchar(1000),
    createtime datetime default now(),
    updatetime datetime default now(),
    uid int
)default charset 'utf8mb4';
--添加一个用户信息
INSERT INTO 'userinfo'('id','username','password','photo','createtime','updatetime','state')
VALUES (1,'admin','admin','','2023-12-06 17:10:48','2024-12-06 17:10:48',1);
--文章添加测试数据
insert into articleinfo(title, content, uid)
    values('Java','Java正文',1);
--添加视频
insert into videoinfo(vid,title,url,uid) values (1,'java title','wwww.baidu.com',1);

3.2 添加MyBatis框架支持

老项目添加MyBatis:

<!-- 添加 MyBatis 框架 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- 添加 MySQL 驱动 -->
<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
        <scope>runtime</scope>
</dependency>

新项目添加MyBatis:

创建 Spring Boot 项⽬的时候添加引⽤就可以了,如下图所示:

3.3 配置 MyBatis

此步骤需要进⾏两项设置,数据库连接配置和 MyBatis 的 XML ⽂件配置

1.数据库的连接配置

在application.yml 添加以下代码

# 数据库连接配置
spring:
    datasource:
        url: jdbc:mysql://localhost:3306/mycnblog?characterEncoding=utf8&useSSL=false
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver

2.配置MyBatis中的XML保存路径与命名格式

MyBatis 的XML中保存是查询数据库的具体操作SQL,配置如下:

#配置mybatis xml的文件路径。
#在resource/mapper创建的所有表的xml文件
mybatis:
    mapper-locations=classpath:mybatis/*Mapper.xml
 # *Mapper代表的是任意长度的字符串,并且以Mapper.xml结尾

四、MyBatis添加业务代码

这是MyBatis查询所有用户功能的流程图:

4.1 添加实体类

先添加用户的实体类:

import lombok.Data;
import java.util.Date;
@Data
public class User {
private Integer id;
private String username;
private String password;
private String photo;
private Date createTime;
private Date updateTime;
}

4.2 添加mapper接口

有了实体类,在接口中声明方法

数据持久层的接口定义:

import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
    public List<User> getAll();//获得所有用户信息
}

4.3 在对应路径下 添加UserMapper.xml

1.位置再之前创建的 resources.mybatis 目录下

2.注意规范命名方式

数据持久层的表现。mybatis的固定xml格式:

mapper属性namespace就对应我们上面的UserMapper接口

<? xml version="1.0" encoding="UTF-8">
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0 //EN"
"http://mybatis.org/dtd/mabatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
</mapper>

4.4在 XML实现方法

UserMapper.xml 查询所有⽤户的具体实现 SQL:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybati
s.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="getAll" resultType="com.example.demo.model.User">
    select * from userinfo
</select>
</mapper>

标签说明:

<mapper>标签:需要指定 namespace 属性,表示命名空间,值为 mapper 接⼝的全限定名,包括全包名.类名。

<select>查询标签:是⽤来执⾏数据库的查询操作的:

        id:是和 Interface(接⼝)中定义的⽅法名称⼀样的,表示对接⼝的具体实现⽅法。

        resultType:是返回的数据类型,也就是开头我们定义的实体类

4.5 添加Service

服务层实现代码如下:

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserService {
@Resource
    private UserMapper userMapper;
    public List<User> getAll() {
    return userMapper.getAll();
    }
}

4.6添加Controller

控制器层的实现代码如下:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/u")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/getall")
    public List<User> getAll(){
    return userService.getAll();
    }
}

然后就可以启动看到访问效果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值