一、简述
1️⃣config文件常用标签
properties标签:引入外部properties文件资源。
settings标签:设置mybatis全局行为。
typeAlias标签:减少mapper文件配置,给模型类起别名。
transactionManager标签:配置mybatis的事务行为(JDBC|MANAGED)
dataSource标签:配置mybatis数据源(POOLED|UNPOOLED|JNDI)
mappers标签:引入Mapper文件或接口(url|resources|class|package)
2️⃣Mapper文件常用标签
a. resultMap标签:对查询的结果进行封装处理
简单pojo类型处理,只需处理表中字段和对像中属性映射处理。
包装pojo类型处理,使用Collection处理包装集合类型的对像,association处理包含单个pojo对像。
Collection处理的两种方式:
1.连接查询join
2.通过多条select查询的方式。
association处理两种方式:
1.连接查询join
2.通过多条select查询的方式。
discriminor鉴别器:根据查询结果中某个字段作为标识符,根据其查询的值,进行分类封装处理。
b.主键映射策略:如果主键由数据库生成,而不是手动指定,怎么获取该主键的问题。
一般使用selectKey标签处理。
3️⃣自定义封装结果集(了解):
按照自已定义方式把数据库返回结果封装成自己想要的类型。
一般自己建立结果的处理类,实现ResultHandler接口,重写里面的方法。
一般在select("statementId",params,ResultHandler实现类对像)
4️⃣解决什么样问题
用来解决sql语句where后条件的拼接问题,
使用流程控制标签来完成条件的拼接:if标签,where标签,foreach标签。。。。
5️⃣常用动态sql标签
1.if标签
2.choose when otherwise标签
3.trim标签
4.foreache标签
5.where标签
6.set标签
二、if标签:类似于Java中的if语句
<select id="selectByItem" parameterType="User" resultType="User">
select * from USER
where 1=1
<!--mybatis如果是低版本时,如果是整形值,不要用‘’来判断;3.4.6版本没有问题-->
<if test="id != null and id !=''">
and id = #{id}
</if>
<if test="username !=null and username !=''">
and username=#{username}
</if>
<if test="password !=null and password !=''">
and password=#{password}
</if>
<if test="address !=null and address !=''">
and address=#{address}
</if>
</select>
三、where标签
根据查询条件是否存在,来决定是否生成where字符串。可以去除where后面紧跟的sql关键字, 如or或and。
<!-- where标签 -->
<select id="selectByItem2" parameterType="User" resultType="User">
select * from USER
<where>
<if test="id !='' and id != null">
and id = #{id}
</if>
<if test="username !=null and username !=''">
and username=#{username}
</if>
<if test="password !=null and password !=''">
and password=#{password}
</if>
<if test="address !=null and address !=''">
and address=#{address}
</if>
</where>
</select>
四、 choose when otherwise
和Java中switch case作用类似。不管有多少条件满足,只拼接其中一个条件。
<!-- choose when otherwise -->
<select id="selectByItem3" parameterType="User" resultType="User">
select * from USER
<where>
<choose>
<when test="id !='' and id != null">
and id = #{id}
</when>
<when test="username !=null and username !=''">
and username=#{username}
</when>
<when test="password !=null and password !=''">
and password=#{password}
</when>
<when test="address !=null and address !=''">
and address=#{address}
</when>
<otherwise>
and 1=1
</otherwise>
</choose>
</where>
</select>
五、Set标签
set用法和上面where用法一致。可以生成update语句中的set关键字,也可去除sql关键字,如逗号(set标签中sql字符串最后的逗号)
<update id="update" parameterType="User">
update USER
<set>
<if test="username !=null and username !=''">
username = #{username},
</if>
<if test="password !=null and password !=''">
password=#{password},
</if>
<if test="address !=null and address !=''">
address=#{address},
</if>
</set>
where id = #{id}
</update>
六、foreach标签
类似于Java中的foreach迭代。把传入sql语句中的数组类型或集合类型数据进行遍历操作。
<!-- 接口中参数为数组时,collection值必须为array -->
<select id="selectByIds" resultType="User">
select * from USER
<foreach collection="array" item="id" open="where id in(" separator="," close=")">
#{id}
</foreach>
</select>
<!-- 接口中参数为pojo,collection值必须为该数组类型属性名 -->
<select id="selectByIds2" resultType="User" parameterType="User">
select * from USER
<foreach collection="ids" item="id" open="where id in(" separator="," close=")">
#{id}
</foreach>
</select>
七、trim标签:可以删除sql语句指定的字符串。
<!--
prefix="前缀":在后面sql字符串前面拼接指定的前缀。
prefixOverrides="要去除的字符":把紧跟着前缀后面的字符去掉。
suffix="后缀":在后面sql字符串后面拼接指定的后缀。
suffixOverrides="要去除的字符":把紧跟着后缀前面的字符去掉。
-->
<update id="update2" parameterType="User">
update USER
<trim prefix="set" prefixOverrides=",">
<if test="username !=null and username !=''">
,username = #{username}
</if>
<if test="password !=null and password !=''">
,password=#{password}
</if>
<if test="address !=null and address !=''">
,address=#{address}
</if>
</trim>
where id = #{id}
</update>
<update id="update3" parameterType="User">
update USER
<trim prefix="set" suffixOverrides=",">
<if test="username !=null and username !=''">
username = #{username},
</if>
<if test="password !=null and password !=''">
password=#{password},
</if>
<if test="address !=null and address !=''">
address=#{address},
</if>
</trim>
where id = #{id}
</update>
网友评论