搭建Mybatis源码环境
源码下载
mybatis源码下载在github上面,链接如下(演示下载的是3.5.1版本)
https://github.com/mybatis/mybatis-3
除了mybatis源码外还需要下载依赖工程mybatis-parent(演示下载的是parent-31版本)
https://github.com/mybatis/parent
工程搭建
idea新建一个空项目
File---->New—>Project
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rP5rdpn9-1657519956426)(https://mmbiz.qpic.cn/mmbiz_png/3THd6lArRajgFMcz4eNtokRskZTaxk8Wm9Hk2peIfJ9UC3Yyz7CfOLSVdm19rZAtyf2ffhckTAz2M1sLLHjfmQ/640?wx_fmt=png)]
File–>Project Structure (导入parent-mybatis-parent-31、mybatis-3-mybatis-3.5.1)
更改mybatis-3-mybatis-3.5.1工程的pom文件,更新mybatis-parent中pom.xml文件的相对位置
编译工程
parent-mybatis-parent-31
mybatis-3-mybatis-3.5.1
按照上面的步骤重复操作,但是可能会出现一些问题
Failed to execute goal org.apache.maven.plugins:maven-pdf-plugin
注释pdf插件,不引入
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pdf-plugin</artifactId>
</plugin>
Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.0:shade (default) on project mybatis
需要手动指定maven-shade-plugin插件版本
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
.......
</plugin>
验证环境
File–>Project Structure 新建一个新的module,mybatis-test用于所有的测试工作
创建数据库表
CREATE TABLE `student` (
`id` int NOT NULL AUTO_INCREMENT COMMENT 'id',
`sno` varchar(20) DEFAULT NULL COMMENT '学号',
`sname` varchar(10) DEFAULT NULL COMMENT '学生姓名',
PRIMARY KEY (`id`)
)
创建数据库配置文件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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
</transactionManager>
<dataSource type="POOLED">
<!-- 8.x版本 com.mysql.cj.jdbc.Driver -->
<!-- 5.x版本 com.mysql.jdbc.Driver -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/unimondb"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 配置扫描包,根据自己建的工程修改 -->
<mappers>
<package name="com.mybatis.test"/>
</mappers>
</configuration>
创建实体类
public class Student {
private String id;
private String sno;
private String sname;
// .....省略get、set、toString方法
}
创建接口StudentMapper
public interface StudentMapper {
Student selectById(String id);
}
创建Mapper的Sql映射文件
<?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">
<!-- 命名空间,是接口StudentMapper的全限定名 -->
<mapper namespace="com.mybatis.test.StudentMapper">
<!-- 实体类的全限定名 -->
<select id="selectById" resultType="com.mybatis.test.Student">
select * from student where id = #{id}
</select>
</mapper>
新增测试入口
public class MyTest {
private static SqlSessionFactory sqlSessionFactory;
public static void main(String[] args) throws IOException {
// 1、创建SqlSessionFactory
String resource = "mybatis-config.xml";
// String resource = "md";
final Reader reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
// 2、获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 3、获取mapper 动态代理,代理对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// 4、执行数据库操作,并处理结果集
Student goods = mapper.selectById("111");
System.out.println(goods);
}
}
引入mybatis-3-mybatis-3.5.1模块依赖
启动项目
可能出现的问题
Cannot enable lazy loading because Javassist is not available
javassist的依赖并没有成功的加载,在mybatis-test的pom中手动引入
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.24.1-GA</version>
</dependency>
Caused by: java.lang.ClassNotFoundException: ognl.PropertyAccessor
ognl依赖没有加载成功,在mybatis-test的pom中手动引入
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.2.10</version>
</dependency>
Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.jdbc.Driver
需要引入驱动包程序
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>