MyBatis 操作数据库(⼊⻔)--JDBC 操作

📅 2026/7/17 6:41:29 👁️ 阅读次数 📝 编程学习
MyBatis 操作数据库(⼊⻔)--JDBC 操作

JDBC 操作⽰例

我们先来回顾⼀下JDBC的操作流程:

1. 创建数据库连接池DataSource 2. 通过DataSource获取数据库连接Connection

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

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

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

6. 使⽤Statement执⾏SQL语句

7. 查询操作:返回结果集ResultSet,更新操作:返回更新的数量

8. 处理结果集

9. 释放资源 下⾯的⼀个完整案例,展⽰了通过JDBC的API向数据库中添加⼀条记录,修改⼀条记录,查询⼀条 记录的操作。

-- 创建数据库 create database if not exists library default character set utf8mb4; -- 使⽤数据库 use library; -- 创建表 create table if not exists soft_bookrack ( book_name varchar(32) NOT NULL, book_author varchar(32) NOT NULL, book_isbn varchar(32) NOT NULL primary key );

以下是JDBC操作的具体实现代码:

package com.example.demo.mapper; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; 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, "9787115417305"); //执⾏语句 stmt.execute(); } catch (SQLException e) { //处理异常信息 } finally { //清理资源 try { if (stmt != null) { stmt.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { // } } } /** * 更新⼀本书 */ public void updateBook() { Connection connection = null; PreparedStatement stmt = null; try { //获取数据库连接 connection = dataSource.getConnection(); //创建语句 stmt = connection.prepareStatement( "update soft_bookrack set book_author=? where book_isbn=?;" ); //参数绑定 stmt.setString(1, "张卫滨"); stmt.setString(2, "9787115417305"); //执⾏语句 stmt.execute(); } catch (SQLException e) { //处理异常信息 } finally { //清理资源 try { if (stmt != null) { stmt.close(); } if (connection != null) { connection.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, "9787115417305"); //执⾏语句 rs = stmt.executeQuery(); if (rs.next()) { book = new Book(); book.setName(rs.getString("book_name")); book.setAuthor(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 ⽅法 } }

从上述代码和操作流程可以看出,对于JDBC来说,整个操作⾮常的繁琐,我们不但要拼接每⼀个参 数,⽽且还要按照模板代码的⽅式,⼀步步的操作数据库,并且在每次操作完,还要⼿动关闭连接 等,⽽所有的这些操作步骤都需要在每个⽅法中重复书写.那有没有⼀种⽅法,可以更简单、更⽅便的 操作数据库呢?

答案是肯定的,这就是我们要学习MyBatis的真正原因,它可以帮助我们更⽅便、更快速的操作数据 库.