目录
1、新建Maven项目工程的详细步骤【myBatis-study】
2、在pom.xml配置文件中添加MyBatis组件依赖:pom.xml
3、在数据库中创建表并插入测试数据:RUNOOB.TBL_Employee
4、编写与表对应的实体类:EmployeeEntity.java
6、编写与表对应的mapper配置文件(对表操作的各种SQL):mapper.xml
7、编写MyBatis的全局配置文件(在全局配置文件中注册mapper.xml文件):mybatis-config.xml
0、写在前面
(1)本系列博客的目标是:MyBatis配置文件编写,MyBatis动态SQL,MyBatis缓存机制,MyBatis-Spring整合,MyBatis逆向工程,MyBatis高级内容(MyBatis源码解析,MyBatis单/多插件运行机制,MyBatis四大对象工作原理,自定义TypeHandler、MyBatis存储过程&游标处理等)。
(2)MyBatis官网:https://mybatis.org/mybatis-3/zh/index.html
(3)下载最新的MyBatis组件和源码:https://github.com/mybatis/mybatis-3/releases
1、新建Maven项目工程的详细步骤【myBatis-study】
(1)file -》 new -》 project
(2)选择 maven工程,选择项目使用的JDK
(3)填写Maven项目的groupID与artifactID
(4)填写Maven项目的工程名,选择Maven项目的地址,最后点击 finish 即可
(5)创建好之后,最初的Maven项目结构图
2、在pom.xml配置文件中添加MyBatis组件依赖:pom.xml
<?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>com.wind</groupId>
<artifactId>mybatis-study</artifactId>
<version>1.0-SNAPSHOT</version>
<!--导入使用MyBatis时相关的依赖=start-->
<dependencies>
<!--MyBatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--MySQL连接依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!--第三方缓存依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<!--lombok插件依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<!--导入使用MyBatis时相关的依赖=end-->
<!--编译之后的文件中少了mapper.xml,这个和maven有关,maven编译src/java代码的时候,默认只会对java文件进行编译然后放在target/classes目录,需要在pom.xml中加入下面配置-->
<!--如果不添加此节点,mapper.xml文件、config.properties文件、config.spring文件都不会被加载到target的classes中去,也就不能被使用,也就会报错-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
3、在数据库中创建表并插入测试数据:RUNOOB.TBL_Employee
CREATE TABLE `TBL_Employee` (
`Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户Id',
`Name` varchar(255) NOT NULL DEFAULT '' COMMENT '姓名',
`Gender` char(1) NOT NULL DEFAULT '' COMMENT '性别',
`Email` varchar(255) NOT NULL DEFAULT '' COMMENT '邮箱',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='员工表';
insert into TBL_Employee(name,gender,email) values("张三","男","123@qq.com"),("李四","女","456@qq.com"),("王二","女","789@qq.com");
select * from TBL_Employee;
4、编写与表对应的实体类:EmployeeEntity.java
package com.wind.entity;
import java.io.Serializable;
public class EmployeeEntity implements Serializable {
private static final long serialVersionUID = -5639704456233181138L;
private Integer id;
private String name;
private String gender;
private String email;
// 省略各种setter、getter、toString方法
}
5、编写与表对应的接口:EmployeeDao.java
package com.wind.dao;
import com.wind.entity.EmployeeEntity;
import org.apache.ibatis.annotations.Param;
/**
* @Description: 这是与MyBatis中的mapper.xml文件相互绑定的接口。注意:该接口是用来操作DB的。
*/
public interface EmployeeDao {
EmployeeEntity queryEmployeeById(@Param("id") int id);
}
6、编写与表对应的mapper配置文件(对表操作的各种SQL):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.xml文件都有一个命名空间,该命名空间表示:该mapper.xml文件和哪个接口绑定起来。
注意:该接口是用来操作DB的。-->
<mapper namespace="com.wind.dao.EmployeeDao">
<select id="queryEmployeeById" resultType="EmployeeEntity">
select * from TBL_Employee where id = #{id}
</select>
</mapper>
7、编写MyBatis的全局配置文件(在全局配置文件中注册mapper.xml文件):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>
<!--MyBatis的设置-->
<settings>
<!--开启在控制台打印SQL日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--MyBatis中entity别名的设置-->
<typeAliases>
<package name="com.wind.entity"/>
</typeAliases>
<!--配置MyBatis运行环境的全局配置文件-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!--配置POOLED类型的JDBC数据源连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/RUNOOB?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--把一个个的mapper配置文件注册到全局配置文件中-->
<mappers>
<mapper resource="mapper/EmployeeMapper.xml"/>
</mappers>
</configuration>
8、最终的Maven项目结构图
9、接口测试:EmployeeTest.java
import com.wind.dao.EmployeeDao;
import com.wind.entity.EmployeeEntity;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
public class EmployeeTest {
public static void main(String[] args) {
//加载MyBatis配置文件
InputStream inputStream = EmployeeTest.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取实现接口的代理对象
EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
EmployeeEntity employeeEntity = employeeDao.queryEmployeeById(1);
System.out.println(employeeEntity);
//关闭资源
sqlSession.close();
}
}
10、MyBatis使用步骤总结
11、参考尚硅谷MyBatis视频
站点:https://www.bilibili.com/video/BV1mW411M737?p=1
12、MyBatis视频PPT截图