思路:搭建环境–>导入Mybatis–>编写代码–>测试!
搭建数据库
CREATE DATABASE `mybatis`; USE `mybatis`;
INSERT INTO `user`(`id`,`name`,`pwd`) VALUES ('5', '玲玲', '789741')
新建项目
新建一个普通的Maven项目
删除src目录
导入maven依赖
<?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>org.example</groupId> <artifactId>Mybatis_Study</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <!--导入依赖--> <dependencies> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <!--mybatis--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
编写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核心配置文件--> <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=true&userUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!--每一个Mapper.XML都需要Mybatis核心配置文件中注册--> <mappers> <mapper resource="com/shuang/dao/UserMapper.xml"/> </mappers> </configuration>
编写mybatis的工具类
package com.shuang.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; //sqlSessionFactory sqlSession public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { //使用Mybatis第一步:获取SqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。 // SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
package com.shuang.pojo; public class User { private int id; public String name; public String ped; public User(){ } public User(int id, String name, String ped) { this.id = id; this.name = name; this.ped = ped; } 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 getPed() { return ped; } public void setPed(String ped) { this.ped = ped; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", ped='" + ped + '\'' + '}'; } }
Dao接口
package com.shuang.dao; import com.shuang.pojo.User; import java.util.List; 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="com.shuang.dao.UserDao"> <!--查询语句--> <select id="getUserList" resultType="com.shuang.pojo.User"> select * from mybatis.m_user </select> </mapper>
<!--每一个Mapper.XML都需要Mybatis核心配置文件中注册--> <mappers> <mapper resource="com/shuang/dao/UserMapper.xml"/> </mappers>
MapperRegistry是什么?
核心配置文件中注册mappers
junit测试
package com.shuang.dao; import com.shuang.pojo.User; import com.shuang.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserDaoTest { @Test public void test(){ //1.获得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(); } }
可能会遇到的问题: