MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射

一、MyBatis配置文件概要

MyBatis核心配置文件在初始化时会被引用,在配置文件中定义了一些参数,当然可以完全不需要配置文件,全部通过编码实现,该配置文件主要是是起到解偶的作用。如第一讲中我们用到conf.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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>                     <!--加载驱动:com.mysql.jdbc.Driver-->
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis_01_user"/>  <!--数据库名称:mybatis_01_user-->
                <property name="username" value="root"/>                                    <!--用户名-->
                <property name="password" value=""/>                                        <!--密码-->

            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- 注册studentMapper.xml文件studentMapper.xml位于mapper这个目录下,所以resource写成mapper/studentMapper.xml-->
        <mapper resource="mapper/studentMapper.xml"/>
    </mappers>
</configuration>

MyBatis核心配置文件的内容和顺序如下:

  1. properties(属性)
  2. settings(全局配置参数)
  3. typeAiases(类型别名)
  4. typeHandlers(类型处理器)
  5. objectFactory(对象工厂)
  6. plugins(插件)
  7. environments(环境集合属性对象)

    1. environment(环境子属性对象)
    2. transactionManager(事物管理)
    3. datesource(数据源
  8. mappers(映射器)

二、MyBatis配置文件详解

2.1、properties属性

作用:将数据连接单独配置在db.properties中,只需要在conf.xml中加载db.properties的属性值,在conf.xml中就不需要对数据库连接参数进行硬编码。数据库连接参数只配置在db.properties中,方便对参数进行统一管理,其它xml可以引用该db.properties。

db.properties的内容:

##MySQL连接字符串
#驱动
driver=com.mysql.jdbc.Driver
#地址
url=jdbc:mysql://localhost:3306/mybatis_01_user?characterEncoding=UTF-8
#用户名
username=root
#密码
password=

在conf.xml中加载db.properties

<?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>
    <!--导入db.properties文件中的所有key-value数据-->
    <properties resource="db.properties" ></properties>

    <!--设置日志-->
    <settings>
        <!--设置是否允许缓存-->
        <setting name="cacheEnabled" value="true"/>
        <!--设置日志输出的目标-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!--别名-->
    <typeAliases>
        <!--方法一:包名+实体类(message.java)-->
        <!--定义单个别名,指定名称为message,对应的类型为com.dinghuoqin.vo.message-->
        <!--<typeAlias type="com.dinghuoqin.vo.message" alias="message"></typeAlias>-->

        <!--方法二:包名-->
        <!--指定包名下所有的类被自动扫描并定义默认别名,mybatis会自动扫描包中的pojo类,自动定义别名,别名就是类名(首字母大写或小写都可以)-->
        <package name="com.dinghuoqin.vo" ></package>
    </typeAliases>

    <!--环境配置,default为默认选择的环境-->
    <environments default="development">
        <!--开发-->
        <environment id="development">
            <!--事务管理-->
            <transactionManager type="JDBC"/>
            <!--连接池-->
            <dataSource type="POOLED">
                <!--引用属性${Key}-->
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--
            根据路径注册一个基于XML的映射器
            注册studentMapper.xml文件studentMapper.xml位于mapper这个目录下,所以resource写成mapper/studentMapper.xml
         -->
        <mapper resource="mapper/studentMapper.xml"/>
    </mappers>
</configuration>

properties特性:

注意:

  • 在properties元素体内定义的属性优先读取。
  • 然后读取properties元素中resource或url加载的属性,它会覆盖已读取的同名属性。
  • 最后读取parameterType传递的属性,它会覆盖已读取的同名属性

建议:

  不要在properties元素体内添加任何属性值,只将属性值定义在properties文件中。

  在properties文件中定义属性名要有一定的特殊性,如xxxx.xxxx(jdbc.driver)

2.2、settings全局参数配置

mybatis框架运行时可以调整一些运行参数。比如,开启二级缓存,开启延迟加载等等。全局参数会影响mybatis的运行行为。

mybatis-settings的配置属性以及描述

setting(设置) Description(描述) valid Values(验证值组) Default(默认值)
cacheEnabled 在全局范围内启用或禁用缓存配置 任何映射器在此配置下。 true | false TRUE
lazyLoadingEnabled 在全局范围内启用或禁用延迟加载。禁用时,所有相关联的将热加载。 true | false TRUE
aggressiveLazyLoading 启用时,有延迟加载属性的对象将被完全加载后调用懒惰的任何属性。否则,每一个属性是按需加载。 true | false TRUE
multipleResultSetsEnabled 允许或不允许从一个单独的语句(需要兼容的驱动程序)要返回多个结果集。 true | false TRUE
useColumnLabel 使用列标签,而不是列名。在这方面,不同的驱动有不同的行为。参考驱动文档或测试两种方法来决定你的驱动程序的行为如何。 true | false TRUE
useGeneratedKeys 允许JDBC支持生成的密钥。兼容的驱动程序是必需的。此设置强制生成的键被使用,如果设置为true,一些驱动会不兼容性,但仍然可以工作。 true | false FALSE
autoMappingBehavior 指定MyBatis的应如何自动映射列到字段/属性。NONE自动映射。 PARTIAL只会自动映射结果没有嵌套结果映射定义里面。 FULL会自动映射的结果映射任何复杂的(包含嵌套或其他)。
NONE,PARTIAL,FULL

PARTIAL
defaultExecutorType 配置默认执行人。SIMPLE执行人确实没有什么特别的。 REUSE执行器重用准备好的语句。 BATCH执行器重用语句和批处理更新。
SIMPLE,REUSE,BATCH

SIMPLE
safeRowBoundsEnabled 允许使用嵌套的语句RowBounds。 true | false FALSE
mapUnderscoreToCamelCase 从经典的数据库列名A_COLUMN启用自动映射到骆驼标识的经典的Java属性名aColumn。 true | false FALSE
localCacheScope MyBatis的使用本地缓存,以防止循环引用,并加快反复嵌套查询。默认情况下(SESSION)会话期间执行的所有查询缓存。如果localCacheScope=STATMENT本地会话将被用于语句的执行,只是没有将数据共享之间的两个不同的调用相同的SqlSession。
SESSION

STATEMENT

SESSION
dbcTypeForNull 指定为空值时,没有特定的JDBC类型的参数的JDBC类型。有些驱动需要指定列的JDBC类型,但其他像NULL,VARCHAR或OTHER的工作与通用值。 JdbcType enumeration. Most common are: NULL, VARCHAR and OTHER OTHER
lazyLoadTriggerMethods 指定触发延迟加载的对象的方法。 A method name list separated by commas equals,clone,hashCode,toString
defaultScriptingLanguage 指定所使用的语言默认为动态SQL生成。 A type alias or fully qualified class name.
org.apache.ibatis.scripting.xmltags

.XMLDynamicLanguageDriver

callSettersOnNulls 指定如果setter方法或map的put方法时,将调用检索到的值是null。它是有用的,当你依靠Map.keySet()或null初始化。注意(如整型,布尔等)不会被设置为null。 true | false FALSE
logPrefix 指定的前缀字串,MyBatis将会增加记录器的名称。 Any String Not set
logImpl 指定MyBatis的日志实现使用。如果此设置是不存在的记录的实施将自动查找。 SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING Not set
proxyFactory 指定代理工具,MyBatis将会使用创建懒加载能力的对象。 CGLIB | JAVASSIST  CGLIB

 官方文档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>

示例:

这里设置MyBatis的日志输出到控制台:

    <!--设置日志-->
    <settings>
        <!--设置是否允许缓存-->
        <setting name="cacheEnabled" value="true"/>
        <!--设置日志输出的目标-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

结果 :

2.3、typeAiases(别名)

在mapper.xml中,定义很多的statement,statement需要parameterType指定输入参数的类型、需要resultType指定输出结果的映射类型。

如果在指定类型时输入类型全路径,不方便进行开发,可以针对parameterType或resultType指定的类型定义一些别名,在mapper.xml中通过别名定义,方便开发。

如下所示类型com.dinghuoqin.vo.message会反复出现,冗余:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.dinghuoqin.dao.studentMapper">

    <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为selectStudentById,id属性值必须是唯一的,不能够重复
         使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
         resultType="com.dinghuoqin.vo.message"就表示将查询结果封装成一个message类的对象返回
         message类就是message表所对应的实体类
     -->

    <!--查询-->
    <!--查询所有数据-->
    <select id="selectStudent"  resultType="com.dinghuoqin.vo.message">
        select id,username,password,name,email,state from  user
    </select>
    <!--根据id查询数据-->
    <select id="selectStudentById" resultType="com.dinghuoqin.vo.message">
        select id,username,password,name,email,state from  user where id = #{id}
    </select>
    <!--根据姓名模糊查询数据-->
    <select id="selectStudentByName" resultType="com.dinghuoqin.vo.message">
        select id,username,password,name,email,state from  user where name  like ‘%${value}%‘;
    </select>
    <!--分页查询-->
    <select id="selectStudentFY" resultType="com.dinghuoqin.vo.message" parameterType="java.lang.Integer">
        select id,username,password,name,email,state from  user limit #{page},#{number};
    </select>

    <!--根据id删除数据-->
    <delete id="deleteStudentById" parameterType="java.lang.Integer">
        delete  from user  where id=#{id}
    </delete>

    <!--添加数据-->
    <insert id="insertStudent" parameterType="com.dinghuoqin.vo.message" >
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer" >
        SELECT LAST_INSERT_ID() AS id;
    </selectKey>
        insert into user(username,password,name,email,state) values(#{username},#{password},#{name},#{email},#{state});
    </insert>

    <!--根据id修改数据修改-->
    <update id="updateStudent" parameterType="com.dinghuoqin.vo.message">
        update user set username=#{username},password=#{password},name=#{name},email=#{email},state=#{state} where id=#{id}
    </update>
</mapper>

2.3.1.MyBatis默认支持的别名


别名


映射的类型


_byte


byte


_long


long


_short


short


_int


int


_integer


int


_double


double


_float


float


_boolean


boolean


string


String


byte


Byte


long


Long


short


Short


int


Integer


integer


Integer


double


Double


float


Float


boolean


Boolean


date


Date


decimal


BigDecimal


bigdecimal


BigDecimal

2.3.2.自定义别名

(一)、单个别名定义(在conf.xml) 

    <!--别名-->
    <typeAliases>
        <!--定义单个别名,指定名称为message,对应的类型为com.dinghuoqin.vo.message-->
       <typeAlias type="com.dinghuoqin.vo.message" alias="message"></typeAlias>
    </typeAliases>

studentMapper.xml引用别名

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.dinghuoqin.dao.studentMapper">

    <!--查询-->
    <!--查询所有数据-->
    <select id="selectStudent"  resultType="message">
        select id,username,password,name,email,state from  user
    </select>
    <!--根据id查询数据-->
    <select id="selectStudentById" resultType="Message">
        select id,username,password,name,email,state from  user where id = #{id}
    </select>
    <!--根据姓名模糊查询数据-->
    <select id="selectStudentByName" resultType="message">
        select id,username,password,name,email,state from  user where name  like ‘%${value}%‘;
    </select>
    <!--分页查询-->
    <select id="selectStudentFY" resultType="message" parameterType="java.lang.Integer">
        select id,username,password,name,email,state from  user limit #{page},#{number};
    </select>

    <!--根据id删除数据-->
    <delete id="deleteStudentById" parameterType="java.lang.Integer">
        delete  from user  where id=#{id}
    </delete>

    <!--
        添加数据
    -->
    <insert id="insertStudent" parameterType="message" >
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer" >
        SELECT LAST_INSERT_ID() AS id;
    </selectKey>
        insert into user(username,password,name,email,state) values(#{username},#{password},#{name},#{email},#{state});
    </insert>

    <!--根据id修改数据修改-->
    <update id="updateStudent" parameterType="message">
        update user set username=#{username},password=#{password},name=#{name},email=#{email},state=#{state} where id=#{id}
    </update>
</mapper>

(二)批量定义别名,扫描指定的包

定义单个别名的缺点很明显,如果项目中有很多别名则需要一个一个定义,且修改类型了还要修改配置文件非常麻烦,可以指定一个包,将下面所有的类都按照一定的规则定义成别名:

<!--别名-->
    <typeAliases>
        <!--方法一:包名+实体类(message.java)-->
        <!--定义单个别名,指定名称为message,对应的类型为com.dinghuoqin.vo.message-->
        <!--<typeAlias type="com.dinghuoqin.vo.message" alias="message"></typeAlias>-->

        <!--方法二:包名-->
        <!--指定包名下所有的类被自动扫描并定义默认别名,mybatis会自动扫描包中的pojo类,自动定义别名,别名就是类名(首字母大写或小写都可以)-->
        <package name="com.dinghuoqin.vo" ></package>
    </typeAliases>

如果com.dinghuoqin.vo包下有一个名为message的类,则使用别名时可以是:message,或Message。

如果两个名称相同时的冲突问题,可以使用注解解决

2.4、typeHandlers(类型处理器)

mybatis中通过typeHandlers完成jdbc类型和java类型的转换。

通常情况下,mybatis提供的类型处理器满足日常需要,不需要自定义.

mybatis支持类型处理器:


类型处理器


Java类型


JDBC类型


BooleanTypeHandler


Boolean,boolean


任何兼容的布尔值


ByteTypeHandler


Byte,byte


任何兼容的数字或字节类型


ShortTypeHandler


Short,short


任何兼容的数字或短整型


IntegerTypeHandler


Integer,int


任何兼容的数字和整型


LongTypeHandler


Long,long


任何兼容的数字或长整型


FloatTypeHandler


Float,float


任何兼容的数字或单精度浮点型


DoubleTypeHandler


Double,double


任何兼容的数字或双精度浮点型


BigDecimalTypeHandler


BigDecimal


任何兼容的数字或十进制小数类型


StringTypeHandler


String


CHAR和VARCHAR类型


ClobTypeHandler


String


CLOB和LONGVARCHAR类型


NStringTypeHandler


String


NVARCHAR和NCHAR类型


NClobTypeHandler


String


NCLOB类型


ByteArrayTypeHandler


byte[]


任何兼容的字节流类型


BlobTypeHandler


byte[]


BLOB和LONGVARBINARY类型


DateTypeHandler


Date(java.util)


TIMESTAMP类型


DateOnlyTypeHandler


Date(java.util)


DATE类型


TimeOnlyTypeHandler


Date(java.util)


TIME类型


SqlTimestampTypeHandler


Timestamp(java.sql)


TIMESTAMP类型


SqlDateTypeHandler


Date(java.sql)


DATE类型


SqlTimeTypeHandler


Time(java.sql)


TIME类型


ObjectTypeHandler


任意


其他或未指定类型


EnumTypeHandler


Enumeration类型


VARCHAR-任何兼容的字符串类型,作为代码存储(而不是索引)。

2.5、mappers(映射配置)

映射配置可以有多种方式,如下XML配置所示:

<!-- 将sql映射注册到全局配置中-->
    <mappers>

        <!--
            mapper 单个注册(mapper如果多的话,不太可能用这种方式)
            resource:引用类路径下的文件
            url:引用磁盘路径下的资源
            class,引用接口
            package 批量注册(基本上使用这种方式)
            name:mapper接口与mapper.xml所在的包名
        -->

        <!-- 第一种:注册sql映射文件-->
        <mapper resource="com/ding/mapper/UserMapper.xml" />

        <!-- 第二种:注册接口sql映射文件必须与接口同名,并且放在同一目录下-->
        <mapper class="com.ding.mapper.UserMapper"/>
        <!-- 第三种:注册基于注解的接口  基于注解   没有sql映射文件,所有的sql都是利用注解写在接口上-->
        <mapper class="com.ding.mapper.TeacherMapper"/>
        <!-- 第四种:批量注册  需要将sql配置文件和接口放到同一目录下-->
        <package name="com.ding.mapper"/>
    </mappers>

2.5.1、通过resource加载单个映射文件

    <mappers>
        <!--
            根据路径注册一个基于XML的映射器
            注册studentMapper.xml文件studentMapper.xml位于mapper这个目录下,所以resource写成mapper/studentMapper.xml
         -->
        <mapper resource="mapper/studentMapper.xml"/>
    </mappers>

2.5.2:通过mapper接口加载单个映射文件

    <mappers>
        <!-- 通过mapper接口加载单个映射配置文件
           遵循一定的规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录中;
           上边规范的前提是:使用的是mapper代理方法;
        -->
        <mapper class="com.dinghuoqin.daoPort.StudentDaoI"></mapper>
    </mappers>

原文地址:https://www.cnblogs.com/huoqin/p/9715411.html

时间: 2024-07-31 11:57:00

MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射的相关文章

Mybatis学习记录(二)--Mybatis开发DAO方式

mybatis开发dao的方法通常用两种,一种是传统DAO的方法,一种是基于mapper代理的方法,下面学习这两种开发模式. 写dao之前应该要对SqlSession有一个更加细致的了解 一.mybatis的SqlSession使用范围 SqlSessionFactoryBuilder用于创建SqlSessionFacoty,SqlSessionFacoty一旦创建完成就不需要SqlSessionFactoryBuilder了,因为SqlSession是通过SqlSessionFactory生产

MyBatis学习笔记二:MyBatis生产中使用环境搭建

这里是在上一个环境的基础上修改的,这里就不在给出所有的配置,只给出哪里修改的配置 1.修改POJO对象为注解方式 2.创建Dao层接口 package com.orange.dao; import com.orange.model.Person; public interface PersonDao { // 这里的返回值和方法名必须和PersonMapper.xml中定义的执行语句的id一致 public Person selectPerson(); } 3.修改PersonMapper.xm

MyBatis学习(四)XML配置文件之SQL映射的XML文件

SQL映射文件常用的元素: 1.select 查询语句是MyBatis最常用的语句之一. 执行简单查询的select元素是非常简单的: <select id="selectUser" parameterType="int" resultType="hashmap"> SELECT * FROM PERSON WHERE ID = #{id} </select> 这个语句被称作selectUser,接受一个int类型的参数,

mybatis学习笔记二mybatis结合spring mvc实现(用户登录,数据查询)

接着上次的来,我做了一个用户登录的例子 UserController: package com.yihaomen.controller; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.spring

MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作(转载)

孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(二)--使用MyBatis对表执行CRUD操作 上一篇博文MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对users表执行CRUD操作.本文中使用到的测试环境是上一篇博文中的测试环境. 一.使用MyBatis对表执行CRUD操作--基于XML的实现 1.定义sql映射xml文件 userMa

MyBatis学习总结(一)——MyBatis快速入门(转载)

孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(一)--MyBatis快速入门 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录. 二.mybatis快速入门 2.1.准备

【转】MyBatis学习总结(一)——MyBatis快速入门

[转]MyBatis学习总结(一)——MyBatis快速入门 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录. 二.mybatis快速入门 2.1.准备开发环境 1.创建测试项目,普通jav

mybatis学习笔记(14)-mybatis整合ehcache

mybatis学习笔记(14)-mybatis整合ehcache mybatis学习笔记14-mybatis整合ehcache 分布缓存 整合方法掌握 整合ehcache 加入ehcache的配置文件 ehcache是一个分布式缓存框架 分布缓存 我们系统为了提高系统并发,性能.一般对系统进行分布式部署(集群部署方式) 不使用分布缓存,缓存的数据在各各服务单独存储,不方便系统开发.所以要使用分布式缓存对缓存数据进行集中管理. mybatis无法实现分布式缓存,需要和其它分布式缓存框架进行整合.

MyBatis学习总结(七)——Mybatis缓存(转载)

孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(七)--Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 Cache 就将清空. 2. 二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,Hash