MyBatis 配置的一些小知识点
MyBatis别名配置——typeAliases
类型别名是为 Java 类型设置一个短的名字。它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余。说白了就是预先设置包名
api是这样写的
<typeAliases> <typeAlias alias="Author" type="domain.blog.Author"/> <typeAlias alias="Blog" type="domain.blog.Blog"/> </typeAliases>
也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean,比如:
<typeAliases> <package name="domain.blog"/> </typeAliases>
这个直接在xml中的<configuration>下设置就可以,再使用的时候就可以直接使用别名了,不过要注意的时候写在xml下的时候有顺序关系的 哦!碰到错误自己解决,调整位置就好
已经为许多常见的 Java 类型内建了相应的类型别名。它们都是大小写不敏感的,需要注意的是由基本类型名称重复导致的特殊处理。
别名 | 映射的类型 |
---|---|
_byte | byte |
_long | long |
_short | short |
_int | int |
_integer | int |
_double | double |
_float | float |
_boolean | boolean |
string | String |
byte | Byte |
long | Long |
short | Short |
int | Integer |
integer | Integer |
double | Double |
float | Float |
boolean | Boolean |
date | Date |
decimal | BigDecimal |
bigdecimal | BigDecimal |
object | Object |
map | Map |
hashmap | HashMap |
list | List |
arraylist | ArrayList |
collection | Collection |
iterator | Iterator |
映射器——mappers
既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要定义 SQL 映射语句了。但是首先我们需要告诉 MyBatis 到哪里去找到这些语句。 Java 在自动查找这方面没有提供一个很好的方法,所以最佳的方式是告诉 MyBatis 到哪里去找映射文件。你可以使用相对于类路径的资源引用, 或完全限定资源定位符(包括 file:/// 的 URL),或类名和包名等。
这里有四种方法
第一种类路径的资源引用 <mapper resource="com/entity/UserMapper.xml"/> 第二种直接给出本地地址形式的,自己存放的地址,完全限定资源定位符 <mapper url="file:///var/mappers/UserMapper.xml"/> 第三种以类的形式去寻找 <mapper class="com.entity.AuthorMapper"/> 第四种包名的形式 <package name="com.entity.builder"/>
映射配置细节
resultMap和resultType
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,
resultType是直接表示返回类型的,查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。当提供的返回类型属性是resultType的时候,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性。
resultMap则是对外部ResultMap的引用,然后基于查找出来的属性名进行键值对封装,主要用在进行复杂联合查询上,结果集映射是MyBatis 中最强大的特性。许多复杂的映射都可以轻松解决。
resultType跟resultMap不能同时存在。
resultMap支持继承
extends="User"说明继承了上一个resultMap,要是一个实体类很重的话,先给出一个框架大家都继承与他,是不错的选择
<!--多对多关联查询 --> <resultMap id="User" type="com.entity.User"> <result property="id" column="id" /> <result property="username" column="username" /> <result property="password" column="password" /> </resultMap> <resultMap id="user_info" type="User" extends="User"> <collection property="userInfos" ofType="com.entity.UserInfo" column="uid"> <result property="id" column="id" /> <result property="address" column="address" /> </collection> </resultMap> <select id="getUser" resultMap="user_info"> select * from user u,userinfo i where u.id=i.uid and u.id=#{id} </select>
动态SQL语句完成多条件查询
if
动态 SQL 通常要做的事情是有条件地包含 where 子句的一部分。
<select id="findActiveBlogWithTitleLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <if test="title != null"> AND title like #{title} </if> </select>
这条语句提供了一个可选的文本查找类型的功能。如果没有传入“title”,那么所有处于“ACTIVE”状态的BLOG都会返回。
choose
<select id="queryEmp" resultType="cn.test.entity.Emp"> select * from emp where 1=1 <choose> <when test="deptNo!=null"> and deptno=#{deptNo} </when> <when test="deptName!=null"> and deptname=#{deptName} </when> <otherwise> and personnum>#{personNum} </otherwise> </choose>
</select>
上面也说了,choose相当于Java中的switch语句;当第一个when满足时;就只执行第一个when中的条件。当when中的条件都不满足时;就会执行默认的的;也就是otherwise中的语句。
Where
<select id="getU" resultMap="User" parameterType="com.entity.User"> select*from user <where> <if test="username!=null and username!=''"> username like concat('%',#{username},'%') </if> <if test="id!=null"> and id=#{id} </if> </where> </select>
where下面第一个if语句中以and开头,也可以省略第一个and ,如果第一个if语句中有and;mybatis会将第一个and忽略。
set
<update id="updateEmp" parameterType="cn.test.entity.Emp" flushCache="true"> update emp <set> <if test="empName!=null">empname=#{empName},</if> <if test="job!=null">job=#{job}</if> </set> where empno=#{empNo} </update>
在mybatis中的SQL语句结尾不能加“;”,这样会导致mybatis无法识别字符;导致SQL语句的语法错误;
foreach
动态 SQL 的另外一个常用的必要操作是需要对一个集合进行遍历,通常是在构建 IN 条件语句的时候
<select id="selectPostIn" resultType="domain.blog.Post"> SELECT * FROM POST P WHERE ID in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
foreach 元素的功能是非常强大的,它允许你指定一个集合,声明可以用在元素体内的集合项和索引变量。它也允许你指定开闭匹配的字符串以及在迭代中间放置分隔符。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。
【版本声明】本文为博主原创文章,转载请注明出处