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

相关文章

jdk1.8的Jenkins安装配置实践

《jdk1.8的Jenkins安装配置实践》Jenkins是一款流行的开源持续集成工具,支持自动构建、测试和部署,通过Jenkins,开发团队可以实现代码提交后自动进行构建、测试,并将构建结果分发到测... 目录Jenkins介绍Jenkins环境搭建Jenkins安装配置Jenkins插件安装Git安装配

JAVA Calendar设置上个月时,日期不存在或错误提示问题及解决

《JAVACalendar设置上个月时,日期不存在或错误提示问题及解决》在使用Java的Calendar类设置上个月的日期时,如果遇到不存在的日期(如4月31日),默认会自动调整到下个月的相应日期(... 目录Java Calendar设置上个月时,日期不存在或错误提示java进行日期计算时如果出现不存在的

Mybatis对MySQL if 函数的不支持问题解读

《Mybatis对MySQLif函数的不支持问题解读》接手项目后,为了实现多租户功能,引入了Mybatis-plus,发现之前运行正常的SQL语句报错,原因是Mybatis不支持MySQL的if函... 目录MyBATis对mysql if 函数的不支持问题描述经过查询网上搜索资料找到原因解决方案总结Myb

Nginx错误拦截转发 error_page的问题解决

《Nginx错误拦截转发error_page的问题解决》Nginx通过配置错误页面和请求处理机制,可以在请求失败时展示自定义错误页面,提升用户体验,下面就来介绍一下Nginx错误拦截转发error_... 目录1. 准备自定义错误页面2. 配置 Nginx 错误页面基础配置示例:3. 关键配置说明4. 生效

Nginx之https证书配置实现

《Nginx之https证书配置实现》本文主要介绍了Nginx之https证书配置的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录背景介绍为什么不能部署在 IIS 或 NAT 设备上?具体实现证书获取nginx配置扩展结果验证

mybatis-plus分表实现案例(附示例代码)

《mybatis-plus分表实现案例(附示例代码)》MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生,:本文主要介绍my... 目录文档说明数据库水平分表思路1. 为什么要水平分表2. 核心设计要点3.基于数据库水平分表注意事项示例

MySQL游标和触发器的操作流程

《MySQL游标和触发器的操作流程》本文介绍了MySQL中的游标和触发器的使用方法,游标可以对查询结果集进行逐行处理,而触发器则可以在数据表发生更改时自动执行预定义的操作,感兴趣的朋友跟随小编一起看看... 目录游标游标的操作流程1. 定义游标2.打开游标3.利用游标检索数据4.关闭游标例题触发器触发器的基

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

Mybatis的mapper文件中#和$的区别示例解析

《Mybatis的mapper文件中#和$的区别示例解析》MyBatis的mapper文件中,#{}和${}是两种参数占位符,核心差异在于参数解析方式、SQL注入风险、适用场景,以下从底层原理、使用场... 目录MyBATis 中 mapper 文件里 #{} 与 ${} 的核心区别一、核心区别对比表二、底

nginx跨域访问配置的几种方法实现

《nginx跨域访问配置的几种方法实现》本文详细介绍了Nginx跨域配置方法,包括基本配置、只允许指定域名、携带Cookie的跨域、动态设置允许的Origin、支持不同路径的跨域控制、静态资源跨域以及... 目录一、基本跨域配置二、只允许指定域名跨域三、完整示例四、配置后重载 nginx五、注意事项六、支持