第一个Mybatis项目
第一个Mybatis项目
1、环境搭建
1.1、创建测试数据库
create database mybatis;
use mybatis;
create table user(
id int not null primary key ,
name varchar(20) not null ,
pwd varchar(20) not null
);
insert into user(id, name, pwd) VALUES (1,'tom', '123456');
insert into user(id, name, pwd) VALUES (2,'sam', '123456');
insert into user(id, name, pwd) VALUES (3,'smith', '123456');
1.2、创建maven项目
导入maven依赖(pom.xml)
- mysql-connector-java(Java连接数据库)
- mybatis(Mybatis框架)
- junit(测试)
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
2、构建SqlSessionFactory
2.1、从 XML 中构建SqlSessionFactory
- 创建mybatis-config.xml(Mybatis核心配置文件)
<?xml version="1.0" encoding="UTF-8" ?>
<!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.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--注册mapper-->
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
XML 配置文件中包含了对 MyBatis 系统的核心设置:
- 获取数据库连接实例的数据源(DataSource)
- 决定事务作用域和控制方式的事务管理器(TransactionManager)
-
创建SqlSessionFactory(这里是java代码)
String resource = "mybatis-config.xml";//配置文件路径 InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
注意:建议将这里提取成工具类,提高代码复用性
2.2、不使用XML构建SqlSessionFactory
如果你更愿意直接从 Java 代码而不是 XML 文件中创建配置,或者想要创建你自己的配置建造器,MyBatis 也提供了完整的配置类,提供了所有与 XML 文件等价的配置项。
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);//注册mapper
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
3、编写代码
3.1、User实体类
package com.mybatis.example;
public class User {
private int id;
private String name;
private String pwd;
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public User() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
3.2、DAO接口
public interface UserMapper {
List<User> getUserList();
}
3.3、创建UserMapper类
public interface UserMapper {
List<User> getUserList();
}
3.4、UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.example.UserMapper">
<select id="getUserList" resultType="com.mybatis.example.User">
select * from mybatis.user;
</select>
</mapper>
3.5、MyTest测试类
public class MyTest {
@Test
public void test01() {
//1. 获取SqlSessionFactory
String resource = "mybatis-config.xml";//配置文件路径
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//3. 测试
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
//4. 关闭sqlSession
sqlSession.close();
}
}
创建图:
第一个Mybatis项目创建成功!
Mybatis官方文档(中文):https://mybatis.net.cn/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!