微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

谷哥的小弟学后台(38)——SqlMapConfig.xml

探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制

Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南

自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理

版权声明

在之前关于Mybatis的示例中我们采用sqlMapConfig.xml配置数据库连接池和管理mapper.xml,今天我们来继续学习sqlMapConfig.xml常用的其他配置。

sqlMapConfig.xml中配置的内容和顺序如下:

properties
settings
typeAliases
typeHandlers
objectFactory
plugins
environments
    environment
        transactionManager
        dataSource
mappers

在此,介绍几个常用的。

properties

我们可将一些配置信息专门写在对应的配置文件中,然后在sqlMapConfig.xml引用即可。比如,将数据库配置信息置于db.properties中:

driver=com.MysqL.jdbc.Driver
url=jdbc:MysqL://localhost:3306/mb?characterEncoding=utf-8
username=root
password=root

然后在sqlMapConfig.xml中引用

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="db.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="sqlmap/StudentMapper.xml" />
    </mappers>
</configuration>
  • 引入db.properties,请参见代码第4行
  • 使用db.properties中的内容,请参见代码第9—12行

setting

可在sqlMapConfig.xml中利用setting配置全局参数从而作用于MyBatis的行为

<settings>  
        <setting name="cacheEnabled" value="true" />  
        <setting name="lazyLoadingEnabled" value="true" />  
        <setting name="multipleResultSetsEnabled" value="true" />  
        <setting name="useColumnLabel" value="true" />  
        <setting name="useGeneratedKeys" value="false" />  
        <setting name="autoMappingBehavior" value="PARTIAL" />  
        <setting name="defaultExecutorType" value="SIMPLE" />  
        <setting name="defaultStatementTimeout" value="25" />  
        <setting name="safeRowBoundsEnabled" value="false" />  
        <setting name="mapUnderscoreToCamelCase" value="false" />  
        <setting name="localCacheScope" value="SESSION" />  
        <setting name="jdbcTypeForNull" value="OTHER" />  
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />  
    </settings>

关于这些setting的具体作用,请官方文档,在此不再赘述

typeAliases

typeAliases可为java类型设置一个短的名字从而减少类完全限定名的冗余.
MyBatis已经为许多常见的java类型内建了相应的类型别名,如下所示:

在mapper.xml中会定义很多的statement,每个statement需要利用parameterType指定输入参数的类型、利用resultType指定输出结果的映射类型。如果在指定类型时输入类型全路径会非常的繁琐,所以我们可利用typeAliases为这些类指定别名,从而提高开发效率

比如,我们可以为单个类指定别名

<typeAliases>
        <typeAlias type="cn.com.Student" alias="Student" />
</typeAliases>

所以在mapper.xml中将parameterType指定为Student即可

<insert id="insertStudent" parameterType="Student">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
            SELECT LAST_INSERT_ID()
        </selectKey>
        INSERT INTO student (name,gender,birthday) value (#{name},#{gender},#{birthday})
    </insert>

当然,我们还可以为某个包下所有类指定别名。

<typeAliases>
        <package name="cn.com"/>
</typeAliases>

这些包下的类名所对应的别名就不再是全路径了,而是java文件本身的名字了。比如cn.com.Student的别名就是Student

typeHandlers

无论是MyBatis在预处理语句(PreparedStatement)中设置一个参数时,还是从结果集中取出一个值时,都会用typeHandlers类型处理器将获取的值以合适的方式进行转换。简单地说:MyBatis中通过typeHandlers完成JDBC类型和java类型的转换。Mybatis认为我们实现了许多TypeHandler,当我们没有配置指定TypeHandler时,Mybatis会根据参数或者返回结果的不同为我们选择合适的TypeHandler进行数据转换处理。

mappers

mappers用于配置sql映射文件

第一种方式

<mappers>
    <mapper resource="sqlmap/StudentMapper.xml" />
</mappers>

利用resource加载单个映射文件,我们在MyBatis的入门示例中就是如此配置的。

第二种方式

通过mapper接口(mapper.java)加载单个映射文件(mapper.xml)

<mappers>
        <mapper class="cn.com.StudentMapper"/>
</mappers>

利用该方式配置mapper时务必注意:必须将xxxmapper.xml和xxxmaper.java置于同一个包下,且文件名必须一致。

第三种方式

批量配置mapper

<mappers>
        <package name="cn.com"/>
</mappers>

配置一个包中的mapper。利用该方式配置mapper时务必注意:必须将xxxmapper.xml和xxxmaper.java置于同一个包下,且文件名必须一致。

原文地址:https://www.jb51.cc/xml/294345.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。