一、环境准备

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'

六、测试

  1. 添加mybati-test项目
  2. 引入依赖
    <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>
  1. 添加数据库
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
);
  1. 添加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;
}
}
  1. 添加 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);
}
  1. 添加 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>
  1. 添加 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>
  1. 测试
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 源码环境搭建的更多相关文章

  1. MyBatis源码环境搭建

    之前研究mybatis都是参考前面学习的人的一些经验,并没有自己搭建源码环境进行.现在以mybatis3.4.6版本搭建,搭建过程中各种failed,下面大致记录环境搭建过程. 1.mybatis3. ...

  2. 【ZooKeeper系列】3.ZooKeeper源码环境搭建

    前文阅读: [ZooKeeper系列]1.ZooKeeper单机版.伪集群和集群环境搭建 [ZooKeeper系列]2.用Java实现ZooKeeper API的调用 在系列的前两篇文章中,介绍了Zo ...

  3. Linux Kafka源码环境搭建

    本文主要讲述的是如何搭建Kafka的源码环境,主要针对的Linux操作系统下IntelliJ IDEA编译器,其余操作系统或者IDE可以类推. 1.安装和配置JDK确认JDK版本至少为1.7,最好是1 ...

  4. 1-web应用之LAMP源码环境搭建

    目录 一.LAMP环境的介绍     1.LAMP环境的重要性     2.LAMP组件介绍 二.Apache源码安装     1.下载Apache以及相关依赖包     2.安装Apache以及相关 ...

  5. 【一步一步】Spring 源码环境搭建

    平时项目中基本上都会用到spring,但是源码还没有深入的了解过.趁这段时间稍微空闲点,开始研究下spring 源码.下面是spring 源码的环境搭建. 主要分为如下步骤: ①安装jdk,gradl ...

  6. Spring源码阅读 源码环境搭建(一)

    ring 源码阅读的搭建(一) 一 下载spring源码 进入官方网页:https://spring.io/projects/spring-framework 进入相关的github位置,下载zip包 ...

  7. Hadoop源码学习笔记之NameNode启动场景流程一:源码环境搭建和项目模块及NameNode结构简单介绍

    最近在跟着一个大佬学习Hadoop底层源码及架构等知识点,觉得有必要记录下来这个学习过程.想到了这个废弃已久的blog账号,决定重新开始更新. 主要分以下几步来进行源码学习: 一.搭建源码阅读环境二. ...

  8. Tomcat7源码环境搭建

    一.下载Tomcat7源码 从官网上下载Tomcat源码,   http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.70/src/apache-t ...

  9. Spring 5.2.x 源码环境搭建(Windows 系统环境下)

    前期准备 1.确保本机已经安装好了 Git 2.Jdk 版本至少为 1.8 3.安装好 IntelliJ IDEA (其他开发工具,如 eclipse.Spring Tool Suite 等也是可以的 ...

  10. Tomcat 源码环境搭建

    Tomcat 源码搭建 下载源码 下载地址 :https://tomcat.apache.org/download-80.cgi#8.5.35 下载之后解压缩 导入Idea 添加pom.xml文件 & ...

随机推荐

  1. C++学习笔记二:变量与数据类型(整型)

    1.int(整型数据): 1.1 进制的表示:十进制,八进制,16进制,二进制 int number1 = 15; // Decimal int number2 = 017; // Octal int ...

  2. tensorflow GPU版本配置加速环境

    import tensorflow as tf tf.test.is_gpu_available() 背景 环境:Anaconda .tensorflow_gpu==1.4.0 (这里就用1.4.0版 ...

  3. JDK1.8下载 用阿里云盘

    JDK1.8下载 用阿里云盘 jdk-8u202-windows-x64.exe https://www.aliyundrive.com/s/jJhWUk17jMt 点击链接保存,或者复制本段内容,打 ...

  4. MinIO客户端之du

    MinIO提供了一个命令行程序mc用于协助用户完成日常的维护.管理类工作. 官方资料 mc du 用于输出桶内对象的数量和占用的空间. 命令如下: ./mc du local1/bkt1 控制台的输出 ...

  5. 【Python】【OpenCV】OCR识别(二)——透视变换

    对于OCR技术在处理有角度有偏差的图像时是比较困难的,而水平的图像使用OCR识别准确度会高很多,因为文本通常是水平排列的,而OCR算法一般会假设文本是水平的. 针对上述情况,所以我们在处理有角度的图象 ...

  6. GaussDB(for MySQL)如何在存储架构设计上做到高可靠、高可用

    摘要: GaussDB(for MySQL)通过ND算子下推解决存储节点和计算节点之间的传输速度,减少网络开销这个难题. 数据库作为高效稳定处理海量数据交易/分析的坚强数据底座,底层架构设计的重要性不 ...

  7. 技术实操丨SoundNet迁移学习之由声音分类到语音情感识别

    摘要:声音也是识别对象的一种重要数据源.其中根据声音来识别声音所处的环境也是语音识别的研究内容之一. 一.思路 1.SoundNet模型在视频数据中先预训练,视频任务可能是场景识别,可参考这篇文章So ...

  8. 华为云MVP付健权:从机械工程师到AI开发者的华丽转身

    摘要:谁说AI开发难,看一个机械工程师是如何转型成为AI工程师的. 付健权,华为云MVP,企业上云导师.软通动力,售前解决方案经理,为制造业讲解华为云产品,为客户提供上云和云上解决方案.为企业提供大数 ...

  9. 浅谈DWS函数出参方式

    摘要:DWS的PL/pgSQL函数/存储过程中有一个特殊的语法PERFORM语法,用于执行语句但是丢弃执行结果的场景,常用于一些状态判断的场景. 本文分享自华为云社区<GassDB(DWS)功能 ...

  10. 云图说|应用魔方AppCube:揭秘码农防脱神器

    摘要: 应用魔方(AppCube)是华为云为行业客户.合作伙伴.开发者量身打造的一款低代码开发平台.通过AppCube可轻松构建专业级应用,创新随心所欲,敏捷超乎想象. 本文分享自华为云社区<云 ...