第四章 配置文件详解

SqlMapConfig配置文件详解

<?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>
	<!-- 1.properties属性引入外部配置文件 -->
	<properties resource="org/mybatis/example/config.properties">
		<!-- property里面的属性全局均可使用 -->
		<property name="username" value="root"/>
		<property name="password" value="1234"/>
	</properties>
	
	<!-- 2.配置全局参数 -->
	<settings>
		<!-- 设置是否使用缓存 -->
		<setting name="cacheEnabked" value="true"/>
		<!-- 设置是否使用懒加载 -->
		<setting name="lazyLoadingEnabled" value="true"/>
	</settings>
	
	<!-- 3.别名设置 -->
	<typeAliases>
		<typeAlias alias="student" type="cn.com.mybatis.student"/>
		<typeAlias alias="teacher" type="cn.com.mybatis.teacher"/>
		<typeAlias alias="integer" type="java.lang.Integer"/>
	</typeAliases>
	
	<!-- 4.类型转换器(程序中是boolean,但是数据库存的是char,需要这个标签转换一下) -->
	<typeHandlers>
		<!-- 一个简单类型转换器 -->
		<typeHandler handler="org.mybatis.example.ExampleTypeHandler"/>
	</typeHandlers>
	
	<!-- 5.对象工厂 -->
	<objectFactory type="org.mybatis.example.ExampleObjectFactory">
		<!-- 对象工厂注入的参数 -->	
		<property name="someProperty" value="100"/>
	</objectFactory>
	
	<!-- 6.插件(就是个拦截器) -->
	<plugins>
		<plugin interceptor="org.mybatis.example.ExamplePlugin">
			<property name="someProperty" value="100"/>
		</plugin>
	</plugins>
	
	<!-- 7.environments数据库环境配置 -->
	<!-- 和Spring整合后environments配置将被废除 -->
	<environments default="development">
		<environment id="development">
			<!-- 使用JDBC事物管理 -->
			<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/UserMapper.xml"/>
		<mapper resource="sqlmap/OtherMapper.xml"/>
	</mappers>
</configuration>

其中需要注意的地方是

       #{}输入参数需要用占位符来标识对应的位置

       ${}用于拼接SQL语句,例如模糊查询时,用于拼接两个"%"字符串

Mapper映射文件,就是myBatis中SQL语句的配置文件,在加载SqlMapConfig.xml中,最后一项就是mapper文件的资源路径的配置,因为创建SqlSessionFactory时会加载全局配置文件SqlMapConfig.xml,这说明Mapper映射文件会在会话创建伊始就被加载了;在Mapper文件的配置信息中,输入映射配置、输出映射配置尤其重要,以及动态SQL配置

Mapper文件使用的标签如下

<?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="test">
	<!-- 1.insert 用来映射插入语句 -->
	<insert id="" parameterType="cn.com.mybatis.po.User">
		insert into user (username,password,gender) 
			values (#{username},#{password},#{gender})
	</insert>
	
	<!-- 2.update 用来映射更新语句 -->
	<update id="updateUserName" parameterType="cn.com.mybatis.po.User">
		update user set username=#{username} where id=#{id}
	</update>
	
	<!-- 3.delete 用来映射删除语句-->
	<delete id="deleteUser" parameterType="java.lang.Integer">
		delete from user where id=#{id}
	</delete>
	
	<!-- 4.select 用来映射查询语句 -->
	<select id="findUserById" parameterType="int" resultType="cn.com.mybatis.po.User">
		SELECT * FROM user WHERE id=#{id}
	</select>
	
	<!-- 5.resultMap 用来将数据库结果集取出的数据映射到相应的实体对象的相应字段中 -->
	<resultMap type="" id=""></resultMap>
	
	<!-- 6.sql 配置可以被其他语句引用的SQL语句块,其中id不能重复,不支持<where>标签,但支持<foreach>标签 -->
	<sql id="query_user_where">
		<!-- 需要复用的SQL语句,test里面的内容是判断条件 -->
		<if test="userParameter!=null">
			<if test="userParameter.username!=null">
				and username like '%${userParameter.username}%'
			</if>
			<if test="userParameter.password!=null and userParameter.password!='' ">
				and password =#{userParameter.password}
			</if>
			<!--collection指定输入对象中的集合属性,open为开始遍历时要拼接的串,close为结束时要拼接的串,item为每次遍历生成
				的对象名,separator为遍历的两个对象中间需要拼接的串  -->
			<!-- 效果相当于where (id=2 or id=4 or id=6) -->
			<foreach collection="ids" open="and id in("   close=")" item="id" separator=",">
				#{id}
			</foreach>
		</if>
	</sql>
	<!-- 为了和sql标签相对于增加的 -->
	<select id="findUserList" parameterType="userParameter" resultType="cn.com.mybatis.po.User">
		SELECT id,username,gender
		<where>
			<!-- 使用<include>标签引入sql片段,若sql片段和引用出不在同一个mapper,则必须在前面加namespace -->
			<include refid="query_user_where"></include>
			<!-- 还可以引用其他<include>标签 -->
		</where>
	</select>
	
	<!-- 7.cache 给定命名空间的缓存配置 -->
	<!-- eviction:缓存的回收策略,默认是LRU
			LRU-最近最少使用,移除最长时间不被使用的对象
			FIFO-先进先出,按对象进入缓存的顺序来移除它们
			SOFT-软引用,移除基于垃圾回收器状态和软引用规则的对象
			WEAK-弱引用,更积极地移除基于垃圾收集器和弱引用规则的对象 -->
	<!-- flushInterval:缓存刷新间隔,缓存多久清空一次,默认不清空,单位毫秒 --><!-- size:缓存存放多少个元素,默认是1024 -->
	<!-- readOnly:是否只读
			true:只读,mybatis认为所有从缓存中获取数据的操作都是只读操作,不会修改数据,为了加快获取速度,直接从缓存中取数据交给用户,不安全,但是速度快
			false:读写(默认),mybatis认为获取的数据可能被修改,mybatis会利用序列化和反序列化的技术克隆一份新的数据给你。安全,速度较慢 -->
	<cache eviction="FIFO" flushInterval="60000" readOnly="false" size="1024"></cache>
	
	<!-- 8.cache-ref 其他命名空间缓存配置的引用 -->
	<cache-ref namespace="其他mapper文件的namespace"/>
	
	<!-- 9.parameterMap 参数映射,该配置现在已被废弃 -->
	<parameterMap type="" id=""></parameterMap>
</mapper>

猜你喜欢

转载自blog.csdn.net/xhf852963/article/details/81705310