type
status
date
slug
summary
tags
category
icon
password

一、搭建环境

创建数据库表

CREATE TABLE `user` ( `id` int(20) NOT NULL primary key, `name` varchar(30) DEFAULT NULL, `password` varchar(30) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `user` (`id`, `name`, `password`) VALUES (1, '内河', '123456'), (2, '张三', '123456'), (3, '李四', '123456');
新建项目

导入依赖

<dependencies> <!--junit依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--mybatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!--mysql依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> </dependencies>

二、创建一个模块

在resources文件夹下编写Mybatis的核心配置文件mybatis-config.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核心配置文件--> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!--导入jdbc--> <property name="driver" value="com.mysql.jdbc.Driver"/> <!--数据库链接--> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSl=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"/> <!--数据库用户名--> <property name="username" value="root"/> <!--数据库密码--> <property name="password" value="root"/> </dataSource> </environment> </environments> <!--每一个mapper.xml都需要在Mybatis核心文件中注册!--> <mappers> <!--注册地址--> <mapper resource="top/ltyzqhh/dao/UserMapper.xml"/> </mappers> </configuration>

编写Mybatis工具类

package top.ltyzqhh.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; //sqlSeessionFactory-->sqlSession public class Mybatis_utils { private static SqlSessionFactory sqlSessionFactory; static { try { //工具类第一步:获取sqlSesscionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } //有了sqlSessionFactory,就可以从中获得SqlSession的实例 //SqlSession完全包含了面向数据库执行SQL命令所需要的所有方法 } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }

编写实体类

package top.ltyzqhh.pojo; public class User { private int id; private String name; private String password; public User() { } public User(int id, String name, String password) { this.id = id; this.name = name; this.password = password; } 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 getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }

编写一个接口

public interface UserDao { List<User> getUserList(); }

接口实现类由原来的UserDaoImpl转变为一个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 namespace="top.ltyzqhh.dao.UserDao"> <!--id=接口的方法名字,resultType=返回单个结果集 resultMap=多个结果集,--> <select id="getUserList" resultType="top.ltyzqhh.pojo.User"> select * from mybatis.user; </select> </mapper>

三、测试

junit测试

public class UserDaoTest { @Test public void test(){ //第一步:获取SqlSession对象 SqlSession sqlSession = Mybatis_utils.getSqlSession(); //执行sql UserDao mapper = sqlSession.getMapper(UserDao.class); List<User> userList = mapper.getUserList(); for (User user : userList) { System.out.println(user.toString()); } //关闭SqlSession sqlSession.close(); } }

测试结果

没配置Mapper注册表
org.apache.ibatis.binding.BindingException: Type interface top.ltyzqhh.dao.UserDao is not known to the MapperRegistry.
xml文件未加载问题,因为maven约定大于配置 在pom.xml上配置一下resources
java.lang.ExceptionInInitializerError ### Error building SqlSession. ### The error may exist in top/ltyzqhh/dao/UserMapper.xml ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configu
 

最后运行结果

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. Sat Apr 16 17:45:28 CST 2022 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. User{id=1, name='内河', password='123456'} User{id=2, name='张三', password='123456'} User{id=3, name='李四', password='123456'}
notion image
算法排序Mybatis简介