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

本文主要是介绍0基础学习Mybatis系列数据库操作框架——配置中字段顺序问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

大纲

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

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

typeAliases

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

<select id="findAll" resultType="org.example.model.AllType">select * from all_type
</select>
<select id="findOne" resultType="org.example.model.AllType">select * from all_type where info_int = #{info_int}
</select>

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

    <typeAliases><typeAlias type="org.example.model.AllType" alias="AllType"/></typeAliases>

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

    <select id="findAll" resultType="AllType">select * from all_type</select><select id="findOne" resultType="AllType">select * from all_type where info_int = #{info_int}</select>

settings

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

    <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]

字段顺序

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

<?xml version="1.0" encoding="UTF-8" ?>
<!-- mybatis-config-2.xml -->
<!DOCTYPE configurationPUBLIC "-//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前,如下(错误的)

<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

参考资料

  • https://mybatis.org/mybatis-3/zh_CN/configuration.html
  • https://mybatis.org/mybatis-3/zh_CN/sqlmap-xml.html

这篇关于0基础学习Mybatis系列数据库操作框架——配置中字段顺序问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/872545

相关文章

MyBatis与其使用方法示例详解

《MyBatis与其使用方法示例详解》MyBatis是一个支持自定义SQL的持久层框架,通过XML文件实现SQL配置和数据映射,简化了JDBC代码的编写,本文给大家介绍MyBatis与其使用方法讲解,... 目录ORM缺优分析MyBATisMyBatis的工作流程MyBatis的基本使用环境准备MyBati

IDEA与JDK、Maven安装配置完整步骤解析

《IDEA与JDK、Maven安装配置完整步骤解析》:本文主要介绍如何安装和配置IDE(IntelliJIDEA),包括IDE的安装步骤、JDK的下载与配置、Maven的安装与配置,以及如何在I... 目录1. IDE安装步骤2.配置操作步骤3. JDK配置下载JDK配置JDK环境变量4. Maven配置下

C#中的 Dictionary常用操作

《C#中的Dictionary常用操作》C#中的DictionaryTKey,TValue是用于存储键值对集合的泛型类,允许通过键快速检索值,并且具有唯一键、动态大小和无序集合的特性,常用操作包括添... 目录基本概念Dictionary的基本结构Dictionary的主要特性Dictionary的常用操作

C# winform操作CSV格式文件

《C#winform操作CSV格式文件》这篇文章主要为大家详细介绍了C#在winform中的表格操作CSV格式文件的相关实例,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录实例一实例效果实现代码效果展示实例二实例效果完整代码实例一实例效果当在winform界面中点击读取按钮时 将csv中

Springboot的自动配置是什么及注意事项

《Springboot的自动配置是什么及注意事项》SpringBoot的自动配置(Auto-configuration)是指框架根据项目的依赖和应用程序的环境自动配置Spring应用上下文中的Bean... 目录核心概念:自动配置的关键特点:自动配置工作原理:示例:需要注意的点1.默认配置可能不适合所有场景

Nginx配置系统服务&设置环境变量方式

《Nginx配置系统服务&设置环境变量方式》本文介绍了如何将Nginx配置为系统服务并设置环境变量,以便更方便地对Nginx进行操作,通过配置系统服务,可以使用系统命令来启动、停止或重新加载Nginx... 目录1.Nginx操作问题2.配置系统服android务3.设置环境变量总结1.Nginx操作问题

基于.NET编写工具类解决JSON乱码问题

《基于.NET编写工具类解决JSON乱码问题》在开发过程中,我们经常会遇到JSON数据处理的问题,尤其是在数据传输和解析过程中,很容易出现编码错误导致的乱码问题,下面我们就来编写一个.NET工具类来解... 目录问题背景核心原理工具类实现使用示例总结在开发过程中,我们经常会遇到jsON数据处理的问题,尤其是

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

springboot3.4和mybatis plus的版本问题的解决

《springboot3.4和mybatisplus的版本问题的解决》本文主要介绍了springboot3.4和mybatisplus的版本问题的解决,主要由于SpringBoot3.4与MyBat... 报错1:spring-boot-starter/3.4.0/spring-boot-starter-

Keepalived+Nginx双机配置小结

《Keepalived+Nginx双机配置小结》本文主要介绍了Keepalived+Nginx双机配置小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1.1 软硬件要求1.2 部署前服务器配置调优1.3 Nginx+Keepalived部署1.3