MyBatis 是一个优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects)映射成数据库中的记录。
MyBatis 的几个主要特点:
原生 JDBC(Java Database Connectivity)是指 Java 数据库连接,它是一个用于 Java 程序和各种数据库之间进行交互的 API。JDBC 提供了一种标准方法来访问数据库,允许 Java 应用程序以一种统一的方式与不同的数据库进行通信。
package com.wyl.mybatis.service;
import java.sql.*;
/**
* @Description
* @Author WuYiLong
* @Date 2024/2/26 11:36
*/
public class JdbcService {
/**
* 数据库链接
*/
private static final String url = "jdbc:mysql://localhost:3306/blog?zeroDateTimeBehavior=CONVERT_TO_NULL&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=GMT%2B8";
/**
* 用户名
*/
private static final String username = "root";
/**
* 密码
*/
private static final String password = "123456";
public static void main(String[] args) {
try {
// 加载所需的 JDBC 驱动,这通常是通过调用 Class.forName() 方法来完成的
Class.forName("com.mysql.cj.jdbc.Driver");
// 使用 DriverManager.getConnection() 方法建立与数据库的连接。
Connection connection = DriverManager.getConnection(url, username, password);
// 创建一个 Statement 或 PreparedStatement 对象来执行 SQL 语句。
String sql = "SELECT * FROM `b_expert` limit 10;";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.executeQuery(sql);
// 如果是查询操作,执行 executeQuery() 方法,并处理返回的 ResultSet。
ResultSet resultSet = preparedStatement.executeQuery(sql);
// 通过遍历,输出数据
while (resultSet.next()) {
System.out.println("专家名称:"+resultSet.getString("name"));
}
// 关闭 ResultSet、Statement 和 Connection,以释放数据库资源。
resultSet.close();
preparedStatement.close();
connection.close();
} catch (SQLException | ClassNotFoundException throwables) {
System.out.println(throwables.getMessage());
}
}
}

CREATE TABLE `d_full_city` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(255) DEFAULT NULL COMMENT '名称',
`code` varchar(255) DEFAULT NULL COMMENT '区域码',
`full_name` varchar(255) DEFAULT NULL COMMENT '全名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=46476 DEFAULT CHARSET=utf8mb4 COMMENT='省市区-字典';
package com.wyl.mybatis.entity;
/**
* @Description 省市区-字典
* @Author wuyilong
* @Date 2024-02-26
*/
public class FullCity {
private static final long serialVersionUID = 1L;
/**
* 主键ID
*/
private Long id;
/**
* 名称
*/
private String name;
/**
* 区域码
*/
private String code;
/**
* 全名称
*/
private String fullName;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
package com.wyl.mybatis.mapper;
import com.wyl.mybatis.entity.FullCity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* @Description 省市区-字典
* @Author wuyilong
* @Date 2024-02-26
*/
@Mapper
public interface FullCityMapper {
/**
* 根据名称查询
* @param name
* @return
*/
FullCity selectByName(@Param("name") String name);
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wyl.mybatis.mapper.FullCityMapper">
<resultMap id="BaseResultMap" type="com.wyl.mybatis.entity.FullCity">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="code" property="code"/>
<result column="full_name" property="fullName"/>
resultMap>
<select id="selectByName" resultType="com.wyl.mybatis.entity.FullCity">
select * from d_full_city where name = #{name}
select>
mapper>
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="mybatis-mysql.properties">
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="driver" value=""/>
<property name="url" value=""/>
properties>
<settings>
<setting name="cacheEnabled" value="true"/>
settings>
<environments default="development">
<environment id="development">
<transactionManager type="jdbc">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="./mapper/ExpertMapper.xml" />
mappers>
configuration>
username=root
password=123456
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/blog?zeroDateTimeBehavior=CONVERT_TO_NULL&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=GMT%2B8
package com.wyl.mybatis.service;
import com.wyl.mybatis.entity.FullCity;
import com.wyl.mybatis.mapper.FullCityMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
/**
* @Description
* @Author WuYiLong
* @Date 2024/2/26 16:04
*/
public class MybatisService {
public static void main(String[] args) throws IOException {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
FullCityMapper mapper = sqlSession.getMapper(FullCityMapper.class);
FullCity fullCity = mapper.selectByName("广东省");
System.out.println("城市的名称:"+fullCity.getName());
}
}

JDBC 提供了最底层的访问数据库的方式,直接使用SQL语句,灵活但需要手动管理连接和事务。
MyBatis 是在JDBC之上的一个抽象层,隐藏了如何连接数据库、如何处理事务、如何关闭流、自动提交,通过ORM提供了更高层次的数据库操作,通过编写简单的sql就可以完成增删改查,简化了开发流程,更适合于复杂的SQL操作和动态SQL需求。
所以在选择使用JDBC还是MyBatis时,通常考虑应用的复杂性、性能需求和开发效率等因素。对于简单的数据库操作,JDBC可能更直接高效;而对于复杂的业务逻辑和大量的数据库交互,MyBatis提供的抽象和便利可能会更加合适。
