目录
一.概述
1.简介
2.maven构建
二.相关概念
1.Mapper接口
2.ORM思想
三.映射配置文件
1.文件结构
2.映射配置文件标签详解
3.SQL语句中参数的获取
(1)获取方式
(2)参数类型
4.各种SQL操作
5.处理表字段和实体类属性名不一致的情况
6.多对一映射关系的处理
7.一对多映射关系的处理
8.分布查询的优点
9.动态SQL
四.核心配置文件
1.文件结构
2.核心配置文件详解
(1)标签顺序
(2)标签详解
五.相关API
1.Resources
2.SqlSessionFactoryBuilder
3.SqlSessionFactory
4.SqlSession
5.最佳实践
六.缓存
1.一级缓存
2.二级缓存
3.缓存的查询顺序
一.概述
1.简介
MyBatis是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis免除了几乎所有的JDBC代码以及设置参数和获取结果集的工作。MyBatis可以通过简单的XML或注解来配置和映射原始类型、接口和Java POJO(Plain Old Java Objects,普通老式Java对象)为数据库中的记录
2.maven构建
将MyBatis相关依赖导入项目,pom.xml添加如下配置
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>org.mybatis<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>mybatis<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>3.5.7<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span></span>
将Mysql相关依赖导入
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>mysql<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>mysql-connector-java<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>5.1.37<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span></span>
将Junit相关依赖代入
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>junit<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>junit<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>4.12<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">scope</span><span style="color:#117700">></span>test<span style="color:#117700"></</span><span style="color:#117700">scope</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span></span>
将log4j相关依赖导入
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>log4j<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>log4j<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>1.2.17<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span></span>
log4j的配置文件名为log4j.xml,存放的位置是src/main/resources目录下:
<span style="background-color:#f8f8f8"><span style="color:#555555"><?xml</span> <span style="color:#555555">version="1.0" encoding="UTF-8" ?></span><span style="color:#555555"><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"></span><span style="color:#117700"><</span><span style="color:#117700">log4j:configuration</span> <span style="color:#0000cc">xmlns:log4j</span>=<span style="color:#aa1111">"http://jakarta.apache.org/log4j/"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">appender</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"STDOUT"</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"org.apache.log4j.ConsoleAppender"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">param</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"Encoding"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"UTF-8"</span> <span style="color:#117700">/></span> <span style="color:#117700"><</span><span style="color:#117700">layout</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"org.apache.log4j.PatternLayout"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">param</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"ConversionPattern"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"%-5p %d{MM-dd HH:mm:ss,SSS}%m (%F:%L) \n"</span> <span style="color:#117700">/></span> <span style="color:#117700"></</span><span style="color:#117700">layout</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">appender</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">logger</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"java.sql"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">level</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"debug"</span> <span style="color:#117700">/></span> <span style="color:#117700"></</span><span style="color:#117700">logger</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">logger</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"org.apache.ibatis"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">level</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"info"</span> <span style="color:#117700">/></span> <span style="color:#117700"></</span><span style="color:#117700">logger</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">root</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">level</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"debug"</span> <span style="color:#117700">/></span> <span style="color:#117700"><</span><span style="color:#117700">appender-ref</span> <span style="color:#0000cc">ref</span>=<span style="color:#aa1111">"STDOUT"</span> <span style="color:#117700">/></span> <span style="color:#117700"></</span><span style="color:#117700">root</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">log4j:configuration</span><span style="color:#117700">></span></span>
二.相关概念
1.Mapper接口
MyBatis中的mapper接口相当于以前的dao。但是区别在于,mapper仅仅是接口,我们不需要提供实现类。
Mapper接口的取名应该是和映射文件名保持一致
比如,某个实体类User,它的Mapper接口如下:
<span style="background-color:#f8f8f8"><span style="color:#770088">public</span> <span style="color:#770088">interface</span> <span style="color:#0000ff">UserMapper</span>{ <span style="color:#008855">int</span> <span style="color:#000000">insert</span>();}</span>
在Mapper接口中可以提供一些抽象方法,用于增删改查
2.ORM思想
ORM是指(Object Relationship Mapping)对象关系映射
其中
对象:Java的实体类对象
关系:关系型数据库
映射:二者之间的对应关系
体现
Java概念 | 数据库概念 |
---|---|
类 | 表 |
属性 | 字段/列 |
对象 | 记录/行 |
三.映射配置文件
1.文件结构
命名规则:数据库表对应的类名+Mapper.xml
一个映射文件对应一个实体类,对应一个表中的操作
映射文件主要用于编写SQL、访问以及操作表中的数据
映射文件存放位置是maven工程下的src/main/resources/mappers目录下
映射配置文件要保证两个一致
mapper接口的全类名和映射文件的命名空间namespace保持一致
mapper接口中方法的方法名和映射文件中编写SQL的标签的id属性保持一致
映射文件的简易结构如下
<span style="background-color:#f8f8f8"><span style="color:#555555"><?xml</span> <span style="color:#555555">version="1.0" encoding="UTF-8" ?></span><span style="color:#aa5500"><!--DTD约束--></span><span style="color:#555555"><!DOCTYPE mapper</span> <span style="color:#555555">PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"</span> <span style="color:#555555">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"></span><span style="color:#117700"><</span><span style="color:#117700">mapper</span> <span style="color:#0000cc">namespace</span>=<span style="color:#aa1111">"Mapper接口全类名"</span><span style="color:#117700">></span> <span style="color:#aa5500"><!--映射语句--></span> <span style="color:#117700"><</span><span style="color:#117700">insert</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"Mapper接口方法名"</span><span style="color:#117700">></span> SQL语句 <span style="color:#117700"></</span><span style="color:#117700">insert</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">mapper</span><span style="color:#117700">></span></span>
2.映射配置文件标签详解
<insert>标签
用于书写插入数据的SQL语句
id属性指定对应mapper接口的方法名
范例
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">insert</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"insertUser"</span><span style="color:#117700">></span> SQL语句<span style="color:#117700"></</span><span style="color:#117700">insert</span><span style="color:#117700">></span></span>
<delete>标签
用于删除表中的数据
id属性指定对应mapper接口的方法名
范例
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">delete</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"deleteUser"</span><span style="color:#117700">></span> SQL语句<span style="color:#117700"></</span><span style="color:#117700">delete</span><span style="color:#117700">></span></span>
<update>标签
用于更新表中数据
id属性指定对应mapper接口中的方法名
范例
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">update</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"updateUser"</span><span style="color:#117700">></span> SQL语句<span style="color:#117700"></</span><span style="color:#117700">update</span><span style="color:#117700">></span></span>
<select>标签
用于查询表中的数据
id属性指定mapper接口中对应的方法名
resultType属性表示自动映射,用于属性名和表中字段名一致的情况
resultMap属性表示自定义映射,用于一对多或多对一或字段名和属性名不一致的情况
范例
查询一条数据:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getUserById"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"com.lxq.pojo.User"</span><span style="color:#117700">></span> SQL语句<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
查询多条数据到List集合:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getUserList"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"com.lxq.pojo.User"</span><span style="color:#117700">></span> SQL语句<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
3.SQL语句中参数的获取
(1)获取方式
MyBatis获取参数值的两种方式:${}和#{}
${}的本质是字符串拼接,#{}的本质是占位符赋值
${}方式会将参数原原本本拼接入SQL语句中,如果参数是字符串类型的需要手动加上引号
<span style="background-color:#f8f8f8">`${参数}`</span>
#{}的方式会自动加上引号,如果是字符串类型的参数无需手动加上引号
<span style="background-color:#f8f8f8">#{参数}</span>
(2)参数类型
单个字面量类型的参数
字面量是指基本数据类型以及包装类还有自定义类等
获取方式
<span style="background-color:#f8f8f8"><select id="getUserByUsername" resultType="User"> select * from t_user where username=#{username}</select></span>
此时获取参数时花括号里边的参数名不做要求,可以是随意的名称,如果使用${}的方式记得加引号
多个字面量类型的参数
若mapper接口中的方法参数为多个时,MyBatis会自动将这些参数放在一个map集合中,此时参数名不能是随意的名称
这个map集合存放参数的形式是arg0,arg1...
为map中的键,参数值为map中的值;还有param1,param2...
为map中的键,参数值为map值的值
arg和param是同时存在于同一个map中的,在SQL语句获取参数时只需指定这些键的名称即可
获取方式
<span style="background-color:#f8f8f8"><span style="color:#aa5500"><!--arg的方式--></span><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"checkLogin"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"User"</span><span style="color:#117700">></span> select * from t_user where username = #{arg0} and password = #{arg1}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span><span style="color:#aa5500"><!--param的方式--></span><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"checkLogin"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"User"</span><span style="color:#117700">></span> select * from t_user where username = #{param1} and password = #{param2}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
如果使用${}的方式记得加引号
map集合类型的参数
为了在SQL语句中指定一些有意义的参数名,我们可以自己提供一个map集合,自定义一些键的名称即可
通过自定义键的名称,我们在SQL语句里就可以使用自定义的参数名了
比如,这个自定义的map集合可以是{("username","参数值"),("password","参数值")}
获取方式
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"checkLoginByMap"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"User"</span><span style="color:#117700">></span> select * from t_user where username = #{username} and password = #{password}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
如果使用${}的方式记得加引号
使用@Param注解标识的参数
如果每次使用多个参数时都要自定义map集合就太麻烦了,所以可以通过使用@Param注解在映射方法的形参中指定好参数名
比如,这个映射方法可以是
<span style="background-color:#f8f8f8"><span style="color:#000000">User</span> <span style="color:#0000ff">getUserByParam</span>(<span style="color:#555555">@Param</span>(<span style="color:#aa1111">"username"</span>) <span style="color:#008855">String</span> <span style="color:#000000">username</span>,<span style="color:#555555">@Param</span>(<span style="color:#aa1111">"password"</span>) <span style="color:#008855">String</span> <span style="color:#000000">password</span>);</span>
获取方式
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"checkLoginByParam"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"User"</span><span style="color:#117700">></span> select * from t_user where username = #{username} and password = #{password}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
使用这种注解就可以方便获取参数了,如果使用${}的方式记得加引号
实体类型的参数
如果映射方法的形参是一个实体类型时,可以通过访问实体类对象中的属性名获取属性值
比如,这个实体类可以是
<span style="background-color:#f8f8f8"><span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">User</span> { <span style="color:#770088">private</span> <span style="color:#008855">Integer</span> <span style="color:#000000">id</span>; <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">username</span>; <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">password</span>; <span style="color:#770088">private</span> <span style="color:#008855">Integer</span> <span style="color:#000000">age</span>; <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">gender</span>; <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">email</span>; <span style="color:#aa5500">//省略有参、无参构造方法以及toString()方法</span> <span style="color:#770088">public</span> <span style="color:#008855">Integer</span> <span style="color:#000000">getId</span>() { <span style="color:#770088">return</span> <span style="color:#000000">id</span>; } <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setId</span>(<span style="color:#008855">Integer</span> <span style="color:#000000">id</span>) { <span style="color:#770088">this</span>.<span style="color:#000000">id</span> <span style="color:#981a1a">=</span> <span style="color:#000000">id</span>; } <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">getUsername</span>() { <span style="color:#770088">return</span> <span style="color:#000000">username</span>; } <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setUsername</span>(<span style="color:#008855">String</span> <span style="color:#000000">username</span>) { <span style="color:#770088">this</span>.<span style="color:#000000">username</span> <span style="color:#981a1a">=</span> <span style="color:#000000">username</span>; } <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">getPassword</span>() { <span style="color:#770088">return</span> <span style="color:#000000">password</span>; } <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setPassword</span>(<span style="color:#008855">String</span> <span style="color:#000000">password</span>) { <span style="color:#770088">this</span>.<span style="color:#000000">password</span> <span style="color:#981a1a">=</span> <span style="color:#000000">password</span>; } <span style="color:#770088">public</span> <span style="color:#008855">Integer</span> <span style="color:#000000">getAge</span>() { <span style="color:#770088">return</span> <span style="color:#000000">age</span>; } <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setAge</span>(<span style="color:#008855">Integer</span> <span style="color:#000000">age</span>) { <span style="color:#770088">this</span>.<span style="color:#000000">age</span> <span style="color:#981a1a">=</span> <span style="color:#000000">age</span>; } <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">getGender</span>() { <span style="color:#770088">return</span> <span style="color:#000000">gender</span>; } <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setGender</span>(<span style="color:#008855">String</span> <span style="color:#000000">gender</span>) { <span style="color:#770088">this</span>.<span style="color:#000000">gender</span> <span style="color:#981a1a">=</span> <span style="color:#000000">gender</span>; } <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">getEmail</span>() { <span style="color:#770088">return</span> <span style="color:#000000">email</span>; } <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setEmail</span>(<span style="color:#008855">String</span> <span style="color:#000000">email</span>) { <span style="color:#770088">this</span>.<span style="color:#000000">email</span> <span style="color:#981a1a">=</span> <span style="color:#000000">email</span>; }}</span>
获取方式
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">insert</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"insertUser"</span><span style="color:#117700">></span> insert into t_user value(null,#{username},#{password},#{age},#{gender},#{email})<span style="color:#117700"></</span><span style="color:#117700">insert</span><span style="color:#117700">></span></span>
如果使用${}的方式记得加引号
总结
上面所说的五种参数类型实际上可以分成两种类型,一种是使用@Param注解一种是使用实体类
就是说不论单个参数或者多个参数,都用注解的方式,如果是实体类那就用实体类属性的方式
4.各种SQL操作
查询一个实体类对象
映射方法:
<span style="background-color:#f8f8f8"><span style="color:#000000">User</span> <span style="color:#0000ff">getUserById</span>(<span style="color:#555555">@Param</span>(<span style="color:#aa1111">"id"</span>) <span style="color:#008855">int</span> <span style="color:#000000">id</span>);</span>
映射文件:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getUserById"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"User"</span><span style="color:#117700">></span> select * from t_user where id = #{id}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
查询一个List集合
映射方法:
<span style="background-color:#f8f8f8"><span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#000000">User</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getAllUser</span>();</span>
映射文件:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getAllUser"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"User"</span><span style="color:#117700">></span> select * from t_user<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
注意:当查询的数据为多条时,不能使用实体类作为返回值,否则会抛出异常 TooManyResultsException;但是若查询的数据只有一条,可以使用实体类或集合作为返回值
查询单个数据
映射方法:
<span style="background-color:#f8f8f8"><span style="color:#008855">int</span> <span style="color:#0000ff">getCount</span>();</span>
映射文件:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getCount"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"java.lang.Integer"</span><span style="color:#117700">></span> select count(id) from t_user<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
查询一条数据到map集合
映射方法:
<span style="background-color:#f8f8f8"><span style="color:#000000">Map</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span>,<span style="color:#008855">Object</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getUserToMap</span>(<span style="color:#555555">@Param</span>(<span style="color:#aa1111">"id"</span>) <span style="color:#008855">int</span> <span style="color:#000000">id</span>);</span>
映射文件:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getUserToMap"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"java.util.Map"</span><span style="color:#117700">></span> select * from t_user where id = #{id}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
注意:将一条数据查询到map集合中时,map的键是表中的字段名,map的值是表中数据
查询多条数据到map集合
方式一
映射方法:
<span style="background-color:#f8f8f8"><span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#000000">Map</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span>,<span style="color:#008855">Object</span><span style="color:#981a1a">>></span> <span style="color:#0000ff">getAllUserToMap</span>();</span>
映射文件:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getAllUserToMap"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"java.util.Map"</span><span style="color:#117700">></span> select * from t_user<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
方式二
映射方法:
<span style="background-color:#f8f8f8"><span style="color:#555555">@MapKey</span>(<span style="color:#aa1111">"id"</span>)<span style="color:#000000">Map</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span>,<span style="color:#008855">Object</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getAllUserToMap</span>();</span>
映射文件:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getAllUserToMap"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"java.util.Map"</span><span style="color:#117700">></span> select * from t_user<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
注意
方式一中每条查出来的数据都对应一个Map集合,然后再利用List集合将这些Map集合组织起来
方式二中每条查出来的数据都存放在一个Map集合中,但是这个Map集合的键由映射方法上方的@MapKey注解指定,而Map集合的值又是另外一个Map集合,作为值的Map集合中键对应表中字段名,值对应表中数据
方式二范例
<span style="background-color:#f8f8f8">{1={password=123456, sex=男, id=1, age=23, username=admin},2={password=123456, sex=男, id=2, age=23, username=张三},3={password=123456, sex=男, id=3, age=23, username=张三}}</span>
模糊查询
映射方法:
<span style="background-color:#f8f8f8"><span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#000000">User</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getUserByLike</span>(<span style="color:#555555">@Param</span>(<span style="color:#aa1111">"mohu"</span>) <span style="color:#008855">String</span> <span style="color:#000000">mohu</span>);</span>
映射文件:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getUserByLike"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"User"</span><span style="color:#117700">></span> <span style="color:#aa5500"><!--方式1--></span> select * from t_user where username like '%${mohu}%' <span style="color:#aa5500"><!--方式2--></span> select * from t_user where username like concat("%",#{mohu},"%") <span style="color:#aa5500"><!--方式3--></span> select * from t_user where username like "%"#{mohu}"%"<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
注意:不能使用like '%#{mohu}%'
的方式,因为#{}会被解析成?,这个问号会被当成字符串的一部分造成参数获取失败
批量删除
映射方法:
<span style="background-color:#f8f8f8"><span style="color:#008855">void</span> <span style="color:#0000ff">deleteSomeUser</span>(<span style="color:#555555">@Param</span>(<span style="color:#aa1111">"ids"</span>) <span style="color:#008855">String</span> <span style="color:#000000">ids</span>);</span>
映射文件:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">delete</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"deleteSomeUser"</span><span style="color:#117700">></span> delete from t_user where id in(${ids})<span style="color:#117700"></</span><span style="color:#117700">delete</span><span style="color:#117700">></span></span>
注意:这里获取参数的方式是${},因为#{}会自动添加引号,如果使用#{}的方式会造成SQL语句解析成delete from t_user where id in('ids')
从而报错
动态设置表名
映射方法:
<span style="background-color:#f8f8f8"><span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#000000">User</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getUserList</span>(<span style="color:#555555">@Param</span>(<span style="color:#aa1111">"table"</span>) <span style="color:#008855">String</span> <span style="color:#000000">table</span>);</span>
映射文件:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getUserList"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"User"</span><span style="color:#117700">></span> select * from ${table}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
注意:这里使用${}是因为使用#{}时会自动添加引号,而表名不允许添加表名
执行添加功能时获取自增的主键
映射方法:
<span style="background-color:#f8f8f8"><span style="color:#008855">void</span> <span style="color:#0000ff">insertUser</span>(<span style="color:#000000">User</span> <span style="color:#000000">user</span>);</span>
映射文件:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">insert</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"insertUser"</span> <span style="color:#0000cc">useGeneratedKeys</span>=<span style="color:#aa1111">"true"</span> <span style="color:#0000cc">keyProperty</span>=<span style="color:#aa1111">"id"</span><span style="color:#117700">></span> insert into t_user values(null,#{username},#{password},#{age},#{gender},#{email})<span style="color:#117700"></</span><span style="color:#117700">insert</span><span style="color:#117700">></span></span>
测试方法:
<span style="background-color:#f8f8f8"><span style="color:#555555">@Test</span><span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#0000ff">testInsertUser</span>(){ <span style="color:#000000">SqlSession</span> <span style="color:#000000">sqlSession</span> <span style="color:#981a1a">=</span> <span style="color:#000000">SqlSessionUtil</span>.<span style="color:#000000">getSqlSession</span>(); <span style="color:#000000">SpecialSQLMapper</span> <span style="color:#000000">mapper</span> <span style="color:#981a1a">=</span> <span style="color:#000000">sqlSession</span>.<span style="color:#000000">getMapper</span>(<span style="color:#000000">SpecialSQLMapper</span>.<span style="color:#770088">class</span>); <span style="color:#000000">User</span> <span style="color:#000000">user</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">User</span>(<span style="color:#221199">null</span>,<span style="color:#aa1111">"李晨"</span>,<span style="color:#aa1111">"1234567"</span>,<span style="color:#116644">46</span>,<span style="color:#aa1111">"男"</span>,<span style="color:#aa1111">"lichen@qq.com"</span>); <span style="color:#000000">mapper</span>.<span style="color:#000000">insertUser</span>(<span style="color:#000000">user</span>); <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">user</span>);<span style="color:#aa5500">//在这一步中打印出的User对象中可以看到自增的id,如果配置文件中不使用useGeneratedKeys和keyProperty,则id仍然是null</span>}</span>
注意:这里的useGeneratedKeys设置使用自增主键为true,keyProperty是将获取的主键值赋给实体对象中的某个属性。这样,在添加这个实体对象后,自增的主键也能在实体对象中获得,而不需要进行查询
5.处理表字段和实体类属性名不一致的情况
方式一:给字段名取别名
如果表中字段名和实体类属性名不一致,可以在SQL语句中给字段名取别名
给字段取得别名必须和实体类属性名一致
方式二:在核心配置文件中配置驼峰映射
使用前提:表字段符合Mysql命名规范(使用下划线_分割单词),而实体类属性符合驼峰命名规范
使用方式:
在核心配置文件中使用<settings>标签,在该标签下使用<setting>子标签来配置
给子标签<setting>设置name属性值为mapUnderscoreToCamelCase,value属性值为true
范例
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">settings</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">setting</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"mapUnderscoreToCamelCase"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"true"</span> <span style="color:#117700">/></span><span style="color:#117700"></</span><span style="color:#117700">settings</span><span style="color:#117700">></span></span>
在核心配置文件使用了如上配置后,在SQL语句中可以使用表的字段名而不用考虑表字段名和实体类属性名不一致的情况
方式三:在映射配置文件中使用<resultMap>标签自定义映射
<resultMap>标签含有id属性和type属性,其中id属性是设置当前自定义映射的标识,type属性是映射的实体类
<resultMap>标签下含有的子标签以及功能
<id>标签:设置主键字段的映射关系,使用column属性设置映射关系中表的字段名,使用property属性设置映射关系中实体类的属性名
<result>标签:设置普通字段的映射关系,使用column属性设置映射关系中表的字段名,使用property属性设置映射关系中实体类的属性名
范例
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">resultMap</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"empResultMap"</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"Emp"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">id</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_id"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empId"</span><span style="color:#117700">></</span><span style="color:#117700">id</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_name"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empName"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"age"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"age"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"gender"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"gender"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">resultMap</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpByEmpId"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"Emp"</span> <span style="color:#0000cc">resultMap</span>=<span style="color:#aa1111">"empResultMap"</span><span style="color:#117700">></span> select * from t_emp where emp_id = #{empId}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
注意:SQL语句所在标签中的resultMap属性值必须是自定义映射的id
6.多对一映射关系的处理
这里多对一是指实体类中某个属性是以表中多个字段为属性构成的实体类,如员工类的部门属性,部门属性的类型是部门类,这个部门类有部门id,部门名称
方式一:使用级联
<resultMap>配置
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">resultMap</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpAndDeptByEmpIdResultMap"</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"Emp"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">id</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_id"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empId"</span><span style="color:#117700">></</span><span style="color:#117700">id</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_name"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empName"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"age"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"age"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"gender"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"gender"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_id"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"dept.deptId"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_name"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"dept.deptName"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">resultMap</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpAndDeptByEmpId"</span> <span style="color:#0000cc">resultMap</span>=<span style="color:#aa1111">"getEmpAndDeptByEmpIdResultMap"</span><span style="color:#117700">></span> select emp_id,emp_name,age,gender,t_dept.dept_id,dept_name from t_emp left join t_dept on t_emp.dept_id = t_dept.dept_id where emp_id = #{empId}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
方式二:使用<association>标签
<resultMap>配置
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">resultMap</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpAndDeptByEmpIdResultMap"</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"Emp"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">id</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_id"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empId"</span><span style="color:#117700">></</span><span style="color:#117700">id</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_name"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empName"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"age"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"age"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"gender"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"gender"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">association</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"dept"</span> <span style="color:#0000cc">javaType</span>=<span style="color:#aa1111">"Dept"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">id</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_id"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"deptId"</span><span style="color:#117700">></</span><span style="color:#117700">id</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_name"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"deptName"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">association</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">resultMap</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpAndDeptByEmpId"</span> <span style="color:#0000cc">resultMap</span>=<span style="color:#aa1111">"getEmpAndDeptByEmpIdResultMap"</span><span style="color:#117700">></span> select emp_id,emp_name,age,gender,t_dept.dept_id,dept_name from t_emp left join t_dept on t_emp.dept_id = t_dept.dept_id where emp_id = #{empId}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
注意:association标签中property属性是指映射实体类中属性的名称,javaType是它的类型,而association标签下的id标签和result标签中的property属性是指javaType指定的类中的属性名称,column属性指表中的字段名
方式三:使用分步查询
<resultMap>配置
查询员工信息:
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">resultMap</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpAndDeptByEmpIdResultMap"</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"Emp"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">id</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_id"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empId"</span><span style="color:#117700">></</span><span style="color:#117700">id</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_name"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empName"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"age"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"age"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"gender"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"gender"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">association</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"dept"</span> <span style="color:#0000cc">select</span>=<span style="color:#aa1111">"com.liaoxiangqian.mapper.DeptMapper.getDeptByDeptId"</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_id"</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">association</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">resultMap</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpAndDeptByEmpId"</span> <span style="color:#0000cc">resultMap</span>=<span style="color:#aa1111">"getEmpAndDeptByEmpIdResultMap"</span><span style="color:#117700">></span> select * from t_emp where emp_id = #{empId}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
根据员工的部门id查询部门信息
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">resultMap</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getDeptByDeptIdResultMap"</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"Dept"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">id</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_id"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"deptId"</span><span style="color:#117700">></</span><span style="color:#117700">id</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_name"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"deptName"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">resultMap</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getDeptByDeptId"</span> <span style="color:#0000cc">resultMap</span>=<span style="color:#aa1111">"getDeptByDeptIdResultMap"</span><span style="color:#117700">></span> select * from t_dept where dept_id = #{deptId}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
7.一对多映射关系的处理
这里一对多是指实体类中某个属性是由许多实体类构成的集合,如部门类中员工属性是一个List集合
方式一:使用<collection>标签
<resultMap>配置
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">resultMap</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getDeptAndEmpByDeptIdResultMap"</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"Dept"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">id</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_id"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"deptId"</span><span style="color:#117700">></</span><span style="color:#117700">id</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_name"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"deptName"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">collection</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"emps"</span> <span style="color:#0000cc">ofType</span>=<span style="color:#aa1111">"Emp"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">id</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_id"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empId"</span><span style="color:#117700">></</span><span style="color:#117700">id</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_name"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empName"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"age"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"age"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"gender"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"gender"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">collection</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">resultMap</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getDeptAndEmpByDeptId"</span> <span style="color:#0000cc">resultMap</span>=<span style="color:#aa1111">"getDeptAndEmpByDeptIdResultMap"</span><span style="color:#117700">></span> select * from t_dept left join t_emp on t_dept.dept_id = t_emp.dept_id where t_dept.dept_id = #{deptId}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
方式二:使用分步查询
<resultMap>配置
查询部门信息
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">resultMap</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getDeptAndEmpByDeptIdResultMap"</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"Dept"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">id</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_id"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"deptId"</span><span style="color:#117700">></</span><span style="color:#117700">id</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_name"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"deptName"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">collection</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"emps"</span> <span style="color:#0000cc">select</span>=<span style="color:#aa1111">"com.liaoxiangqian.mapper.EmpMapper.getEmpByDeptId"</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"dept_id"</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">collection</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">resultMap</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getDeptAndEmpByDeptId"</span> <span style="color:#0000cc">resultMap</span>=<span style="color:#aa1111">"getDeptAndEmpByDeptIdResultMap"</span><span style="color:#117700">></span> select * from t_dept where dept_id = #{deptId}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
根据部门id查询员工信息
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">resultMap</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpByDeptIdResultMap"</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"Emp"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">id</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_id"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empId"</span><span style="color:#117700">></</span><span style="color:#117700">id</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"emp_name"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"empName"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"age"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"age"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">result</span> <span style="color:#0000cc">column</span>=<span style="color:#aa1111">"gender"</span> <span style="color:#0000cc">property</span>=<span style="color:#aa1111">"gender"</span><span style="color:#117700">></</span><span style="color:#117700">result</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">resultMap</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpByDeptId"</span> <span style="color:#0000cc">resultMap</span>=<span style="color:#aa1111">"getEmpByDeptIdResultMap"</span><span style="color:#117700">></span> select * from t_emp where dept_id = #{deptId}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
8.分布查询的优点
分布查询的优点是可以实现延迟加载
延迟加载可以避免在分步查询中执行所有的SQL语句,节省资源,实现按需加载
需要在核心配置文件中添加如下的配置信息
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">settings</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">setting</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"lazyLoadingEnabled"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"true"</span><span style="color:#117700">/></span> <span style="color:#117700"><</span><span style="color:#117700">setting</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"aggressiveLazyLoading"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"false"</span><span style="color:#117700">/></span><span style="color:#117700"></</span><span style="color:#117700">settings</span><span style="color:#117700">></span></span>
lazyLoadingEnabled表示全局的延迟加载开关,true表示所有关联对象都会延迟加载,false表示关闭
aggressiveLazyLoading表示是否加载该对象的所有属性,如果开启则任何方法的调用会加载这个对象的所有属性,如果关闭则是按需加载
由于这个配置是在核心配置文件中设定的,所以所有的分步查询都会实现延迟加载,而如果某个查询不需要延迟加载,可以在collection标签或者association标签中的fetchType属性设置是否使用延迟加载,属性值lazy表示延迟加载,属性值eager表示立即加载
9.动态SQL
if标签
if标签通过test属性给出判断的条件,如果条件成立,则将执行标签内的SQL语句
范例
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpByCondition"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"Emp"</span><span style="color:#117700">></span> select * from t_emp where <span style="color:#117700"><</span><span style="color:#117700">if</span> <span style="color:#0000cc">test</span>=<span style="color:#aa1111">"empName != null and empName != ''"</span><span style="color:#117700">></span> emp_name = #{empName} <span style="color:#117700"></</span><span style="color:#117700">if</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">if</span> <span style="color:#0000cc">test</span>=<span style="color:#aa1111">"age != null and age != ''"</span><span style="color:#117700">></span> and age = #{age} <span style="color:#117700"></</span><span style="color:#117700">if</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">if</span> <span style="color:#0000cc">test</span>=<span style="color:#aa1111">"gender != null and gender != ''"</span><span style="color:#117700">></span> and gender = #{gender} <span style="color:#117700"></</span><span style="color:#117700">if</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
where标签
考虑if标签中的范例出现的一种情况:当第一个if标签条件不成立而第二个条件成立时,拼接成的SQL语句中where后面连着的是and,会造成SQL语句语法错误,而where标签可以解决这个问题
范例
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpByCondition"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"Emp"</span><span style="color:#117700">></span> select * from t_emp <span style="color:#117700"><</span><span style="color:#117700">where</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">if</span> <span style="color:#0000cc">test</span>=<span style="color:#aa1111">"empName != null and empName != ''"</span><span style="color:#117700">></span> emp_name = #{empName} <span style="color:#117700"></</span><span style="color:#117700">if</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">if</span> <span style="color:#0000cc">test</span>=<span style="color:#aa1111">"age != null and age != ''"</span><span style="color:#117700">></span> and age = #{age} <span style="color:#117700"></</span><span style="color:#117700">if</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">if</span> <span style="color:#0000cc">test</span>=<span style="color:#aa1111">"gender != null and gender != ''"</span><span style="color:#117700">></span> and gender = #{gender} <span style="color:#117700"></</span><span style="color:#117700">if</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">where</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
where标签只会在子标签返回任何内容的情况下才插入WHERE子句。而且,若子句的开头有多余的and或者or,where标签也会将它们去除,但是子句末尾的and或者or不能去除
trim标签
trim标签用于去掉或添加标签中的内容
trim标签常用属性
prefix:在trim标签中的内容的前面添加某些内容
prefixOverrides:在trim标签中的内容的前面去掉某些内容
suffix:在trim标签中的内容的后面添加某些内容
suffixOverrides:在trim标签中的内容的后面去掉某些内容
用trim实现where标签范例相同的功能
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpByCondition"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"Emp"</span><span style="color:#117700">></span> select * from t_emp <span style="color:#117700"><</span><span style="color:#117700">trim</span> <span style="color:#0000cc">prefix</span>=<span style="color:#aa1111">"where"</span> <span style="color:#0000cc">prefixOverrides</span>=<span style="color:#aa1111">"and"</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">if</span> <span style="color:#0000cc">test</span>=<span style="color:#aa1111">"empName != null and empName != ''"</span><span style="color:#117700">></span> emp_name = #{empName} <span style="color:#117700"></</span><span style="color:#117700">if</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">if</span> <span style="color:#0000cc">test</span>=<span style="color:#aa1111">"age != null and age != ''"</span><span style="color:#117700">></span> and age = #{age} <span style="color:#117700"></</span><span style="color:#117700">if</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">if</span> <span style="color:#0000cc">test</span>=<span style="color:#aa1111">"gender != null and gender != ''"</span><span style="color:#117700">></span> and gender = #{gender} <span style="color:#117700"></</span><span style="color:#117700">if</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">trim</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
choose、when、otherwise标签
这三个标签是组合使用的,用于在多条件中选择一个条件,类似Java中的if...else if...else...语句
范例
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpByCondition"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"Emp"</span><span style="color:#117700">></span> select * from t_emp where gender = #{gender} <span style="color:#117700"><</span><span style="color:#117700">choose</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">when</span> <span style="color:#0000cc">test</span>=<span style="color:#aa1111">"empName != null and empName != ''"</span><span style="color:#117700">></span> and emp_name = #{empName} <span style="color:#117700"></</span><span style="color:#117700">when</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">when</span> <span style="color:#0000cc">test</span>=<span style="color:#aa1111">"age != null and age != ''"</span><span style="color:#117700">></span> and age = #{age} <span style="color:#117700"></</span><span style="color:#117700">when</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">choose</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
当某个when标签的条件满足时将对应的SQL语句返回,如果都不满足并且有otherwise标签时,才会返回otherwise标签中的SQL语句
foreach标签
foreach标签允许指定一个集合或数组,并且对这个集合或数组进行遍历
foreach标签可以用的属性有
collection:指定需要遍历的集合或数组
item:当前遍历到的元素
index:当前遍历到的元素的序号
当遍历的集合是Map类型时,index表示键,item表示值
open:指定遍历开始前添加的字符串
close:指定遍历开始后添加的字符串
separator:指定每次遍历之间的分隔符
collection属性值注意事项
如果遍历的是List时,属性值为list
如果遍历的是数组时,属性值为array
如果遍历的是Map时,属性值可以是map.keys()、map.values()、map.entrySet()
除此之外,还可以在映射方法的参数中使用@Param()注解自定义collection属性值
批量添加数据
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">insert</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"addMoreEmp"</span><span style="color:#117700">></span> insert into t_emp values <span style="color:#117700"><</span><span style="color:#117700">foreach</span> <span style="color:#0000cc">collection</span>=<span style="color:#aa1111">"list"</span> <span style="color:#0000cc">separator</span>=<span style="color:#aa1111">","</span> <span style="color:#0000cc">item</span>=<span style="color:#aa1111">"emp"</span><span style="color:#117700">></span> (null,#{emp.empName},#{emp.age},#{emp.gender},null) <span style="color:#117700"></</span><span style="color:#117700">foreach</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">insert</span><span style="color:#117700">></span></span>
批量删除数据
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">delete</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"deleteMoreEmp"</span><span style="color:#117700">></span> delete from t_emp where emp_id in <span style="color:#117700"><</span><span style="color:#117700">foreach</span> <span style="color:#0000cc">collection</span>=<span style="color:#aa1111">"array"</span> <span style="color:#0000cc">item</span>=<span style="color:#aa1111">"empId"</span> <span style="color:#0000cc">separator</span>=<span style="color:#aa1111">","</span> <span style="color:#0000cc">open</span>=<span style="color:#aa1111">"("</span> <span style="color:#0000cc">close</span>=<span style="color:#aa1111">")"</span><span style="color:#117700">></span> #{empId} <span style="color:#117700"></</span><span style="color:#117700">foreach</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">delete</span><span style="color:#117700">></span></span>
sql标签
用于记录一段通用的SQL语句片段,在需要用到该SQL语句片段的地方中通过include标签将该SQL语句片段插入
sql标签通过id属性唯一标识一个SQL语句片段,include标签通过refid属性指定使用某个SQL片段
范例
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">sql</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"item"</span><span style="color:#117700">></span> emp_id,emp_name,age,gender,dept_id<span style="color:#117700"></</span><span style="color:#117700">sql</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">select</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"getEmpByEmpId"</span> <span style="color:#0000cc">resultType</span>=<span style="color:#aa1111">"Emp"</span><span style="color:#117700">></span> select <span style="color:#117700"><</span><span style="color:#117700">include</span> <span style="color:#0000cc">refid</span>=<span style="color:#aa1111">"item"</span><span style="color:#117700">></</span><span style="color:#117700">include</span><span style="color:#117700">></span> from t_emp where emp_id = #{empId}<span style="color:#117700"></</span><span style="color:#117700">select</span><span style="color:#117700">></span></span>
四.核心配置文件
1.文件结构
核心配置文件命名建议是mybatis-config.xml,无强制要求
核心配置文件主要用于配置连接数据库的环境以及MyBatis的全局配置信息
核心配置文件存放的位置是maven工程下的src/main/resources目录下
简易结构如下,核心配置文件的标签不止这几个
<span style="background-color:#f8f8f8"><span style="color:#555555"><?xml</span> <span style="color:#555555">version="1.0" encoding="UTF-8" ?></span><span style="color:#aa5500"><!--DTD约束--></span><span style="color:#555555"><!DOCTYPE configuration</span> <span style="color:#555555">PUBLIC "-//mybatis.org//DTD Config 3.0//EN"</span> <span style="color:#555555">"http://mybatis.org/dtd/mybatis-3-config.dtd"></span><span style="color:#117700"><</span><span style="color:#117700">configuration</span><span style="color:#117700">></span> <span style="color:#aa5500"><!--</span> <span style="color:#aa5500">1.environments配置数据库的环境,环境可以有多个</span> <span style="color:#aa5500">2.default属性指定使用的环境</span> <span style="color:#aa5500">--></span> <span style="color:#117700"><</span><span style="color:#117700">environments</span> <span style="color:#0000cc">default</span>=<span style="color:#aa1111">"development"</span><span style="color:#117700">></span> <span style="color:#aa5500"><!--</span> <span style="color:#aa5500">1.environment配置具体某个数据库的环境</span> <span style="color:#aa5500">2.id属性唯一标识这个环境</span> <span style="color:#aa5500">--></span> <span style="color:#117700"><</span><span style="color:#117700">environment</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"development"</span><span style="color:#117700">></span> <span style="color:#aa5500"><!--</span> <span style="color:#aa5500">1.transactionManager设置事务管理方式</span> <span style="color:#aa5500">2.type属性取值有“JDBC|MANAGED”</span> <span style="color:#aa5500">3.JDBC指当前环境中使用的是JDBC中原生的事务管理方式,事务的提交或回滚需要手动处理</span> <span style="color:#aa5500">4.MANAGED指被管理,例如Spring中</span> <span style="color:#aa5500">--></span> <span style="color:#117700"><</span><span style="color:#117700">transactionManager</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"JDBC"</span><span style="color:#117700">/></span> <span style="color:#aa5500"><!--</span> <span style="color:#aa5500">1.dataSource配置数据源</span> <span style="color:#aa5500">2.取值有"POOLED|UNPOOLED|JNDI"</span> <span style="color:#aa5500">3.POOLED表示使用数据库连接池缓存数据库连接</span> <span style="color:#aa5500">4.UNPOOLED:表示不使用数据库连接池</span> <span style="color:#aa5500">5.JNDI表示使用上下文中的数据源</span> <span style="color:#aa5500">--></span> <span style="color:#117700"><</span><span style="color:#117700">dataSource</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"POOLED"</span><span style="color:#117700">></span> <span style="color:#aa5500"><!--设置链接数据库的驱动--></span> <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"driver"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"com.mysql.jdbc.Driver"</span><span style="color:#117700">/></span> <span style="color:#aa5500"><!--设置连接数据库的地址--></span> <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"url"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"jdbc:mysql://localhost:3306/ssm"</span><span style="color:#117700">/></span> <span style="color:#aa5500"><!--设置连接数据库的用户名--></span> <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"username"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"root"</span><span style="color:#117700">/></span> <span style="color:#aa5500"><!--设置连接数据库的密码--></span> <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"password"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"lxq"</span><span style="color:#117700">/></span> <span style="color:#117700"></</span><span style="color:#117700">dataSource</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">environment</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">environments</span><span style="color:#117700">></span> <span style="color:#aa5500"><!--mappers用于引入映射的配置文件--></span> <span style="color:#117700"><</span><span style="color:#117700">mappers</span><span style="color:#117700">></span> <span style="color:#aa5500"><!--mapper用于指定某个映射文件,resource属性指定文件路径--></span> <span style="color:#117700"><</span><span style="color:#117700">mapper</span> <span style="color:#0000cc">resource</span>=<span style="color:#aa1111">"mappers/UserMapper.xml"</span><span style="color:#117700">/></span> <span style="color:#117700"></</span><span style="color:#117700">mappers</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">configuration</span><span style="color:#117700">></span></span>
2.核心配置文件详解
(1)标签顺序
核心配置文件中configuration标签下的子标签要按照一定的顺序书写
properties => settings => typeAliases => typeHandlers => objectFactory => objectWrapperFactory => reflectorFactory => plugins => environments => databaseIdProvider => mappers
(2)标签详解
<properties>标签
用于引入某个properties配置文件,是一个单标签
resource属性指定配置文件
范例
<span style="background-color:#f8f8f8"><properties resource="jdbc.properties" /></span>
<typeAliases>标签
用于为某个类的全类名设置别名,子标签是<typeAlias>
一个子标签对应设置一个类的别名
子标签下有type和alias两个属性,type指定需要设置别名的类的全类名,alias指定别名
如果只设置了type属性,那么默认的别名就是它的类名(不是全类名)而且不区分大小写
如果想要设置某个包下所有类的别名,可以使用<package>标签,用name属性指定包名
范例
<span style="background-color:#f8f8f8"><typeAliases> <typeAlias type="com.lxq.pojo.User" alias="User"></typeAlias> <package name="com.lxq.pojo"></package></typeAliases></span>
MyBatis中内建了一些类型的别名,常见的有
Java类型 | 别名 |
---|---|
int | _int或_integer |
Integer | int或integer |
String | string |
List | list |
Map | map |
<property>标签
用于配置连接数据库时用到的各种属性,是一个单标签
该标签有两个属性,一个是name指定属性名,另一个是value指定属性值
如果不使用<properties>标签引入相关配置文件时,使用方式如下
<span style="background-color:#f8f8f8"><property name="driver" value="com.mysql.jdbc.Driver" /></span>
如果使用<properties>标签引入相关的配置文件时,value属性可以写成如下形式
<span style="background-color:#f8f8f8"><property name="driver" value="${jdbc.driver}" /></span>
其中配置文件的内容是
<span style="background-color:#f8f8f8">jdbc.driver=com.mysql.jdbc.Driver</span>
注意:这里使用jdbc.driver来给键命名是因为核心配置文件中可能会引入其他的配置文件,如果使用driver来命名键的话有可能会跟其他配置文件中的键同名而产生冲突
<mappers>标签
该标签用于引入映射文件
每个映射文件使用子标签<mapper>来表示,该子标签是一个单标签
子标签<mapper>使用属性resource来指定需要引入的映射文件
如果想要将某个包下所有的映射文件都引入,可以使用<package>标签,使用name属性来指定需要引入的包
范例
<span style="background-color:#f8f8f8"><mappers> <mapper resource="mappers/UserMapper.xml" /> <package name="com.lxq.mapper" /></mappers></span>
注意:使用包的形式引入映射文件需要满足两个条件,1.mapper接口所在的包和映射文件所在的包要一致;2.mapper接口名和映射文件名要相同
五.相关API
1.Resources
Resources类由MyBatis提供用于获取来自核心配置文件的输入流
相关方法是:InputStream getResourceAsStream(String filepath)
,注意这是一个静态方法
2.SqlSessionFactoryBuilder
SqlSessionFactoryBuilder类由MyBatis提供用于获取SqlSessionFactory的实例对象
相关方法是:SqlSessionFactory build(InputStream is)
,该方法通过一个输入流返回了SqlSessionFactory对象
3.SqlSessionFactory
SqlSessionFactory类由MyBatis提供用于获取SqlSession对象,每个基于MyBatis的应用都是以一个SqlSessionFactory的实例为核心的
相关方法:SqlSession openSession()
和SqlSession openSession(boolean autoCommit)
,这两个方法都用于获取SqlSession对象,如果使用有参数的可以指定是否自动提交事务,没有指定参数的默认是不自动提交事务
4.SqlSession
SqlSession类由MyBatis提供用于执行SQL、管理事务、接口代理
常用方法
方法 | 说明 |
---|---|
void commit() | 提交事务 |
void rollback() | 回滚事务 |
T getMapper(Class<T> aClass) | 获取指定接口的代理实现类 |
void close() | 释放资源 |
除了以上常用方法外,SqlSession还有很多有关数据库增删改查的方法,但是这些方法繁琐而且不符合类型安全,所以使用getMapper()方法来获取一个Mapper接口的代理实现类来执行映射语句是个比较方便的做法
5.最佳实践
SqlSessionFactoryBuilder
这个类可以被实例化、使用和丢弃,一旦创建了 SqlSessionFactory,就不再需要它了。因此 SqlSessionFactoryBuilder 实例的最佳范围是方法范围(也就是局部方法变量)。你可以重用 SqlSessionFactoryBuilder 来创建多个 SqlSessionFactory 实例,但是最好还是不要让其一直存在以保证所有的 XML 解析资源开放给更重要的事情
SqlSessionFactory
SqlSessionFactory 一旦被创建就应该在应用的运行期间一直存在,没有任何理由对它进行清除或重建。使用 SqlSessionFactory 的最佳实践是在应用运行期间不要重复创建多次,多次重建 SqlSessionFactory 被视为一种代码“坏味道(bad smell)”。因此 SqlSessionFactory 的最佳范围是应用范围。有很多方法可以做到,最简单的就是使用单例模式或者静态单例模式
SqlSession
每个线程都应该有它自己的 SqlSession 实例。SqlSession 的实例不是线程安全的,因此是不能被共享的,所以它的最佳的范围是请求或方法范围。绝对不能将 SqlSession 实例的引用放在一个类的静态域,甚至一个类的实例变量也不行。也绝不能将 SqlSession 实例的引用放在任何类型的管理范围中,比如 Serlvet 架构中的 HttpSession。如果你现在正在使用一种 Web 框架,要考虑 SqlSession 放在一个和 HTTP 请求对象相似的范围中。换句话说,每次收到的 HTTP 请求,就可以打开一个 SqlSession,返回一个响应,就关闭它。这个关闭操作是很重要的,你应该把这个关闭操作放到 finally 块中以确保每次都能执行关闭
工具类
<span style="background-color:#f8f8f8">public class SqlSessionUtil { private static SqlSessionFactory sqlSessionFactory; static{ try { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); sqlSessionFactory = sqlSessionFactoryBuilder.build(is); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(true); }}</span>
六.缓存
1.一级缓存
一级缓存是SqlSession级别的,通过同一个SqlSession查询的数据会被缓存,下次查询相同的数据,就会从缓存中直接获取,不会从数据库重新访问,一级缓存是默认开启的
一级缓存失效的四种情况:
使用另一个SqlSession
同一个SqlSession但是查询条件不同
同一个SqlSession但是两次查询中间执行了任何一次增删改操作
同一个SqlSession但是两次查询中间手动清空了缓存,手动清空缓存的方法是调用SqlSession的clearCache()
方法
2.二级缓存
二级缓存是SqlSessionFactory级别,通过同一个SqlSessionFactory创建的SqlSession查询的结果会被缓存,此后若再次执行相同的查询语句,结果就会从缓存中获取
二级缓存开启的条件:
在核心配置文件中,设置全局配置属性cacheEnabled="true",默认为true,不需要设置
在映射文件中设置标签<cache/>
二级缓存必须在SqlSession关闭或提交之后有效
查询的数据所转换的实体类类型必须实现序列化的接口
二级缓存失效的情况:两次查询之间执行了任意的增删改,会使一级和二级缓存同时失效
<cache/>可以设置的一些属性:
eviction属性:缓存回收策略,默认的是 LRU
LRU(Least Recently Used) – 最近最少使用的:移除最长时间不被使用的对象
FIFO(First in First out) – 先进先出:按对象进入缓存的顺序来移除它们
SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象
WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象
flushInterval属性:刷新间隔,单位是毫秒,默认情况下不设置也就是没有刷新间隔,缓存仅仅调用语句时刷新
size属性:引用数目,正整数代表缓存最多可以存储多少个对象,太大容易导致内存溢出
readOnly属性:只读, 取值是true/false
true:只读缓存,会给所有调用者返回缓存对象的相同实例,因此这些对象不能被修改,这提供了很重要的性能优势
false:读写缓存,会返回缓存对象的拷贝(通过序列化),这会慢一些,但是安全,因此默认是false
3.缓存的查询顺序
先查询二级缓存,因为二级缓存中可能会有其他程序已经查出来的数据,可以拿来直接使用
如果二级缓存没有命中,再查询一级缓存
如果一级缓存也没有命中,则查询数据库
注意SqlSession关闭之后,一级缓存中的数据才会写入二级缓存