文章目录
- 一、环境准备
- 二、下载 MyBatis 源码和 MyBatis-Parent 源码
- 三、创建空项目、导入项目
- 四、编译 mybatis-parent
- 五、编译 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>
总结
不必过于纠结一些错误,对于一些失败的可以考虑直接注释