1、建项目
2、导包
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
配置mybatis-config.xml文件 <?xml version="1.0" encoding="UTF-8" ?>
<!-- defaultTransactionIsolationLevel – 默认的连接事务隔离级别。–>-->
<!-- <property name="defaultTransactionIsolationLevel" value=""/>-->
<!-- defaultNetworkTimeout – 等待数据库操作完成的默认网络超时时间(单位:毫秒)–>-->
<!-- <property name="efaultNetworkTimeout" value=""/>-->
</dataSource>
</environment>
</environments>
<mappers>
<!-- 使用相对于类路径的资源引用 -->
<mapper resource="cn/zhy/dao/UserMapper.xml"/>
<!-- 使用完全限定资源定位符(URL)
不推荐使用
<mapper url="E:\JetBrains\mybatis学习\Mybatis-study\Mybatis-03\src\main\java\asia\xiaojiang\mybatis03\dao\UserMapper.xml"/>
-->
<!-- 使用映射器接口实现类的完全限定类名
使用注意点:
接口和其配置文件必须同名, 必须在同一个包下
-->
<!-- <mapper class="cn.zhy.dao.UserMapper"/>-->
<!-- 将包内的映射器接口实现全部注册为映射器
使用包扫描注意点:
接口和其配置文件必须同名, 必须在同一个包下
-->
<!-- <package name="asia.xiaojiang.mybatis03.dao"/>-->
</mappers>
编写MybatisUtils工具类
package cn.zhy.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;
/**
* @author D_sir
* @createDate 2023-03-20 21:03
*/
//sq1SessionFactory-->sqlSession
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) {
throw new RuntimeException(e);
}
}
//既然有了sqLSessionFactory,顾名思义,我们就可以从中获得SqLSession 的实例了。
// SqLSession完全包含了面向数据库执行sQL命令所需的所有方法。
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
编写代码
User类
package cn.zhy.pojo;
/**
* @author D_sir
* @createDate 2023-03-20 21:23
*/
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 + '\'' +
'}';
}
}
Dao接口
package cn.zhy.dao;
import cn.zhy.pojo.User;
import java.util.List;
/**
* @author D_sir
* @createDate 2023-03-20 21:24
*/
public interface UserDao {
List<User> getUserList();
}
接口实现类由原来的UserMapperImpl转变为一个Mapper配置文件
<?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" >
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<!--mapper文件相当于实现dao接口-->
<mapper namespace="cn.zhy.dao.UserDao">
<!--select id相当于重写dao接口方法的名字-->
<!-- resultType返回一个值-->
<select id="getUserList" resultType="cn.zhy.pojo.User">
select *
from mybatis.user;
</select>
</mapper>
测试
package cn.zhy.dao;
import cn.zhy.pojo.User;
import cn.zhy.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
/**
* @author D_sir
* @createDate 2023-03-20 21:45
*/
public class UserDaoTest {
@Test
public void test() {
//第一步获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//执行SQL
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭SqlSession
sqlSession.close();
}
}
注意点:绑定异常
● 可能遇到的问题
○ 1、配置文件没有注册
○ 2、绑定接口错误
○ 3、方法名不对
○ 4、返回值类型不对
○ 5、Maven导出资源问题
org.apache.ibatis.binding.BindingException: Type interface cn.zhy.dao.UserDao is not known to the MapperRegistry.