Idea+Mybatis源码构建
准备工作:下载源码包
注意:
Mybatis源码还依赖于一个父工程mybatis-parent,因此需要一起下载
并且需要注意mybatis中引入的mybatis-parent的版本的版本得和自己下载的mybatis-parent的版本一致
mybatis-3-mybatis-3.5.5 源码下载
提取码:5t68
parent-mybatis-parent-31 源码下载
提取码:0z7b
如果想下载其他版本,可以去官方进行选择下载:
GitHub地址:https://github.com/mybatis/mybatis-3
一、将源码安装至本地仓库中
1.1 切换到你下载的mybatis-parent目录
进入dos窗口
执行命令:mvn clean install -Dmaven.test.skip=true
看到BUILD SUCCESS 代表install成功
1.2 切换到你下载的mybatis源码目录(和1.1中的操作一致)
执行命令:mvn clean install -Dmaven.test.skip=true
报错:
pdf插件问题
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-pdf-plugin:1.4:pdf (pdf) on project mybatis: Error during document generation: Error parsing C:\Users\sx\ws\mybatis_lg\ mybatis_four_source\mybatis-3-mybatis-3.5.5\mybatis-3-mybatis-3.5.5\target\pdf\site.tmp\xdoc\getting-started.xml: Error parsing the model: only whitespace content allowed before sta rt tag and not \ufeff (position: COMMENT seen ...rning permissions and\n limitations under the License.\n\n-->\n\ufeff... @18:2) -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
解决:
打开pom.xml 文件注释掉 maven-pdf-plugin 插件(可以选择把项目导入到idea中进行相关内容的删除,或者直接使用文本编辑工具进行删除)
重新install一次
此时再次install,看到BUILD SUCCESS 代表install成功
C:\Users\sx\ws\mybatis_lg\mybatis_four_source\mybatis-3-mybatis-3.5.5\mybatis-3-mybatis-3.5.5>mvn clean install -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< org.mybatis:mybatis >-------------------------
[INFO] Building mybatis 3.5.5
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ mybatis ---
[INFO] Deleting C:\Users\sx\ws\mybatis_lg\mybatis_four_source\mybatis-3-mybatis-3.5.5\mybatis-3-mybatis-3.5.5\target
[INFO]
[INFO] --- maven-enforcer-plugin:3.0.0-M1:enforce (enforce-java) @ mybatis ---
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.5:prepare-agent (prepare-agent) @ mybatis ---
[INFO] argLine set to -javaagent:C:\\Users\\sx\\sx_tools\\local_maven_repository\\org\\jacoco\\org.jacoco.agent\\0.8.5\\org.jacoco.agent-0.8.5-runtime.jar=destfile=C:\\Users\\sx\\ws
\\mybatis_lg\\mybatis_four_source\\mybatis-3-mybatis-3.5.5\\mybatis-3-mybatis-3.5.5\\target\\jacoco.exec,excludes=org.apache.ibatis.ognl.*:org.apache.ibatis.javassist.*
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ mybatis ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources to META-INF
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ mybatis ---
[INFO] Compiling 384 source files to C:\Users\sx\ws\mybatis_lg\mybatis_four_source\mybatis-3-mybatis-3.5.5\mybatis-3-mybatis-3.5.5\target\classes
[INFO] /C:/Users/sx/ws/mybatis_lg/mybatis_four_source/mybatis-3-mybatis-3.5.5/mybatis-3-mybatis-3.5.5/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java: 某些输
入文件使用或覆盖了已过时的 API。
[INFO] /C:/Users/sx/ws/mybatis_lg/mybatis_four_source/mybatis-3-mybatis-3.5.5/mybatis-3-mybatis-3.5.5/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java: 有关详
细信息, 请使用 -Xlint:deprecation 重新编译。
[INFO] /C:/Users/sx/ws/mybatis_lg/mybatis_four_source/mybatis-3-mybatis-3.5.5/mybatis-3-mybatis-3.5.5/src/main/java/org/apache/ibatis/reflection/MetaObject.java: 某些输入文件使用了
未经检查或不安全的操作。
[INFO] /C:/Users/sx/ws/mybatis_lg/mybatis_four_source/mybatis-3-mybatis-3.5.5/mybatis-3-mybatis-3.5.5/src/main/java/org/apache/ibatis/reflection/MetaObject.java: 有关详细信息, 请使
用 -Xlint:unchecked 重新编译。
[INFO]
[INFO] --- license-maven-plugin:3.0:format (default) @ mybatis ---
[INFO] Updating license headers...
[INFO]
[INFO] --- animal-sniffer-maven-plugin:1.17:check (check-java-compat) @ mybatis ---
[INFO] Checking unresolved references to org.codehaus.mojo.signature:java18:1.0
[INFO]
[INFO] --- maven-bundle-plugin:4.1.0:manifest (bundle-manifest) @ mybatis ---
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ mybatis ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ mybatis ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ mybatis ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ mybatis ---
[INFO] Building jar: C:\Users\sx\ws\mybatis_lg\mybatis_four_source\mybatis-3-mybatis-3.5.5\mybatis-3-mybatis-3.5.5\target\mybatis-3.5.5.jar
[INFO]
[INFO] --- maven-shade-plugin:3.2.1:shade (default) @ mybatis ---
[INFO] Including ognl:ognl:jar:3.2.14 in the shaded jar.
[INFO] Including org.javassist:javassist:jar:3.27.0-GA in the shaded jar.
[INFO] Excluding org.slf4j:slf4j-api:jar:1.7.30 from the shaded jar.
[INFO] Excluding org.slf4j:slf4j-log4j12:jar:1.7.30 from the shaded jar.
[INFO] Excluding log4j:log4j:jar:1.2.17 from the shaded jar.
[INFO] Excluding org.apache.logging.log4j:log4j-core:jar:2.13.3 from the shaded jar.
[INFO] Excluding org.apache.logging.log4j:log4j-api:jar:2.13.3 from the shaded jar.
[INFO] Excluding commons-logging:commons-logging:jar:1.2 from the shaded jar.
[INFO] Excluding cglib:cglib:jar:3.3.0 from the shaded jar.
[INFO] Excluding org.ow2.asm:asm:jar:7.1 from the shaded jar.
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing C:\Users\sx\ws\mybatis_lg\mybatis_four_source\mybatis-3-mybatis-3.5.5\mybatis-3-mybatis-3.5.5\target\mybatis-3.5.5.jar with C:\Users\sx\ws\mybatis_lg\mybatis_four_s
ource\mybatis-3-mybatis-3.5.5\mybatis-3-mybatis-3.5.5\target\mybatis-3.5.5-shaded.jar
[INFO]
[INFO] --- maven-site-plugin:3.7.1:attach-descriptor (attach-descriptor) @ mybatis ---
[INFO] Skipping because packaging 'jar' is not pom.
[INFO]
[INFO] --- modernizer-maven-plugin:1.7.1:modernizer (modernizer) @ mybatis ---
[INFO]
[INFO] --- maven-install-plugin:3.0.0-M1:install (default-install) @ mybatis ---
[INFO] Installing C:\Users\sx\ws\mybatis_lg\mybatis_four_source\mybatis-3-mybatis-3.5.5\mybatis-3-mybatis-3.5.5\target\mybatis-3.5.5.jar to C:\Users\sx\sx_tools\local_maven_reposito
ry\org\mybatis\mybatis\3.5.5\mybatis-3.5.5.jar
[INFO] Installing C:\Users\sx\ws\mybatis_lg\mybatis_four_source\mybatis-3-mybatis-3.5.5\mybatis-3-mybatis-3.5.5\pom.xml to C:\Users\sx\sx_tools\local_maven_repository\org\mybatis\my
batis\3.5.5\mybatis-3.5.5.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.412 s
[INFO] Finished at: 2021-03-18T17:37:35+08:00
[INFO] ------------------------------------------------------------------------
C:\Users\sx\ws\mybatis_lg\mybatis_four_source\mybatis-3-mybatis-3.5.5\mybatis-3-mybatis-3.5.5>
二、在idea中导入mybatis-parent和mybatis源码
三、在idea中创建测试项目
3.1 创建新的Module
此时工程的目录结构如下
3.2 引入mybatis相应依赖,此处得引入我们上面构建的mybatis
测试项目需要的其他的完整依赖
<dependencies>
<!--mybatis坐标-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!--mysql驱动坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<!--单元测试坐标-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--⽇志坐标-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
<build>
<!--修改pom文件中加上此resources即可解决-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
3.3 新建Mapper接口和对应的映射文件
AccountMapper.java
public interface AccountMapper {
List<Account> findAll() throws IOException;
Account findByCondition(Account account) throws IOException;
List<Account> findByIds(int [] ids) throws IOException;
Account findById(int id) throws IOException;
}
AccountMapper.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.dabing.source.mapper.AccountMapper">
<select id="findAll" resultType="account">
select * from Account
</select>
<!--动态sql-->
<select id="findByCondition" parameterType="account" resultType="account">
select * from Account
<where>
<if test="id!=0">
and id=#{id}
</if>
<if test="username!=null">
and username=#{username}
</if>
</where>
</select>
<!--动态sql:循环执⾏sql的拼接操作-->
<select id="findByIds" parameterType="list" resultType="account">
select * from Account
<where>
<foreach collection="array" open="id in(" close=")" item="id"
separator=",">
#{id}
</foreach>
</where>
</select>
<!--抽取sql⽚段简化编写-->
<sql id="selectAccount" >select * from Account</sql>
<select id="findById" parameterType="int" resultType="account">
<include refid="selectAccount"></include> where id=#{id}
</select>
</mapper>
3.4 新建mybatis配置文件,在resource目录下创建sqlMapConfig.xml文件:
sqlMapConfig.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>
<!--给实体类的全限定类名给别名-->
<typeAliases>
<!--批量起别名:该包下所有的类的本身的类名:别名还不区分大小写-->
<package name="com.dabing.source.pojo"/>
</typeAliases>
<!--environments:运行环境-->
<environments default="development">
<environment id="development">
<!--当前事务交由JDBC进行管理-->
<transactionManager type="JDBC"></transactionManager>
<!--当前使用mybatis提供的连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///zdy_mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--引入映射配置文件-->
<mappers>
<package name="com.dabing.source.mapper"/>
</mappers>
</configuration>
3.5 编写测试类测试
@Test
public void test() throws IOException {
InputStream resourceAsStream =
Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//获得MyBatis框架⽣成的UserMapper接⼝的实现类
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
List<Account> accountMapperAll = accountMapper.findAll();
for (Account account : accountMapperAll) {
System.out.println(account);
}
sqlSession.close();
}
执行:报错
解决:
再次执行:执行成功