搭建Mybatis源码环境

搭建Mybatis源码环境

源码下载

mybatis源码下载在github上面,链接如下(演示下载的是3.5.1版本)

https://github.com/mybatis/mybatis-3

除了mybatis源码外还需要下载依赖工程mybatis-parent(演示下载的是parent-31版本)

图片

https://github.com/mybatis/parent

工程搭建

idea新建一个空项目

File---->New—>Project

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rP5rdpn9-1657519956426)(https://mmbiz.qpic.cn/mmbiz_png/3THd6lArRajgFMcz4eNtokRskZTaxk8Wm9Hk2peIfJ9UC3Yyz7CfOLSVdm19rZAtyf2ffhckTAz2M1sLLHjfmQ/640?wx_fmt=png)]

File–>Project Structure (导入parent-mybatis-parent-31、mybatis-3-mybatis-3.5.1)

图片

更改mybatis-3-mybatis-3.5.1工程的pom文件,更新mybatis-parent中pom.xml文件的相对位置

图片

编译工程

parent-mybatis-parent-31

图片

mybatis-3-mybatis-3.5.1

按照上面的步骤重复操作,但是可能会出现一些问题

Failed to execute goal org.apache.maven.plugins:maven-pdf-plugin

注释pdf插件,不引入

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pdf-plugin</artifactId>
</plugin>

Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.0:shade (default) on project mybatis

需要手动指定maven-shade-plugin插件版本

 <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-shade-plugin</artifactId>
     <version>3.2.4</version>
     .......
</plugin>

验证环境

File–>Project Structure 新建一个新的module,mybatis-test用于所有的测试工作

图片

创建数据库表

CREATE TABLE `student` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT 'id',
  `sno` varchar(20) DEFAULT NULL COMMENT '学号',
  `sname` varchar(10) DEFAULT NULL COMMENT '学生姓名',
  PRIMARY KEY (`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">
            </transactionManager>
            <dataSource type="POOLED">
                <!--  8.x版本  com.mysql.cj.jdbc.Driver -->
                <!--  5.x版本   com.mysql.jdbc.Driver -->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/unimondb"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 配置扫描包,根据自己建的工程修改 -->
    <mappers>
        <package name="com.mybatis.test"/>
    </mappers>
</configuration>

创建实体类

public class Student {
    private String id;
    private String sno;
    private String sname;
    // .....省略get、set、toString方法
}

创建接口StudentMapper

public interface StudentMapper {
    Student selectById(String id);
}

创建Mapper的Sql映射文件

<?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">
<!-- 命名空间,是接口StudentMapper的全限定名 -->
<mapper namespace="com.mybatis.test.StudentMapper">

    <!-- 实体类的全限定名 -->
    <select id="selectById" resultType="com.mybatis.test.Student">
        select * from student where id = #{id}
    </select>
</mapper>

新增测试入口

public class MyTest {
    private static SqlSessionFactory sqlSessionFactory;

    public static void main(String[] args) throws IOException {
        // 1、创建SqlSessionFactory
        String resource = "mybatis-config.xml";
        //  String resource = "md";
        final Reader reader = Resources.getResourceAsReader(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        reader.close();
        // 2、获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 3、获取mapper 动态代理,代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        // 4、执行数据库操作,并处理结果集
        Student goods = mapper.selectById("111");
        System.out.println(goods);
    }
}

引入mybatis-3-mybatis-3.5.1模块依赖

图片

图片

启动项目

图片

可能出现的问题

Cannot enable lazy loading because Javassist is not available

javassist的依赖并没有成功的加载,在mybatis-test的pom中手动引入

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.24.1-GA</version>
</dependency>

Caused by: java.lang.ClassNotFoundException: ognl.PropertyAccessor

ognl依赖没有加载成功,在mybatis-test的pom中手动引入

<dependency>
    <groupId>ognl</groupId>
    <artifactId>ognl</artifactId>
    <version>3.2.10</version>
</dependency>

Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.jdbc.Driver

需要引入驱动包程序

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.23</version>
</dependency>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值