1.项目结构
2.引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion >
<groupId > com.fy</groupId >
<artifactId > mybatistest</artifactId >
<version > 1.0-SNAPSHOT</version >
<dependencies >
<dependency >
<groupId > org.mybatis</groupId >
<artifactId > mybatis</artifactId >
<version > 3.5.7</version >
</dependency >
<dependency >
<groupId > mysql</groupId >
<artifactId > mysql-connector-java</artifactId >
<version > 8.0.26</version >
</dependency >
<dependency >
<groupId > junit</groupId >
<artifactId > junit</artifactId >
<version > 4.13.2</version >
</dependency >
</dependencies >
<build >
<resources >
<resource >
<directory >
src/main/java
</directory >
<includes >
<include > **/*.properties</include >
<include > **/*.xml</include >
</includes >
<filtering > true</filtering >
</resource >
</resources >
</build >
</project >
3.创建数据库
create database mybatis;
4.创建数据库配置文件
mysql.driver =com.mysql.cj.jdbc.Driver
mysql.url =jdbc:mysql://localhost:3306 /mybatis?serverTimezone=UTC&\
characterEncoding =utf8&useUnicode=true &useSSL=false
mysql.username =root
mysql.password =123456
5.创建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 >
<properties resource ="db.properties" />
<typeAliases >
<package name ="com.pojo" />
</typeAliases >
<environments default ="development" >
<environment id ="development" >
<transactionManager type ="JDBC" />
<dataSource type ="POOLED" >
<property name ="driver" value ="${mysql.driver}" />
<property name ="url" value ="${mysql.url}" />
<property name ="username" value ="${mysql.username}" />
<property name ="password" value ="${mysql.password}" />
</dataSource >
</environment >
</environments >
<mappers >
<mapper resource ="mapper/UserMapper.xml" />
</mappers >
</configuration >
6.创建用户表并添加数据
use mybatis;
create table users(
id int primary key auto_increment,
name varchar (20 ) not null ,
age int not null
);
insert into users(id,name,age) values (null ,'李四' ,20 ),(null ,'王五' ,18 );
7.创建pojo实体类
package com.pojo ;
public class User {
private int id;
private String name;
private String age;
public User () {
}
public User (int id, String name, String age) {
this .id = id;
this .name = name;
this .age = age;
}
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 getAge ( ) {
return age;
}
public void setAge (String age ) {
this .age = age;
}
@Override
public String toString ( ) {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
'}' ;
}
}
8.创建UserDao接口
package com.dao;
import com.pojo.User;
public interface UserDao {
public User findById (int id) ;
}
9.创建映射文件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.dao.UserDao" >
<select id ="findById" parameterType ="int" resultType ="user" >
SELECT * FROM user WHERE id=#{id}
</select >
</mapper >
10.编写测试类
import com.pojo.User;
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 org.junit.Test;
import java.io.IOException;
import java.io.Reader;
public class UserTest {
@Test
public void userFindByIdTest () {
Reader reader = null ;
SqlSession session = null ;
try {
reader = Resources.getResourceAsReader("mybatis-config.xml" );
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder ().build(reader);
session = sqlMapper.openSession();
User user = session.selectOne("findById" ,1 );
System.out.println(user);
}catch (IOException e){
e.printStackTrace();
}finally {
session.close();
}
}
}
11.运行结果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南