MyBatis 系列:MyBatis 源码环境搭建
一、环境准备
jdk:17
maven:3.9.5
二、下载 MyBatis 源码和 MyBatis-Parent 源码
Mybatis:https://github.com/mybatis/mybatis-3.git
Mybatis-Parent:https://github.com/mybatis/parent.git
建议使用git的方式拉取代码,后期就不需要执行
git init
三、创建空项目、导入项目
导入两个项目

注意 mybatis-parent 必须采用 jdk版本:11-23,maven版本: 3.9.5
否则提示:
ERROR] Rule 1: org.apache.maven.enforcer.rules.version.RequireJavaVersion failed with message: [ERROR] Detected JDK version 1.8.0-361 (JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_361.jdk/Contents/Home/jre) is not in the allowed range [11,12),[17,18),[21,22),[22,23). [ERROR] Rule 2: org.apache.maven.enforcer.rules.version.RequireMavenVersion failed with message: [ERROR] Detected Maven Version: 3.6.3 is not in the allowed range [3.9.5,).未来可能发生改变
设置为maven 3.9.5

设置为java 17

四、编译 mybatis-parent
执行命令
mvn clean install
或者通过窗口执行

注意:如果出现Error: One of setGitDir or setWorkTree must be called.
执行命令:
git init
五、编译 mybatis
修改成自己特有的版本,方便区分,避免与官网依赖相同版本

执行 maven 命令
mvn install -Dmaven.test.skip=true
PS:建议直接删除test相关文件夹
注意:如果出现:
Could not get HEAD Ref, are you sure you have some commits in the dotGitDirectory (currently set to xxx/java-mybatis-source/mybatis-3-master/.git)?执行命令
git add .
git commit -m 'xxx'
六、测试
- 添加mybati-test项目
- 引入依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16-DEMO</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
- 添加数据库
create database test;
create table if not exists test.user
(
id int auto_increment
primary key,
userName varchar(50) not null,
createTime datetime not null
);
- 添加entity
package com.mcode.entity;
import java.time.LocalDateTime;
/**
* ClassName: User
* Package: com.mcode.entity
* Description:
*
* @Author: robin
* @Version: v1.0
*/
public class User {
private int id;
private String userName;
private LocalDateTime createTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
}
- 添加 UserMapper
package com.mcode.mapper;
import com.mcode.entity.User;
/**
* ClassName: UserMapper
* Package: com.mcode.mapper
* Description:
*
* @Author: robin
* @Version: v1.0
*/
public interface UserMapper {
User selectById(int 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"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="./mappers/UserMapper.xml"/>
</mappers>
</configuration>
- 添加 UserMapper.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 namespace="com.mcode.mapper.UserMapper">
<!--namespace根据自己需要创建的的mapper的路径和名称填写-->
<select id="selectById" resultType="com.mcode.entity.User">
select * from user where id = #{id}
</select>
</mapper>
- 测试
package com.mcode;
import com.mcode.entity.User;
import com.mcode.mapper.UserMapper;
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.Reader;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
String resource = "mybatis-config.xml";
Reader reader;
try {
//将XML配置文件构建为Configuration配置类
reader = Resources.getResourceAsReader(resource);
// 通过加载配置文件流构建一个SqlSessionFactory DefaultSqlSessionFactory
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
// 数据源 执行器 DefaultSqlSession
SqlSession session = sqlMapper.openSession();
try {
UserMapper mapper = session.getMapper(UserMapper.class);
System.out.println(mapper.getClass());
User user = mapper.selectById(1);
System.out.println(user.getUserName());
} catch (Exception e) {
e.printStackTrace();
}finally {
session.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

问题:Cannot enable lazy loading because Javassist is not available. Add Javassist to your classpath.
看报错信息应该是缺少Javassit的jar包,我们去 mybatis的源码pom.xml把相应的jar复制过来
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.2.15</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.27.0-GA</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
总结
不必过于纠结一些错误,对于一些失败的可以考虑直接注释
MyBatis 系列:MyBatis 源码环境搭建的更多相关文章
- MyBatis源码环境搭建
之前研究mybatis都是参考前面学习的人的一些经验,并没有自己搭建源码环境进行.现在以mybatis3.4.6版本搭建,搭建过程中各种failed,下面大致记录环境搭建过程. 1.mybatis3. ...
- 【ZooKeeper系列】3.ZooKeeper源码环境搭建
前文阅读: [ZooKeeper系列]1.ZooKeeper单机版.伪集群和集群环境搭建 [ZooKeeper系列]2.用Java实现ZooKeeper API的调用 在系列的前两篇文章中,介绍了Zo ...
- Linux Kafka源码环境搭建
本文主要讲述的是如何搭建Kafka的源码环境,主要针对的Linux操作系统下IntelliJ IDEA编译器,其余操作系统或者IDE可以类推. 1.安装和配置JDK确认JDK版本至少为1.7,最好是1 ...
- 1-web应用之LAMP源码环境搭建
目录 一.LAMP环境的介绍 1.LAMP环境的重要性 2.LAMP组件介绍 二.Apache源码安装 1.下载Apache以及相关依赖包 2.安装Apache以及相关 ...
- 【一步一步】Spring 源码环境搭建
平时项目中基本上都会用到spring,但是源码还没有深入的了解过.趁这段时间稍微空闲点,开始研究下spring 源码.下面是spring 源码的环境搭建. 主要分为如下步骤: ①安装jdk,gradl ...
- Spring源码阅读 源码环境搭建(一)
ring 源码阅读的搭建(一) 一 下载spring源码 进入官方网页:https://spring.io/projects/spring-framework 进入相关的github位置,下载zip包 ...
- Hadoop源码学习笔记之NameNode启动场景流程一:源码环境搭建和项目模块及NameNode结构简单介绍
最近在跟着一个大佬学习Hadoop底层源码及架构等知识点,觉得有必要记录下来这个学习过程.想到了这个废弃已久的blog账号,决定重新开始更新. 主要分以下几步来进行源码学习: 一.搭建源码阅读环境二. ...
- Tomcat7源码环境搭建
一.下载Tomcat7源码 从官网上下载Tomcat源码, http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.70/src/apache-t ...
- Spring 5.2.x 源码环境搭建(Windows 系统环境下)
前期准备 1.确保本机已经安装好了 Git 2.Jdk 版本至少为 1.8 3.安装好 IntelliJ IDEA (其他开发工具,如 eclipse.Spring Tool Suite 等也是可以的 ...
- Tomcat 源码环境搭建
Tomcat 源码搭建 下载源码 下载地址 :https://tomcat.apache.org/download-80.cgi#8.5.35 下载之后解压缩 导入Idea 添加pom.xml文件 & ...
随机推荐
- 容器网络Cilium:DualStack双栈特性分析
本文分享自华为云社区<容器网络Cilium入门系列之DualStack双栈特性分析>,作者: 可以交个朋友. 一 . 关于IPV6/IPV4 双栈 目前很多公司开始将自己的业务由ipv4切 ...
- 安装华企盾DSC防泄密软件造成CAD2012卡住怎么办?
将下图目录的.exe程序删除或者重命名
- 数字孪生和GIS的融合能够为智慧水务带来什么帮助?
数字孪生和地理信息系统(GIS)的融合在智慧水务领域有着重要的应用前景.让我们一起探讨数字孪生和GIS如何合作,为智慧水务系统带来了哪些帮助. GIS系统提供了准确的地理数据,包括水资源.管道网络.水 ...
- Collection和Map的区别
Collection接口,包含list和set子接口Collection和Map接口之间的主要区别在于:Collection中存储了一组对象,而Map存储关键字/值对.在Map对象中,每一个关键字最多 ...
- 火爆全网的Log4j 漏洞复现GetShell
目录: 一.搭建环境 1. 首先拉一个docker镜像 2. 然后启动环境 二.获取shell 首先,试验一下DNSLog 1. 准备JNDI注入工具 下载 进入目录打包成jar包 2. 利用 生成p ...
- 开心自走棋:使用 Laf 云开发支撑数百万玩家
先介绍一下开心自走棋 开心自走棋是一款剑与魔法的烧脑自走棋游戏.以著名的魔幻世界观为蓝本,采用了轻松可爱的画面风格,精致细腻的动画和特效来还原魔兽之战. 现在市面上自走棋游戏多是 PvP 玩法为主,而 ...
- Java 中时间对象的序列化
在 Java 应用程序中,时间对象是使用地比较频繁的对象,比如,记录某一条数据的修改时间,用户的登录时间等应用场景.在传统的 Java 编程中,大部分的程序员都会选择使用 java.uti.Date ...
- bazel test 编译失败:googletest、gtest 报错
问题描述 bazel test 遇到很多奇怪的编译错误,报错位置位于"googletest"目录,而且没有修改过 googletest 源码: ERROR: /bazel_cach ...
- OPPO关停自研芯片公司哲库,这对行业将产生什么影响?
OPPO什么时候关停自研芯片公司哲库? 公元2023年5月12日,OPPO关停了自研芯片公司哲库.这也是汶川大地震的日子,而OPPO创始人是四川人,真是冥冥之中自有天意.OPPO公司在一份声明中表示, ...
- Langchain-Chatchat项目:1.2-Baichuan2项目整体介绍
由百川智能推出的新一代开源大语言模型,采用2.6万亿Tokens的高质量语料训练,在多个权威的中文.英文和多语言的通用.领域benchmark上取得同尺寸最佳的效果,发布包含有7B.13B的Bas ...