- cache – 对给定命名空间的缓存配置。
- cache-ref – 对其他命名空间缓存配置的引用。
- resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象。
parameterMap– 已被废弃!老式风格的参数映射。更好的办法是使用内联参数,此元素可能在将来被移除。文档中不会介绍此元素。- sql – 可被其他语句引用的可重用语句块。
- insert – 映射插入语句
- update – 映射更新语句
- delete – 映射删除语句
- select – 映射查询语句
1.select
属性 | 描述 |
---|---|
id | 在命名空间中唯一的标识符,可以被用来引用这条语句。 |
parameterType | 将会传入这条语句的参数类的完全限定名或别名。这个属性是可选的,因为 MyBatis 可以通过类型处理器(TypeHandler) 推断出具体传入语句的参数,默认值为未设置(unset)。 |
resultType | 从这条语句中返回的期望类型的类的完全限定名或别名。 注意如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身。可以使用 resultType 或 resultMap,但不能同时使用。 |
resultMap | 外部 resultMap 的命名引用。结果集的映射是 MyBatis 最强大的特性,如果你对其理解透彻,许多复杂映射的情形都能迎刃而解。可以使用 resultMap 或 resultType,但不能同时使用。 |
flushCache | 将其设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认值:false。 |
useCache | 将其设置为 true 后,将会导致本条语句的结果被二级缓存缓存起来,默认值:对 select 元素为 true。 |
timeout | 这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为未设置(unset)(依赖驱动)。 |
fetchSize | 这是一个给驱动的提示,尝试让驱动程序每次批量返回的结果行数和这个设置值相等。 默认值为未设置(unset)(依赖驱动)。 |
statementType | STATEMENT,PREPARED 或 CALLABLE 中的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。 |
resultSetType | FORWARD_ONLY,SCROLL_SENSITIVE, SCROLL_INSENSITIVE 或 DEFAULT(等价于 unset) 中的一个,默认值为 unset (依赖驱动)。 |
databaseId | 如果配置了数据库厂商标识(databaseIdProvider),MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;如果带或者不带的语句都有,则不带的会被忽略。 |
resultOrdered | 这个设置仅针对嵌套结果 select 语句适用:如果为 true,就是假设包含了嵌套结果集或是分组,这样的话当返回一个主结果行的时候,就不会发生有对前面结果集的引用的情况。 这就使得在获取嵌套的结果集的时候不至于导致内存不够用。默认值:false。 |
resultSets | 这个设置仅对多结果集的情况适用。它将列出语句执行后返回的结果集并给每个结果集一个名称,名称是逗号分隔的。 |
2.insert, update 和 delete
属性 | 描述 |
---|---|
id | 命名空间中的唯一标识符,可被用来代表这条语句。 |
parameterType | 将要传入语句的参数的完全限定类名或别名。这个属性是可选的,因为 MyBatis 可以通过类型处理器推断出具体传入语句的参数,默认值为未设置(unset)。 |
flushCache | 将其设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认值:true(对于 insert、update 和 delete 语句)。 |
timeout | 这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为未设置(unset)(依赖驱动)。 |
statementType | STATEMENT,PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。 |
useGeneratedKeys | (仅对 insert 和 update 有用)这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键(比如:像 MySQL 和 SQL Server 这样的关系数据库管理系统的自动递增字段),默认值:false。 |
keyProperty | (仅对 insert 和 update 有用)唯一标记一个属性,MyBatis 会通过 getGeneratedKeys 的返回值或者通过 insert 语句的 selectKey 子元素设置它的键值,默认值:未设置(unset)。如果希望得到多个生成的列,也可以是逗号分隔的属性名称列表。 |
keyColumn | (仅对 insert 和 update 有用)通过生成的键值设置表中的列名,这个设置仅在某些数据库(像 PostgreSQL)是必须的,当主键列不是表中的第一列的时候需要设置。如果希望使用多个生成的列,也可以设置为逗号分隔的属性名称列表。 |
databaseId | 如果配置了数据库厂商标识(databaseIdProvider),MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;如果带或者不带的语句都有,则不带的会被忽略。 |
2.1 Author 传入数组或集合时使用
如果你的数据库支持自动生成主键的字段(比如 MySQL 和 SQL Server),那么你可以设置 useGeneratedKeys=”true”,然后再把 keyProperty 设置到目标属性上就 OK 了。
如果你的数据库还支持多行插入, 你也可以传入一个 Author 数组或集合,并返回自动生成的主键。
<!-- databaseId用什么类型的数据库,parameterType返回参数 -->
<insert id="testAdd" databaseId="oracle" parameterType="entity.User">
<!-- keyProperty返回类型,order在插入之前先运行selectKey标签,selectKey标签数据返回的类型resultType -->
<selectKey keyProperty="userId" order="BEFORE" resultType="Integer">
select t_user.nextval from dual
</selectKey>
insert into user(user_id, username, age)
values(#{userId}, #{userName}, #{age})
<!-- #{userId}这个参数是selectKey标签返回的 -->
</insert>
<!-- 可废弃 -->
<insert id="insertAuthor" useGeneratedKeys="true"
keyProperty="id">
insert into Author (username, password, email, bio) values
<foreach item="item" collection="list" separator=",">
(#{item.username}, #{item.password}, #{item.email}, #{item.bio})
</foreach>
</insert>
2.2 selectKey的使用,selectKey 元素中的语句将会在insert, update 和 delete之前运行,但select语句中无法使用
3.字符串替换
传入字符串时数据没有转义,使用#{}会发生错误,这是${} 。这样MyBatis 不会修改或转义字符串。
${} 会被直接替换,而 #{} 会被使用 ? 预处理
备注:LEFT JOIN语句配合association、collection使用,可以快速生成JSON树形数据格式
4.resultMap 复杂的查询*
association单个对象
4.1映射文件与类的关联-最初版 级联属性封装(连表关联) 关键在于 orgsss.orgName中
//User表
private String userId;
private String username;
private Org orgsss;//关键点
//Org表
private String orgId;
private String orgName;
<resultMap id="userName" type="com.litchon.lottery.pojo.UserName" >
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="org_id" property="orgsss.orgId" jdbcType="VARCHAR" /><!-- orgsss类中定义的Org对象名 -->
<result column="org_name" property="orgsss.orgName" jdbcType="VARCHAR" />
</resultMap>
SQL连表 select * from t_user u INNER JOIN t_org o IN u.org_id = o.org_id where u.id = #{id}
4.2映射文件与类的关联-加强版 使用association定义单个对象封装规则(嵌套)SQL语句必须写全
<resultMap id="userName" type="com.litchon.lottery.pojo.UserName" >
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<!-- property封装的名字,javaType类型名 -->
<association property="orgsss" javaType="com.litchon.lottery.pojo.Org">
<id column="org_id" property="orgId" jdbcType="VARCHAR" />
<result column="org_name" property="orgName" jdbcType="VARCHAR" />
</association>
</resultMap>
SQL连表 select * from t_user u INNER JOIN t_org o IN u.org_id = o.org_id where u.id = #{id}
结果 {userId: , username: , orgsss:{只是个对象}}
4.3 映射文件与映射文件的关联 association
<resultMap id="userName" type="com.litchon.lottery.pojo.UserName" >
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<!-- property封装的名字,select的值为另一个映射文件的路径+方法(Org的mapper),column为传给select的值 -->
<association property="orgsss" select="com.litchon.lottery.mapper.OrgMapper.getOrg" column="user_id">
</association>
</resultMap>
SQL独表(查两次) 第一次查select * from t_user u where u.id = #{id}
第二次查select * from t_org o where o.id = #{u.id}
结果 {userId: , username: , orgsss:{只是个对象}}
4.4 association的延迟加载配置(只需要配置config.xml就可以了)(参考4.3)
配置默认为true,但是我们为了安全自己再定义一次,因为上一个版本默认值可能为false
当开启时,任何方法的调用都会加载该对象的所有属性。 否则,每个属性会按需加载(参考 lazyLoadTriggerMethods)。
<settings>
<setting name="lazyLoadingEnabled" value="true "/><!-- 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。 特定关联关系中可通过设置 fetchType 属性来覆盖该项的开关状态。 -->
<setting name="aggressiveLazyLoading" value="false "/><!-- 为true时调用任何参数都会获取所有数据,为false时只会加载需要的数据 -->
</settings>
collection集合(集合中可以有多个对象)
4.5 collection 关联集合查询(ofType对象类)
<!-- collection定义集合类型规则
property="orgsss" 文件返回名
ofType 指定集合里面的元素类型(对象类)
-->
<resultMap id="userName" type="com.litchon.lottery.pojo.UserName" >
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="org_id" property="orgId" jdbcType="VARCHAR" />
<collection property="orgsss" ofType="com.litchon.lottery.pojo.Org">
<id column="org_id" property="orgId" />
<result column="org_name" property="orgName" />
</collection>
</resultMap>
SQL连表 select * from t_user u INNER JOIN t_org o IN u.org_id = o.org_id where u.id = #{id}
结果 {userId: , username: , orgsss:[{对象},{对象}]}
4.6 collection 分步查询()
<!-- collection定义集合类型规则
property="orgsss" 文件返回名
select 定义接收的接口类型 getOrg是一个方法名
column="org_id" 自定义条件参数
-->
<resultMap id="userName" type="com.litchon.lottery.pojo.UserName" >
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="org_id" property="orgId" jdbcType="VARCHAR" />
<collection property="orgsss" select="com.litchon.lottery.mapper.OrgMapper.getOrg" column="org_id">
<id column="org_id" property="orgId" />
<result column="org_name" property="orgName" />
</collection>
</resultMap>
SQL独表(查两次) 第一次查select * from t_user u where u.id = #{id}
第二次查select * from t_org o where o.id = #{u.id}
结果 {userId: , username: , orgsss:[{对象},{对象}]}
可以参考:https://blog.youkuaiyun.com/qq_16946803/article/details/90266442
4.7 collection多参数传递
<collection property="orgsss" select="com.litchon.lottery.mapper.OrgMapper.getOrg" column="{uorgId=orgId, uOrg= orgName}"><!-- 多参数传递 -->
</collection>
collection标签中的fetchType延迟加载(结合4.4 , 即使配置文件中设置了延迟加载,collection中的fetchType也配置的话优先级更高),当然association标签也有fetchType参数
fetchType的参数
eager : 立即
lazy : 延迟
4.8 discriminator鉴别器
<!--
discriminator鉴别器标签
javaType="string" 鉴别内容的java类型
column="org_id" 鉴别的条件
<case value="10001" 鉴别条件为10001时执行,相当于if语句
resultType="com.litchon.lottery.pojo.UserName"> 返回的类型,一般为resultMap的type类型
-->
<discriminator javaType="string" column="org_id" >
<case value="10001" resultType="com.litchon.lottery.pojo.UserName">
<association property="orgsss" select="com.litchon.lottery.mapper.OrgMapper.getOrg" column="user_id">
<!--虽然执行了association语句,但是discriminator外部的id、result标签都有运行-->
</association>
</case>
<case value="10002" resultType="com.litchon.lottery.pojo.UserName">
<!--修改表的内容-->
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_id" property="userName" jdbcType="VARCHAR" /><!--把返回参数user_id赋值给了userName-->
</case>
</discriminator>