mybatis(二)自定义基于注解的mybatis

本文介绍如何自定义基于注解的MyBatis框架,首先从pom文件中移除MyBatis依赖并引入XML解析库。接着详细讲解了自定义过程:从Resources类到SqlSessionFactoryBuilder,再到SqlSessionFactory接口及其实现类,然后是SqlSession及其实现,最后阐述如何利用Java动态代理在运行时创建代理对象,以实现接口与mapper.xml的结合,完成业务方法的调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里我们自定义mybatis框架,pom文件中移除mybatis的相关依赖,由于是基于xml格式的,我们需要引入解析xml的相关依赖。

 <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.1.6</version>
        </dependency>
    </dependencies>

在这里插入图片描述
由入门案列可知:
第一步:自定义读取配置文件的Resources类

public class Resources {

    /**
     * 根据传入的参数,获取一个字节输入流
     * @param filePath
     * @return
     */
    public static InputStream getResourceAsStream(String filePath){
        return Resources.class.getClassLoader().getResourceAsStream(filePath);
    }
}

第二步:自定义SqlSessionFactoryBuild类

public class SqlSessionFactoryBuilder {

    /**
     * 根据参数的字节输入流来构建一个SqlSessionFactory工厂
     * @param config
     * @return
     */
    public SqlSessionFactory build(InputStream config){
        Configuration cfg = XMLConfigBuilder.loadConfiguration(config);
        return  new DefaultSqlSessionFactory(cfg);
    }
}

第三步:自定义SqlSessionFactory接口和SqlSessionFactory的实现类

public interface SqlSessionFactory {

    /**
     * 用于打开一个新的SqlSession对象
     * @return
     */
    SqlSession openSession();
}

SqlSessionFactory 的实现类

public class SqlSeesionFactoryImpl implements SqlSessionFactory {
    private Configuration cfg;

    public SqlSeesionFactoryImpl(Configuration cfg){
        this.cfg = cfg;
    }

    /**
     * 用于创建一个新的操作数据库对象
     * @return
     */
    @Override
    public SqlSession openSession() {
        return new SqlSessionImpl(cfg);
    }
}

第四步:自定义SqlSeesion和实现类

public interface SqlSession<T> {
   <T> T getMapper(Class<T> userDaoClass);

   void close();
}

public class SqlSessionImpl<T> implements SqlSession<T> {

    private Configuration cfg;
    private Connection connection;

    public SqlSessionImpl(Configuration cfg){
        this.cfg = cfg;
        connection = DataSourceUtil.getConnection(cfg);
    }

    /**
     * 用于创建代理对象
     * @param  dao的接口字节码
     * @param <T>
     * @return
     */
    @Override
    public <T> T getMapper(Class<T> daoInterfaceClass) {
        return (T) Proxy.newProxyInstance(daoInterfaceClass.getClassLoader(),
                new Class[]{daoInterface
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值