# schema.sql 文件
DROP TABLE IF EXISTS `article`;
CREATE TABLE `article`
(
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_id` int NOT NULL COMMENT '作者 ID',
`title` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '文章标题',
`summary` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '文章概要',
`read_count` int(11) UNSIGNED ZEROFILL NOT NULL COMMENT '阅读读数',
`create_time` datetime NOT NULL COMMENT '创建时间',
`update_time` datetime NOT NULL COMMENT '最后修改时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB
AUTO_INCREMENT = 1
CHARACTER SET = utf8mb4
COLLATE = UTF8MB4_0900_AI_CI
ROW_FORMAT = Dynamic;
DROP TABLE IF EXISTS `article_detail`;
CREATE TABLE `article_detail`
(
`id` int NOT NULL AUTO_INCREMENT COMMENT '注解',
`article_id` int NOT NULL COMMENT '文章 ID',
`content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '文章内容',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB
AUTO_INCREMENT = 1
CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci
ROW_FORMAT = Dynamic;
# data.sql 文件
-- ----------------------------
-- Records of article
-- ----------------------------
INSERT INTO `article`
VALUES (1, 2021, 'MyBatis概述', 'MyBatis是半自动的ORM框架', 654897, '2024-01-26 03:11:12',
'2024-02-25 12:11:19');
INSERT INTO `article`
VALUES (2, 2324, 'Spring容器', 'IOC、AOP', 345, '2025-01-19 15:15:57', '2025-02-16 4:19:30');
-- ----------------------------
-- Records of article_detail
-- ----------------------------
INSERT INTO `article_detail`
VALUES (1, 1, 'MyBatis是非常优秀的持久层框架');
<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.0modelVersion>
<groupId>com.gdbgroupId>
<artifactId>mybatis-codeartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<properties>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.13version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.33version>
dependency>
dependencies>
project>
在resources根⽬录下新建mybatis-config.xml配置⽂件(可以参考mybatis⼿册拷⻉)。
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/使用的数据库名"/>
<property name="username" value="root"/>
<property name="password" value="自己的数据库密码!"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource=""/>
mappers>
configuration>
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="article">
<insert id="insertArticle">
insert into article values
(null, 9874, 'JavaWeb', 'Filter', 645133, '2026-05-16 12:15:27', '2026-08-16 4:15:30');
insert>
mapper>
<mappers>
<mapper resource="articleMapper.xml"/>
mappers>
public class TestMyBatis {
public static void main(String[] args) throws Exception{
//获取 SqlSessionBuilder 对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//获取 SqlSessionFactory 对象,Resources.getResourceAsReader()默认从类的根路径下加载文件。
//Resources是MyBatis提供的工具类,源码是 ClassLoder.getSystemClassLoader().getResourceAsStream("类路径下的文件路径");
SqlSessionFactory sqlSessionFactory = builder.build(Resources.getResourceAsReader("mybatis-config.xml"));
//获取 SqlSession 对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行 SQL 语句
int count = sqlSession.insert("insertArticle");
System.out.println("插入的记录条数:====> " + count);
//MyBatis默认关闭的自动提交机制的
sqlSession.commit();
//释放资源
sqlSession.close();
}
}
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
settings>
public class TestMyBatis {
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
// 1.创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 2.创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder
.build(Resources.getResourceAsStream("mybatis-config.xml"));
// 3.创建SqlSession对象
sqlSession = sqlSessionFactory.openSession();
// 4.执⾏SQL
int count = sqlSession.insert("insertArticle");
System.out.println("插入了⼏条记录:" + count);
// 5.提交
sqlSession.commit();
} catch (Exception e) {
// 回滚
if (sqlSession != null) {
sqlSession.rollback();
}
e.printStackTrace();
} finally {
// 6.关闭
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
/**
* 类加载时初始化sqlSessionFactory对象
*/
static {
try {
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 每调⽤⼀次openSession()可获取⼀个新的会话,该会话⽀持⾃动提交。
*/
public static SqlSession openSession() {
return sqlSessionFactory.openSession(true);
}
}
public class TestMyBatis {
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtils.openSession();
// 4.执⾏SQL
int count = sqlSession.insert("insertArticle");
System.out.println("插入了⼏条记录:" + count);
// 5.提交
sqlSession.commit();
} catch (Exception e) {
// 回滚
if (sqlSession != null) {
sqlSession.rollback();
}
e.printStackTrace();
} finally {
// 6.关闭
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
<transactionManager type="JDBC"/>
SqlSession sqlSession = sqlSessionFactory.openSession(true); //关闭事务(自动提交)
SqlSessionFactoryBuilder --> SqlSessionFactory --> SqlSession