SQL中注释
//方式一
-- 1=1
//方式二【推荐使用】
<!-- 1=1 -->9.1 动态SQL概述
9.2 常用标签
9.3 示例代码
<?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.atguigu.mybatis.mapper.EmployeeMapper">
<sql id="emp_col">
id,
last_name,
email,
salary
</sql>
<sql id="select_employee">
select
id,
last_name,
email,
salary
from
tbl_employee
</sql>
<!-- 按条件查询员工信息【条件不确定】-->
<select id="selectEmpByOpr" resultType="employee">
<include refid="select_employee"></include>
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="lastName != null">
and last_name = #{lastName}
</if>
<if test="email != null">
and email = #{email}
</if>
<if test="salary != null">
and salary = #{salary}
</if>
</where>
</select>
<select id="selectEmpByOprTrim" resultType="employee">
<include refid="select_employee"></include>
<trim prefix="where" suffixOverrides="and">
<if test="id != null">
id = #{id} and
</if>
<if test="lastName != null">
last_name = #{lastName} and
</if>
<if test="email != null">
email = #{email} and
</if>
<if test="salary != null">
salary = #{salary}
</if>
</trim>
</select>
<update id="updateEmpByOpr">
update
tbl_employee
<set>
<if test="lastName != null">
last_name=#{lastName},
</if>
<if test="email != null">
email=#{email},
</if>
<if test="salary != null">
salary=#{salary}
</if>
</set>
where
id = #{id}
</update>
<select id="selectEmpByOneOpr" resultType="employee">
select
<include refid="emp_col"></include>
from
tbl_employee
<where>
<choose>
<when test="id != null">
id = #{id}
</when>
<when test="lastName != null">
last_name = #{lastName}
</when>
<when test="email != null">
email = #{email}
</when>
<when test="salary != null">
salary = #{salary}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>
<select id="selectEmpByIds" resultType="employee">
select
id,
last_name,
email,
salary
from
tbl_employee
<where>
id in(
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>
)
</where>
</select>
<insert id="batchInsertEmp">
INSERT INTO
tbl_employee(last_name,email,salary)
VALUES
<foreach collection="employees" item="emp" separator=",">
(#{emp.lastName},#{emp.email},#{emp.salary})
</foreach>
</insert>
</mapper>10.1 缓存概述
10.2 Mybatis中的缓存概述
10.3 Mybatis缓存机制之一级缓存
10.4 Mybatis缓存机制之二级缓存
10.5 Mybatis中缓存机制之第三方缓存
第三方缓存:EhCache
EhCache 是一个纯Java的进程内缓存框架
使用步骤
导入jar包
<!-- mybatis-ehcache -->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.3</version>
</dependency>
<!-- slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>编写配置文件【ehcache.xml】
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<!-- 磁盘保存路径 -->
<diskStore path="E:\mybatis\ehcache" />
<defaultCache
maxElementsInMemory="512"
maxElementsOnDisk="10000000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>加载第三方缓存【映射文件】
开始使用
注意事项