创建数据库
CREATE DATABASE mybatis;
USE mybatis;
CREATE TABLE USER ( id INT NOT NULL PRIMARY KEY,
NAME VARCHAR (64 ) NOT NULL ,
pwd VARCHAR (64 ) NOT NULL )
ENGINE= INNODB DEFAULT CHARSET= utf8;
INSERT INTO USER (id,NAME,pwd)VALUE (1 ,'张三' ,'123123' );
INSERT INTO USER (id,NAME,pwd)VALUES (2 ,'小落' ,'12341234' ),(3 ,'李四' ,'1231233' );
创建项目
1.创建一个普通的Maven项目
2.删除src目录,形成父工程
3.导入需要的包
<dependency >
<groupId > mysql</groupId >
<artifactId > mysql-connector-java</artifactId >
<version > 5.1.47</version >
</dependency >
<dependency >
<groupId > org.mybatis</groupId >
<artifactId > mybatis</artifactId >
<version > 3.4.6</version >
</dependency >
<dependency >
<groupId > junit</groupId >
<artifactId > junit</artifactId >
<version > 4.12</version >
<scope > test</scope >
</dependency >
4.编辑包的结构
5.编写Mybatis.xml核心配置文件
<?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 ="root" />
</dataSource >
</environment >
</environments >
<mappers >
<mapper resource ="com/Google/Dao/userMapper.xml" > </mapper >
</mappers >
</configuration >
5.1报错
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 449 milliseconds ago. The last packet sent successfully to the server was 442 milliseconds ago.
这里的问题出在数据库,所以检查数据库连接。发现useSSL=true
要改为useSSL=false;
6.编写工具类(getSession)
package com.Google.units;
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;
public class sqlSessionFactory {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "Mybatis.xml" ;
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder ().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getsqlSession () {
return sqlSessionFactory.openSession();
}
}
7.编写实体类
package com.Google.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User () {
}
public User (int id, String name, String pwd) {
this .id = id;
this .name = name;
this .pwd = pwd;
}
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 + '\'' +
'}' ;
}
}
8.编写接口
package com.Google.Dao;
import com.Google.pojo.User;
import java.util.List;
public interface userMapper {
List<User> getUserList () ;
}
9.编写Mapper.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.Google.Dao.userMapper" >
<select id ="getUserList" resultType ="com.Google.pojo.User" >
select * from user
</select >
</mapper >
10测试
package com.Google.Dao;
import com.Google.pojo.User;
import com.Google.units.sqlSessionFactory;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class userDao {
@Test
public void test () throws Exception{
SqlSession sqlSession = sqlSessionFactory.getsqlSession();
userMapper md = sqlSession.getMapper(userMapper.class);
List<User> userList = md.getUserList();
for (User user : userList) {
System.out.println(user);
}
}
}
注意点
1.先把需要配置的再区写测试,防止测试类的名字和Dao一样,这样先写测试类,然后再去配置文件,可能会把测试类配置到Mybatis配置文件中,这样就会报这样的错
type class com .google.dao.usermapper is not known to the mapperregistry.
2.由于Maven中约定大于配置,所以我们配置的文件可能会被过滤掉,只需要加入这些代码,就可解决这样的问题
<build >
<resources >
<resource >
<directory > src/main/resources</directory >
<includes >
<include > **/*.properties</include >
<include > **/*.xml</include >
</includes >
<filtering > true</filtering >
</resource >
<resource >
<directory > src/main/java</directory >
<includes >
<include > **/*.properties</include >
<include > **/*.xml</include >
</includes >
<filtering > true</filtering >
</resource >
</resources >
</build >
3.最常见的错误
org.apache.ibatis.binding.BindingException: Type interface com .Google.Dao.userMapper is not known to the MapperRegistry.
这里就是要在Mybatis.xml和Dao目录下的配置文件一起配置,具体如下
<mappers >
<mapper resource ="com/Google/Dao/userMapper.xml" > </mapper >
</mappers >
<mapper namespace ="com.Google.Dao.userMapper" >
<select id ="getUserList" resultType ="com.Google.pojo.User" >
select * from user
</select >
</mapper >
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理