mybatis学习笔记3

mybatis学习笔记1

mybatis学习笔记2
4.MyBatis 接口绑定方案及多参数传递
作用:实现创建一个接口后把mapper.xml由mybatis生成接口的实现类,通过调用接口对象就可以获取 mapper.xml 中编写的 sql.而后与spring 整合时使用的是这个方案.
实现步骤:
(1)创建一个接口
(2)接口包名和接口名与 mapper.xml 中mapper的namespace
相同;接口中方法名和 mapper.xml 标签的 id 属性相同
(3)在 mybatis.xml 中使用进行扫描接口和 mapper.xml
具体代码:
在 mybatis.xml 中mappers下使用package

<mappers>
<package name="com.mapper"/>
</mappers>

在 com.mapper 下新建接口

public interface LogMapper {
List<Log> selAll();
}

在 com.mapper 新建一个 LogMapper.xml
namespace 必须和接口全限定路径(包名+类名)一致;id 值必须和接口中方法名相同。如果接口中方法为多个参数,可以省略 parameterType

<mapper namespace="com.mapper.LogMapper">
<select id="selAll" resultType="log">
select * from log
</select>
</mapper>

多参数实现办法
在接口中声明方法

List<Log> selByAccInAccout(String accin,String accout);

在 mapper.xml 中添加 #{}中使用 0,1,2 或 param1,param2

<!-- 当多参数时,不需要写 parameterType -->
<select id="selByAccInAccout" resultType="log" >
select * from log where accin=#{0} and accout=#{1}
</select>

也可以使用注解的方式
在接口处声明方法

List<Log> selByAccInAccout(@Param("accin") String accin123,@Param("accout") String accout3454);

在 mapper.xml 中添加 #{} 里面写@Param(“内容”)参数中内容

<select id="selByAccInAccout" resultType="log" >
select * from log where accin=#{accin} and
accout=#{accout}
</select>

5.动态 SQL
概念:根据不同的条件需要执行不同的 SQL 命令.称为动态 SQL。
MyBatis 中动态 SQL 在 mapper.xml 中添加逻辑判断等
(1)If的使用

<select id="selByAccinAccout" resultType="log">
select * from log where 1=1
<!-- OGNL 表达式,直接写 key 或对象的属性.不需要添加任
何特字符号 -->
<if test="accin!=null and accin!=''">
and accin=#{accin}
</if>
<if test="accout!=null and accout!=''">
and accout=#{accout}
</if>
</select>

(2)where的使用
当编写 where 标签时,如果内容中第一个是 and 去掉第一个
and;如果中有内容会生成 where 关键字,如果没有内容不
生成 where 关键

<select id="selByAccinAccout" resultType="log">
select * from log
<where>
<if test="accin!=null and accin!=''">
and accin=#{accin}
</if>
<if test="accout!=null and accout!=''">
and accout=#{accout}
</if>
</where>
</select>

(3)choose、when、otherwise
只有有一个成立,其他都不执行
如果 accin 和 accout 都不是 null 或不是” ”生成的 sql 中只有 where accin=?

<select id="selByAccinAccout" resultType="log">
select * from log
<where>
<choose>
<when test="accin!=null and accin!=''">
and accin=#{accin}
</when>
<when test="accout!=null and accout!=''">
and accout=#{accout}
</when>
</choose>
</where>
</select>

(4)set用在修改sql中set从句
作用:去掉最后一个逗号,如果set里面有内容生成set关键字,没有就不生成
例如:

<update id="upd" parameterType="log" >
	update log
	<set>
		id=#{id},
			<if test="accIn!=null and accIn!=''">
				accin=#{accIn},
			</if>
			<if test="accOut!=null and accOut!=''">
				accout=#{accOut},
			</if>
	</set>
	where id=#{id}
</update>

其中,id=#{id} 目的是防止set中没有内容,mybatis 不生成 set 关键字,如果修改中没有 set 从句 SQL 语法错误.
(5)tirm
因为tirm的内容很多,引用了其他大佬的博客
tirm标签的理解
写的很详细

(6)bind
作用:给参数重新赋值,大多用于模糊查询

<select id="selByLog" parameterType="log" resultType="log">
<bind name="accin" value="'%'+accin+'%'"/>
	#{money}
</select>

(7)foreach
作用:循环参数内容,且还具备在内容的前后添加内容,添加分隔符;大多用于in查询或批量新增中(mybatis中foreach的效率比较低)。
批量新增数据的sql命令为

insert into log VALUES
(default,1,2,3),(default,2,3,4),(default,3,4,5)

openSession必须指定底层JDBC的PreprareStatement.addBatch();

factory.openSession(ExecutorType.BATCH);

在而后的xml文件代码为

<select id="selIn" parameterType="list" resultType="log">
	select * from log where id in
	<foreach collection="list" item="abc" open="(" close=")" separator=",">
		#{abc}
	</foreach>
</select>

collectino=”” 要遍历的集合
item 迭代变量, #{迭代变量名}获取内容
open 循环后左侧添加的内容
close 循环后右侧添加的内容
separator 每次循环时,元素之间的分隔符

(8)sql与include
某些sql片段如果希望复用,可以使用sql定义这个片段

<sql id="mysql">
	id,accin,accout,money
</sql>

在增删改查中也可以使用include引用

<select id="">
 	select <include refid="mysql"></include>from log
</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值