您的位置:首页 > 其它

Mybatis配置解释

2020-03-28 20:18 246 查看

一、前言

本文主要记录学习Mybatis的一些配置的解释,通过本文和Mybatis3官方文档的配合希望也能够对初学者有所帮助。
注意仅仅为一些关键配置描述,直接cv并不能直接使用!!!

二、基础核心配置

<?xml version="1.0" encoding="UTF-8"?>

<!--注意这里的configuration,Mapper配置不一样的,不要搞混-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
<!--引入外部配置文件,给予下面的${}调用,当然你也可以直接写-->
<properties resource="db.yaml" />

<!--mybatis的一些设置-->
<settings>
<!--开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射-->
<setting name="mapUnderscoreToCamelCase" value="true" />
<!--开启二级数据缓存(一级缓存默认开启)-->
<setting name="cacheEnabled" value="true"/>
</settings>

<!--注册别名,方便缩写-->
<typeAliases>
<!--扫描这个包下的所有类,没有注解使用类名称,有使用注解名称-->
<package name="com.twelvet.entity"/>
</typeAliases>

<!--默认配置-->
<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>

<!--每一个Mpapper.xml都需要在mybatis核心配置文件注册-->
<mappers>
<!--注意mybatis默认不支持通配符的,需要一个一个写,确实需要的请自行改写-->
<mapper resource="mapper/StudentMapper.xml" />
<!--<mapper class="com.twelvet.dao.UserMapper" />-->
<!--<package name="com.twelvet.dao"/>-->
</mappers>

</configuration>

对应的*Mapper.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">
<!--需要指定命名空间:防止映射id重复-->
<mapper namespace="com.twelvet.dao.UserMapper">

<!--在当前mapper.xml中使用二级缓存-->
<cache
<!--
LRU – 最近最少使用:移除最长时间不被使用的对象。
FIFO – 先进先出:按对象进入缓存的顺序来移除它们。
SOFT – 软引用:基于垃圾回收器状态和软引用规则移除对象。
WEAK – 弱引用:更积极地基于垃圾收集器状态和弱引用规则移除对象。
-->
eviction="FIFO"
<!--自动刷新时间(秒),仅仅会在调用语句时刷新-->
flushInterval="60000"
<!--最大缓存数目-->
size="512"
<!--是否只读-->
readOnly="true"/>

<!--
id;对应接口名称
parameterType:参数类型
resultType:结果类型
resultMap:结果类型映射(配置)
-->
<select id="queryBlogIf" parameterType="map" resultType="blog">
select * from blog

<where>
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>

</select>

<!--
id:resultMap的属性
type:结果类型
-->
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"></result>
<result property="name" column="name"></result>

<!--
复杂的属性,我们需要单独处理
对象:association
property:student属性
column:数据库列表
javaType:java类型
select:调用的xml配置
-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>

<!--
复杂的属性,我们需要单独处理
集合:collection
ofType:申明这个集合的类型
-->
<collection property="students" javaType="ArrayList" ofType="Student" select="getTeacher" column="id" />
</resultMap>
<!--给予上面的select调用-->
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{tid}
</select>

</mapper>
  • 点赞
  • 收藏
  • 分享
  • 文章举报
twelvet 发布了5 篇原创文章 · 获赞 0 · 访问量 295 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: