Mybatis 源码搭建


源码下载

首先下载mybatis-parent的源码:

然后下载mybatis的源码:


mybatis源码POM中的以下包需要注释,否则会报错 :

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### Cause: java.lang.IllegalStateException: Cannot enable lazy loading because Javassist is not available. Add Javassist to your classpath.
    <dependency>
      <groupId>ognl</groupId>
      <artifactId>ognl</artifactId>
      <version>3.2.20</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-parent与mybatis导入idea同一个project下。

mybatis需要设置父工程为mybatis-parent,需要注意 parent-version标签要一致:

在这里插入图片描述

测试模块搭建

新建一个测试模块,这里叫mybatis-gabriel

项目基础架构
在这里插入图片描述

测试实例原博客

pom :

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.3.0-SNAPSHOT</version>
    </dependency>

  </dependencies>

相关代码

主要测试入口代码

public class TestMain {
    public static void main(String[] args) {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = null;
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionFactory.openSession();
            RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
            Role role = roleMapper.getRole(1L);
            System.out.println(role.getId() + ":" + role.getRoleName() + ":" + role.getNote());
            sqlSession.commit();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
    }
}

po :

/*
 * @author gethin
 * 角色的实体类
 */
public class Role {
    private long id;
    private String roleName;
    private String note;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }
}

MyStringHandler :

@MappedTypes({String.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MyStringHandler implements TypeHandler<String> {

     Logger log= Logger.getLogger(MyStringHandler.class.getName());

    @Override
    public String getResult(ResultSet rs, String colName) throws SQLException {
        log.info("使用我的TypeHandler,ResultSet列名获取字符串");
        return rs.getString(colName);
    }

    @Override
    public String getResult(ResultSet rs, int index) throws SQLException {
        log.info("使用我的TypeHandler,ResultSet下标获取字符串");
        return rs.getString(index);
    }

    @Override
    public String getResult(CallableStatement cs, int index) throws SQLException {
        log.info("使用我的TypeHandler,CallableStatement下标获取字符串");
        return cs.getString(index);
    }

    @Override
    public void setParameter(PreparedStatement ps, int index, String value, JdbcType arg3) throws SQLException {
        log.info("使用我的TypeHandler");
        ps.setString(index, value);
    }

}

mapper :

public interface RoleMapper {
    public Role getRole(Long id);
    Role selectTest(String roleName, int rowNum, Map map);
    public Role findRole(String roleName);
    public int deleteRole(Long id);
    public int insertRole(Role role);
}

RoleMapper.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="org.mybatis.debug.mapper.RoleMapper">
    <resultMap type="org.mybatis.debug.po.Role" id="roleMap">
        <id column="id" property="id" javaType="long" jdbcType="BIGINT" />
        <result column="role_name" property="roleName" javaType="string"
                jdbcType="VARCHAR" />
        <result column="note" property="note"
                typeHandler="org.mybatis.debug.handle.MyStringHandler" />
    </resultMap>


	<!--复杂sql解析调试-->
    <select id="selectTest"  resultMap="roleMap">
        select id,role_name as roleName,note from role
        <where>
            <if test="roleName==#roleName">
                and #{roleName} = #{roleName} and #{rowNum} = #{rowNum}
            </if>
            <if test="map.list != null">
                <foreach collection="map.list" item="item"  open="(" close=")" separator="," >
                    #{item}
                </foreach>
            </if>
        </where>
        <!--prefix=“set" 给拼接的sql语句加前缀,前缀为set,
        suffixOverrides=","自动将最后一个逗号删除-->
        <trim prefix="AND" suffixOverrides=",">
            <choose>
                <when test="roleName!=null">
                     roleName like #{roleName},
                </when>
                <when test="rowNum!=null">
                     rowNum = #{rowNum},
                </when>
                <otherwise>
                     1 = 1
                </otherwise>
            </choose>
        </trim>
    </select>

    <select id="getRole" parameterType="long" resultMap="roleMap">
        select
        id,role_name as roleName,note from role where id=#{id}
    </select>
    <select id="findRole" parameterType="long" resultMap="roleMap">
        select
        id,role_name,note from role where role_name like CONCAT('%',#{roleName
        javaType=string,
        jdbcType=VARCHAR,typeHandler=com.gethin.handler.MyStringHandler},'%')
    </select>
    <insert id="insertRole" parameterType="org.mybatis.debug.po.Role">
        insert into
        role(role_name,note) value(#{roleName},#{note})
    </insert>
    <delete id="deleteRole" parameterType="long">
        delete from role where
        id=#{id}
    </delete>
</mapper>

sql :

-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
  `id` int(11) DEFAULT NULL,
  `role_name` varchar(255) DEFAULT NULL,
  `note` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `role` VALUES ('1', '管理员', '管理员');
-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) DEFAULT NULL,
  `role_id` int(11) DEFAULT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `user_note` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `user` VALUES ('1', '1','张三', '管理员张三');

oracle :


create table ROLE
(
    ID        NUMBER not null
        constraint ROLE_PK
            primary key,
    ROLE_NAME VARCHAR2(50),
    NOTE      VARCHAR2(100)
);
/
INSERT INTO ROLE VALUES ('1', '超级管理员', '超级管理员');
INSERT INTO ROLE VALUES ('2', '普通管理员', '普通管理员');
INSERT INTO ROLE VALUES ('3', '超级会员', '超级会员');
INSERT INTO ROLE VALUES ('4', '普通会员', '普通会员');

create table "TUSER"
(
    ID        NUMBER not null,
    ROLE_ID   NUMBER,
    USER_NAME VARCHAR2(100),
    USER_NOTE VARCHAR2(100)
);
/

create unique index USER_ID_UINDEX
    on "TUSER" (ID);
/

alter table "TUSER"
    add constraint USER_PK
        primary key (ID);
/

INSERT INTO TUSER VALUES ('1', '1','张三', '超级管理员张三');
INSERT INTO TUSER VALUES ('2', '1','李四', '超级管理员李四');

错误解决

通过指定mybatis-config.xml启动时可能会报错:

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: java.net.ConnectException: Connection timed out: connect
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:37)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:99)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
	at org.mybatis.debug.TestMain.main(TestMain.java:30)
Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: java.net.ConnectException: Connection timed out: connect
	at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:283)
	at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:134)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.<init>(XMLConfigBuilder.java:85)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:96)
	... 2 more

这是因为指定mybatis配置验证xml规则的dtd头连接失败

<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

将头部修改为:

<!--<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">-->

<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "src\main\java\org\apache\ibatis\builder\xml\mybatis-3-config.dtd">

或者本地路径的全路径,如果在非mybatis源码包下,需要写全路径,比如自己定义的mybatis-gabriel项目。只要是mybatis的xml配置都得修改。

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "D:\StudyData\Mybatis\SourceCode\mybatis-master\src\main\java\org\apache\ibatis\builder\xml\mybatis-3-mapper.dtd">

根据自己实际项目地址修改,ctrl+鼠标左键可打开本地‘http://mybatis.org/dtd/mybatis-3-config.dtd’,将路径指定为本地路径即可。
如果在项目发布时,记得把这个路径改为服务器环境下的dtd文件地址,切记!

学习博客

调试博客
Mybatis重要类解析
四大组件Executor、StatementHandler、ParameterHandler、ResultSetHandler 详解
知乎源码解析

Mybatis设计模式
Mybtis设计模式2

  • 17
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建一个MyBatis项目的一般步骤如下: 1. 首先,你需要创建一个Spring Boot项目,并添加相关的依赖,包括Spring Boot、MyBatis Plus、Spring MVC、Shiro和Redis等。 2. 接下来,你需要在src/main/resources目录下创建一个mappers文件夹,用于存放MyBatis映射文件。 3. 创建一个名为mybatis-config.xml的核心配置文件,用于连接数据库。这个文件可以放在src/main/resources目录下。在该文件中,你可以配置数据库相关的信息,如数据库连接信息、数据库驱动、数据库方言等。 4. 在编写MyBatis映射文件时,你需要定义数据库表与Java对象之间的映射关系。可以使用注解或XML方式进行映射。同时,你还需要编写对应的SQL语句,包括查询、插入、更新和删除等操作。 5. 在Spring Boot的配置文件中,你需要配置数据库连接信息、MyBatis相关的配置信息和其他相关配置,如Redis的连接信息和Shiro的配置信息等。 6. 在编写业务逻辑代码时,你可以使用MyBatis Plus提供的便捷方法进行数据库操作,如增删改查等。同时,你还可以使用Spring MVC框架进行web开发,使用Shiro进行权限管理和认证授权,以及使用Redis进行缓存操作等。 通过以上步骤,你就可以搭建一个基于MyBatis的Spring Boot项目了。希望对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java企业报表管理系统码](https://download.csdn.net/download/m0_55416028/88269629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [MyBatis搭建](https://blog.csdn.net/HG13542386794/article/details/127967671)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值