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文件 & ...
随机推荐
- ES索引误删的名场面
ES索引误删的名场面 慌了3秒,果断发个邮件: 01 最近,在版本发布时: ES线上未备份的索引,被当场「误删」了: 对于新手来说,妥妥的社死名场面: 对于老手来说,慌它3秒表示一下态度: 当时的情况 ...
- LeetCode5716:好因子的最大数目(数学、快速幂)
解题思路:因为primeFactors比较大,所以需要使用快速幂. class Solution: def quick_pow(self,base,x): ans = 1 while x>0: ...
- 华企盾DSC控制台操作卡顿如何解决
解决方法: 修改注册表: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows 的 USERProcessHa ...
- 数字孪生融合GIS系统将为交通领域带来什么改变?
随着科技的不断发展,数字孪生和GIS技术正成为交通领域的新宠.数字孪生是指通过数学建模.数据采集和实时仿真等技术手段,将实体世界与数字世界相互关联,形成一个全新的虚拟系统.而GIS(地理信息系统)则是 ...
- PBKDF2(Password-Based Key Derivation Function 2)算法
一.引言 在当今数字时代,保护用户数据和隐私的安全变得越来越重要.为实现这一目标,加密和密钥管理技术发挥着关键作用.PBKDF2(Password-Based Key Derivation Funct ...
- ChatGPT 沦为了我的打工仔
大家好,我是老章 最近在折腾之前搞的R-bookdown网站.Jekyll博客,还有刚上了Astro做的周刊.Astro Starlight做的文档站 R2ML:https://r2ml.zhangl ...
- Liquid 常用语法记录
一.什么是 Liquid Liquid 是一款专为特定需求而打造的模板引擎. Liquid 中有两种类型的标记:Output 和 Tag. Output 通常用来显示文本 {{ 两个花括号 }} Ta ...
- 【有奖征文】WEB前端大作战,走在技术最前端!
摘要:投稿分享你在前端领域的积累,秀出你的技术"肌肉",为自己,也为技术发声. 近几年大家对于WEB前端的关注度很高, 比如整体势头发展良好,各种技术百花齐放,人才稀缺, 随着互联 ...
- Vuex在TSX中的改造方案:TS改造Vue2项目Vuex如何处置?
vuex目前比较流行的有:vuex-aggregate . vuex-class.vuex-module-decorators npm搜到相关的,看下趋势图:https://www.npmtrends ...
- 2023开发者必备iOS开发工具
2023开发者必备iOS开发工具 工欲善其事,必先利其器.进行开发工作时,利用并熟练使用恰当的工具可以让工作效率得到大幅度提高.下边会介绍一些在进行iOS开发工作时常用的一些工具,本文并不对其进行 ...