一、環(huán)境準(zhǔn)備
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的方式拉取代碼,后期就不需要執(zhí)行
git init
三、創(chuàng)建空項(xiàng)目、導(dǎo)入項(xiàng)目
導(dǎo)入兩個(gè)項(xiàng)目
注意 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,).
未來可能發(fā)生改變
設(shè)置為maven 3.9.5
設(shè)置為java 17
四、編譯 mybatis-parent
執(zhí)行命令
mvn clean install
或者通過窗口執(zhí)行
注意:如果出現(xiàn)Error: One of setGitDir or setWorkTree must be called.
執(zhí)行命令:
git init
五、編譯 mybatis
修改成自己特有的版本,方便區(qū)分,避免與官網(wǎng)依賴相同版本
執(zhí)行 maven 命令
mvn install -Dmaven.test.skip=true
PS:建議直接刪除test相關(guān)文件夾
注意:如果出現(xiàn):
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)?
執(zhí)行命令
git add .
git commit -m ‘xxx’
六、測(cè)試
- 添加mybati-test項(xiàng)目
- 引入依賴
<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>
- 添加數(shù)據(jù)庫(kù)
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根據(jù)自己需要?jiǎng)?chuàng)建的的mapper的路徑和名稱填寫-->
<select id="selectById" resultType="com.mcode.entity.User">
select * from user where id = #{id}
</select>
</mapper>
- 測(cè)試
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配置文件構(gòu)建為Configuration配置類
reader = Resources.getResourceAsReader(resource);
// 通過加載配置文件流構(gòu)建一個(gè)SqlSessionFactory DefaultSqlSessionFactory
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
// 數(shù)據(jù)源 執(zhí)行器 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.
看報(bào)錯(cuò)信息應(yīng)該是缺少Javassit的jar包,我們?nèi)?mybatis的源碼pom.xml把相應(yīng)的jar復(fù)制過來文章來源:http://www.zghlxwxcb.cn/news/detail-822398.html
<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>
總結(jié)
不必過于糾結(jié)一些錯(cuò)誤,對(duì)于一些失敗的可以考慮直接注釋文章來源地址http://www.zghlxwxcb.cn/news/detail-822398.html
到了這里,關(guān)于MyBatis 系列:MyBatis 源碼環(huán)境搭建的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!