一、MyBatis 接口绑定方案及多参数传递
1、作用:实现创建一个接口后把mapper.xml由mybatis生成接口的实现类,通过调用接口对象就可以获取mapper.xml中编写的sql
2、后面:mybatis和spring整合时使用的是这个方案
3、实现步骤:
3.1 创建一个接口
3.1.1 接口包名和接口名与xxxmapper.xml 中<mappers> namespace相同
3.1.2 接口中的方法名和xxxmapper.xml中的id名相同
3.2 在mybatis.xml 中使用<package> 进行扫描接口和xxxmapper.xml
4、代码实现步骤:
4.1 在mybatis.xml中<mappers>下使用<package>
1 <mappers> 2 <package name="com.bjsxt.mapper"/> 3 </mappers>
4.2 在com.bjsxt.mapper包下新建接口
1 public interface PeopleMapper { 2 查询所有信息 3 List<People> selall (); 4 查询带参数的 这是使用注解 5 List<People> selcount(int age); List<People> zhujie(@Param("ace")int id) 6 }
4.3 在com.bjsxt.mapper包下新建xml
注意:namespace = 必须和接口全限定路径(包名+类名)一致
id值 必须和接口中方法名相同
如果接口中方法为多个参数,可以省略parameterType
1 <mapper namespace="com.bjsxt.mapper.PeopleMapper"> 2 <select id="selall" resultType="People"> 3 select * from people 4 </select> 5 <select id="selcount" resultType="People"> 6 select * from people where age=#{0} 7 </select>
这是使用注解 使用注解原理是 mybatis把参数转换为map,其中@Param("key")
参数内容就是map的value
<select id="zhujie" resultType="People">
select * from people where id=#{ace} id=#{} 里面的内容就是上面中@Param("ace")
</select> 8 </mapper>
实现方式: 接口绑定 的实现原理是 使用proxy代理类(作用是实现接口中的方法把通过代理类实现 反射实现)
1 PeopleMapper mapper = session.getMapper(PeopleMapper.class); 2 List<People> pel = mapper.selcount(25); 3 for(People li:pel){ 4 System.out.println(li); 5 }
二、动态SQL
1、根据不同的条件需要执行不同的SQL语句,称之为 动态SQL
2、MyBatis中动态SQL在mapper.xml中添加逻辑判断等
3、<if> 使用
1 <select id="selcount" resultType="People"> 2 select * from people where 1=1 3 <!-- OGNL表达式 直接写Key或对象属性,不需要添加任何特定符号--> 4 <if test="id!=null and id!=''"> 5 and id=#{id} 6 </if> 7 <if test="age!=null and age!=''"> 8 and age=#{age} 9 </if> 10 </select>
4、<where> 使用
4.1 当编写where 标签时,如果内容中第一个是and去掉第一个 and
4.2 如果<where> 中有内容会生成where关键字,如果没有内容不生成where关键
4.3 使用实例 使用where其实比if 少写了where关键字,使用where比if效率更高,因为 where中少了一个条件判断
1 <select id="selcount" resultType="People"> 2 select * from people 3 <where> 4 <if test="id!=null and id!=''"> 5 and id=#{id} 6 </if> 7 <if test="age!=null and age!=''"> 8 and age=#{age} 9 </if> 10 </where> 11 </select>
5、<choose><when><otherwise>
5.1 只有有一个成立,其他都不执行
5.2 代码实例
5.2.1 如果 id 和 age 都不是null 或不是 " " 生成的sql语句中只有where id=?
1 <select id="selcount" resultType="People"> 2 select * from people 3 <where> 4 <choose> 5 <when test="id!=null and id!=''"> 6 and id=#{id} 7 </when> 8 <when test="age!=null and age!=''"> 9 and age=#{age} 10 </when> 11 </choose> 12 </where> 13 </select>
6、<set> 用在修改SQL中 set 从句
6.1 作用:去掉最后一个 逗号
6.2 作用:如果<set> 里面有内容就会生成set关键字,没有就不生成
6.3 实例:
1 <update id="upd" parameterType="People"> 2 update people 3 <set> 4 id=#{id}, 目的防止<set>中没有内容,mybatis不生成set关键字,如果修改中没有set从句SQL语法错误 5 <if test="name!=null and name!=''"> 6 name=#{name}, 7 </if> 8 <if test="age!=null and age!=''"> 9 age=#{age}, 10 </if> 11 </set> 12 where id=#{id} 13 </update> 14
7、<Trim>
7.1 prefix 在前面添加内容
7.2 prefixOverrides 去掉前面内容
7.3 suffix 在后面添加内容
7.4 suffixOverrides 去掉后面内容
7.5 执行顺序先去掉内容后添加内容
代码实例:
1 <update id="upd" parameterType="People"> 2 update people 3 <trim prefix="set" suffixOverrides=","> 4 name=#{name}, 5 </trim> 6 where id=#{id} 7 </update>
8 <bind>
8.1 作用:给参数重新赋值
8.2 场景:
8.2.1 模糊查询
8.2.2 在原内容前或后添加内容
8.3 代码实例:
1 <select id="selall" parameterType="People" resultType="People" 2 <bind name="name" value="'%'+name+'%'" /> 3 #{name} 4 </select>
9、<foreach>标签
9.1 循环参数内容,还具备在内容的前后添加内容,还具备分隔符功能
9.2 使用场景:in查询中,批量新增中(mybatis中foreach效率比较低 很低)
9.2.1 如果希望批量新增SQL命令
1 insert into people values(default,'xu',15),(default,'ji',45),(default,'we',12)
9.2.2 openSession() 必须指定
底层实现是 JDBC的 PreparedStatement.addBatch();
1 SqlSession session=sql.openSession(ExecutorType.BATCH);
代码实例:collection=" " 要遍历的集合
item=" " 迭代变量,#{迭代变量名} 获取内容
open 循环后左侧添加的内容
close 循环后右侧添加的内容
separator 每次循环时,元素之间的分隔符
1 <select id="selIn" parameterType="list" resultType="People"> 2 select * from people where id in 3 <foreach collection="list" item="li" open="(" close=")" separator=","> 4 #{li} 5 </foreach> 6 </select>
10 <sql>和<include>
10.1 某些SQL片段如果希望复用,可以使用<sql>
<sql id="people">
name,age
</sql>
10.2 在<select> 或<delete> 或<update> 或<insert> 中使用<include>引用
<select id="seltwotable"> select <include refid="people"></include> for people </select>