纯Java代码实现Mybatis的执行调用操作

一、概述
平时大家在使用Mybatis时,通常都会使用Spring或者Spring-boot的方式简化我们的Mybatis的使用,从而达到开箱即用的效果。当然也会觉着这种方式很是方便,不过不知道大家有没有想过,为何Spring或者Spring-boot为何能帮我们做到这么简洁的么?如果我们使用原生的方式操作Mybatis不知道大家会不会使用?本章节内容就带大家深入了解一些,使用原生的方式来配置使用Mybatis的过程,让大家为以后查阅Spring整合Mybatis的源码时能有更深的体会。

二、Mybatis的介绍
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。

三、让我们先看看Mybatis的执行过程吧
在这里插入图片描述
过程说明:

  • 1、构建Configuration对象
  • 2、根据Configuration构建出SqlSessionFactory
  • 3、调用openSession()方法,建立JDBC连接
  • 4、获取Mapper代理对象
  • 5、使用代理对象,执行对应的sql

四、代码实现

@Test
public void test074() throws IOException {
  // 构建Configuration
  Configuration configuration = new Configuration();
  
  // 配置数据源
  PooledDataSource dataSource = new PooledDataSource();
  dataSource.setDriver("com.mysql.cj.jdbc.Driver");
  dataSource.setUrl("jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true");
  dataSource.setUsername("root");
  dataSource.setPassword("root");
  
  // 设置环境对象
  Environment environment = new Environment("development", new JdbcTransactionFactory(), dataSource);
  configuration.setEnvironment(environment);
  
  // 加载xml mapper文件, 同时也会加载Mapper接口中的注解
  String xmlResource = "mappers/ApikeyMapper.xml";
  InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(xmlResource);
  XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, configuration, xmlResource, configuration.getSqlFragments());
  xmlParser.parse();
  
  // 这种方式针对于没有mapper.xml文件的配置的方式
  if (!configuration.hasMapper(ApikeyMapper.class)) {
     configuration.addMapper(ApikeyMapper.class);
  }
  
  
  // 构建sqlSessionFactory
  SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);
  
  try (SqlSession sqlSession = sqlSessionFactory.openSession();) {
     ApikeyMapper apikeyMapper = sqlSession.getMapper(ApikeyMapper.class);
     System.out.println(apikeyMapper.getHashKey());
  }
}

从上面代码上看,所有的配置都在Java代码上实现,包括数据库连接的配置,mapper.xml文件的读取等。

相关代码说明:
1、读取xml中的mapper文件的方式

// 加载xml mapper文件
String xmlResource = "mappers/common/ApikeyMapper.xml";
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(xmlResource);
XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, configuration, xmlResource, configuration.getSqlFragments());
xmlParser.parse();

2、针对没有xml的mapper文件的场景

// 这种方式只能适用于注解配置的方式
configuration.addMapper(ApikeyNewMapper.class);

对应的Mapper类:

public interface ApikeyNewMapper extends MyMapper<ApiKey> {

   @Select("select hash_key from tenant_apikey_hash limit 1")
   String getHashKey();

}

3、使用mybatis-config.xml的配置文件进行配置Mybatis
如果想通过读取xml配置的话进行构建,可以直接使用下面这个进行构建SqlSessionFactory

String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

后面的代码跟上面的一致,只是SqlSessionFactory的构建方式不一样而已。

mybatis-config.xml配置如下

TYPE 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:3306/test?createDatabaseIfNotExist=true"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
         </dataSource>
      </environment>
   </environments>
   
   <!-- 设置xml mapper文件 -->
   <mappers>
      <mapper resource="mappers/ApikeyMapper.xml"/>
   </mappers>
</configuration>

至此一套完整的纯Java代码实现的Mybatis的执行调用操作介绍就结束了,感谢大家阅读,后续我会陆续更新有关于Mybatis的其他方面的文章,希望大家有喜欢的可以点赞、收藏加关注哈!!!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漂泊之云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值