文章目录
- 准备
- mysql
- 开始
- 新建maven项目
- maven添加引用
- mybatis配置文件
- 工具类
- 创建实例类
- 添加mapper
- mapper
- mapper.xml
- 测试类
- 发现问题
- org.apache.ibatis.binding.BindingException: Type interface com.cpyy.mapper.UserMapper is not known to the MapperRegistry.
- The error may exist in com/cpyy/mapper/UserMapper.xml
- 参考
准备
Java 18
mysql 8
maven
idea
mysql
准备数据
CREATE DATABASE `mybatis`;
USE `mybatis`;
CREATE TABLE `user`(
`id` int not null PRIMARY KEY,
`name` varchar(30) DEFAULT NULL,
`pwd` varchar(30) DEFAULT NULL
)ENGINE=INNODB,CHARSET=utf8;
INSERT INTO `mybatis`.`user` (`id`, `name`, `pwd`)
VALUES
(1, 'haha', '123'),
(2, 'lala', '123'),
(3, 'xixi', '1234');
开始
新建maven项目
这个就不用详细说了
新建maven项目,然后新建一个module作为第一个mybatis程序
整体文件目录如下
maven添加引用
在外面一层的pom添加引用
mysql-connect
mybatis
junit(用于测试代码)
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
mybatis配置文件
在resource文件夹下添加mybatis配置文件mybatis-config.xml
mappers页签中的内容可以先不写,等创建mapper后再添加
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://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/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTime=Asia/shanghai"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/cpyy/mapper/UserMapper.xml"/>
</mappers>
</configuration>
工具类
创建utils工具类,方便代码复用
package com.cpyy.utils;
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 MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
创建实例类
创建实体类,和mysql表中的信息保持一致
(未包含构造器等代码)
public class User {
private int id;
private String name;
private String pwd;
}
添加mapper
创建mapper后可以在mybatis-config.xml中补充相应内容
mapper
package com.cpyy.mapper;
import com.cpyy.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> getUserList();
}
mapper.xml
mapper.xml中的namespace为上方mapper的路径
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cpyy.mapper.UserMapper">
<select id="getUserList" resultType="com.cpyy.pojo.User">
select * from mybatis.user
</select>
</mapper>
测试类
在test目录中创建测试类,可以直接进行测试
package com.cpyy.mapper;
import com.cpyy.pojo.User;
import com.cpyy.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
@Test
public void getUserListTest() {
try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
正常结果如下
发现问题
org.apache.ibatis.binding.BindingException: Type interface com.cpyy.mapper.UserMapper is not known to the MapperRegistry.
在mybatis-config.xml中没有记录mapper信息,补充即可
The error may exist in com/cpyy/mapper/UserMapper.xml
在target中没有对应的UserMapper.xml文件,没有进行导出
在pom文件中添加文件过滤的内容
<build>
<resources>
<resource>
<directory>src/main/resource</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>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
参考
mybatis官方文档