前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >0基础学习Mybatis系列数据库操作框架——配置中字段顺序问题

0基础学习Mybatis系列数据库操作框架——配置中字段顺序问题

作者头像
方亮
发布2024-05-24 19:31:15
540
发布2024-05-24 19:31:15
举报
文章被收录于专栏:方亮方亮
大纲

  • typeAliases
  • settings
  • 字段顺序
  • 参考资料

我们在《0基础学习Mybatis系列数据库操作框架——多环境配置》中,给配置文件新增了properties字段,让这些属性值可以被同文件中其他地方引用,简化了文件。

typeAliases

我们还可以使用typeAliases定义一些值,让SQL Mapper XML中引用。 比如我们所有的查找操作,返回的都是"org.example.model.AllType"。在SQL Mapper XML(AllTypeMapper.xml)中如下使用。

代码语言:javascript
复制
<select id="findAll" resultType="org.example.model.AllType">
      select * from all_type
</select>
<select id="find" resultType="org.example.model.AllType">
    select * from all_type where info_int = #{info_int}
</select>

如果我们觉得这个值太长,可以在Mybatis配置文件(mybatis-config.xml)中新增如下字段

代码语言:javascript
复制
    <typeAliases>
        <typeAlias type="org.example.model.AllType" alias="AllType"/>
    </typeAliases>

然后将在SQL Mapper XML中org.example.model.AllType替换成AllType即可。

代码语言:javascript
复制
    <select id="findAll" resultType="AllType">
        select * from all_type
    </select>
    <select id="find" resultType="AllType">
        select * from all_type where info_int = #{info_int}
    </select>

settings

除了这些简化配置的功能,Mybatis配置文件还可以给框架设置一些属性。比如我们希望执行过程可以输出到终端,则需要加入如下配置即可

代码语言:javascript
复制
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

这样我们执行代码时就可以看大SQL语句模板

Logging initialized using ‘class org.apache.ibatis.logging.stdout.StdOutImpl’ adapter. Opening JDBC Connection ==> Preparing: insert into all_type(info_int, info_tint, info_sint) values (?, ?, ?) , (?, ?, ?) , (?, ?, ?) , (?, ?, ?) , (?, ?, ?) , (?, ?, ?) , (?, ?, ?) , (?, ?, ?) , (?, ?, ?) , (?, ?, ?) ==> Parameters: 100(Integer), 100(Byte), 100(Short), 101(Integer), 101(Byte), 101(Short), 102(Integer), 102(Byte), 102(Short), 103(Integer), 103(Byte), 103(Short), 104(Integer), 104(Byte), 104(Short), 105(Integer), 105(Byte), 105(Short), 106(Integer), 106(Byte), 106(Short), 107(Integer), 107(Byte), 107(Short), 108(Integer), 108(Byte), 108(Short), 109(Integer), 109(Byte), 109(Short) <== Updates: 10 Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@78a287ed]

字段顺序

经过这番修改,我们的配置文件最后如下

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<!-- mybatis-config-2.xml -->
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties>
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="username" value="root"/>
        <property name="password" value="fangliang"/>
    </properties>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <typeAlias type="org.example.model.AllType" alias="AllType"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="jdbc:mysql://localhost:3306/testdb?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mybatis/mapper/AllTypeMapper-2.xml"/>
    </mappers>

</configuration>

这儿特别需要注意的是:configuration下字段是有顺序的。 假如我们将settings放在properties前,如下(错误的)

代码语言:javascript
复制
<configuration>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <properties>
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="username" value="root"/>
        <property name="password" value="fangliang"/>
    </properties>
    ……
<configuration>

就会报错:

The content of element type “configuration” must match “(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)”.

这句话的意思是:configuration下字段需要按如下顺序排列。

  1. properties
  2. settings
  3. typeAliases
  4. typeHandlers
  5. objectFactory
  6. objectWrapperFactory
  7. reflectorFactory
  8. plugins
  9. environments
  10. databaseIdProvider
  11. mappers

参考资料

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-04-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 大纲
  • typeAliases
  • settings
  • 字段顺序
  • 参考资料
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档