• MyBatis源码基础-常用类-Configuration


    Configuration类

    a.java配置

    针对上述的xml配置,可以使用如下的java代码替换:

    @Test
    public void testConfiguration() {
        Configuration configuration = new Configuration();
    
        // 配置properties
        Properties properties = new Properties();
        properties.put("driver", "com.mysql.cj.jdbc.Driver");
        properties.put("url", "jdbc:mysql://localhost:3306/dcyMybatis");
        properties.put("username", "root");
        properties.put("password", "root");
        configuration.setVariables(properties);
        // 配置日志处理器
        configuration.setLogImpl(Slf4jImpl.class);
    
        // 配置驼峰命名
        configuration.setUseActualParamName(true);
    
        // 配置别名注册器注册一个包
        configuration.getTypeAliasRegistry().registerAliases("com.dcy.entity");
    
        // 配置Environments的 id、事务工厂、数据源
        JdbcTransactionFactory jdbcTransactionFactory = new JdbcTransactionFactory();
        PooledDataSource pooledDataSource = new PooledDataSource();
        pooledDataSource.setDriver(configuration.getVariables().getProperty("driver"));
        pooledDataSource.setUrl(configuration.getVariables().getProperty("url"));
        pooledDataSource.setUsername(configuration.getVariables().getProperty("username"));
        pooledDataSource.setPassword(configuration.getVariables().getProperty("password"));
        Environment environment = new Environment("development", jdbcTransactionFactory, pooledDataSource);
        configuration.setEnvironment(environment);
    
        // 配置Mapper
        StaticSqlSource staticSqlSource = new StaticSqlSource(configuration,"insert into account (username,money) values ('Cat', 291)");
        MappedStatement mappedStatement = new MappedStatement.Builder(
            configuration,
            "dcy.insert",
            staticSqlSource,
            SqlCommandType.INSERT
        ).build();
        configuration.addMappedStatement(mappedStatement);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    所有的mybatis.xml的配置项都可以使用Configuration类的形式进行配置

    b.构建配置类

    一个包装了XPathParser的工具类XMLConfigBuilder,可以将xml的配置文件解析成为一个配置类Configuration,代码如下:

    @Test
    public void tesXMLConfigBuilder() throws IOException {
        InputStream inputStream = new ClassPathResource("mybatis.xml").getInputStream();
        XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder(inputStream);
        Configuration configuration = xmlConfigBuilder.parse();
        log.info("Configuration -> {}", configuration);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    如何在小程序中设置导航栏文字颜色和背景颜色
    【数据结构】队列的实现与优化指南
    深度学习—cv动物/植物数据集
    WEB前端 网页设计 简介
    基于Java的磁盘调度模拟系统
    SpringBoot 统一响应返回格式格式 数组
    PriorityQueue常用接口介绍
    Elasticsearch部署中的两大常见问题及其解决方案
    万能在线答题考试小程序源码系统 既能刷题 又能考试 带完整的搭建教程
    LeetCode二叉树系列——94.二叉树的中序遍历
  • 原文地址:https://blog.csdn.net/weixin_46926189/article/details/133998849