一、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>

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

  4.2 在com.bjsxt.mapper包下新建接口

 public interface PeopleMapper {
查询所有信息
List<People> selall ();
查询带参数的             这是使用注解
List<People> selcount(int age); List<People> zhujie(@Param("ace")int id)
}

   4.3 在com.bjsxt.mapper包下新建xml

      注意:namespace = 必须和接口全限定路径(包名+类名)一致

         id值 必须和接口中方法名相同

           如果接口中方法为多个参数,可以省略parameterType

  <mapper namespace="com.bjsxt.mapper.PeopleMapper">
<select id="selall" resultType="People">
select * from people
</select>
<select id="selcount" resultType="People">
select * from people where age=#{0}
</select>
      这是使用注解                     使用注解原理是 mybatis把参数转换为map,其中@Param("key")
                                        参数内容就是map的value
    <select id="zhujie" resultType="People">
       select * from people where id=#{ace}            id=#{} 里面的内容就是上面中@Param("ace")
    </select>
</mapper>

  

   实现方式:         接口绑定 的实现原理是  使用proxy代理类(作用是实现接口中的方法把通过代理类实现   反射实现)

         PeopleMapper mapper = session.getMapper(PeopleMapper.class);
List<People> pel = mapper.selcount(25);
for(People li:pel){
System.out.println(li);
}

二、动态SQL

  1、根据不同的条件需要执行不同的SQL语句,称之为 动态SQL

  2、MyBatis中动态SQL在mapper.xml中添加逻辑判断等

  3、<if> 使用

     <select id="selcount" resultType="People">
select * from people where 1=1
<!-- OGNL表达式 直接写Key或对象属性,不需要添加任何特定符号-->
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="age!=null and age!=''">
and age=#{age}
</if>
</select>

  4、<where> 使用

    4.1 当编写where 标签时,如果内容中第一个是and去掉第一个 and

    4.2 如果<where> 中有内容会生成where关键字,如果没有内容不生成where关键

    4.3 使用实例   使用where其实比if 少写了where关键字,使用where比if效率更高,因为 where中少了一个条件判断

     <select id="selcount" resultType="People">
select * from people
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="age!=null and age!=''">
and age=#{age}
</if>
</where>
</select>

  5、<choose><when><otherwise>

    5.1 只有有一个成立,其他都不执行

    5.2 代码实例

      5.2.1  如果 id 和 age 都不是null 或不是 " " 生成的sql语句中只有where id=?

 <select id="selcount" resultType="People">
select * from people
<where>
<choose>
<when test="id!=null and id!=''">
and id=#{id}
</when>
<when test="age!=null and age!=''">
and age=#{age}
</when>
</choose>
</where>
</select>

  6、<set> 用在修改SQL中 set 从句

    6.1 作用:去掉最后一个 逗号

    6.2 作用:如果<set> 里面有内容就会生成set关键字,没有就不生成

    6.3 实例:

     <update id="upd" parameterType="People">
update people
<set>
id=#{id},   目的防止<set>中没有内容,mybatis不生成set关键字,如果修改中没有set从句SQL语法错误 <if test="name!=null and name!=''">
name=#{name},
</if>
<if test="age!=null and age!=''">
age=#{age},
</if>
</set>
where id=#{id}
</update>

  7、<Trim>

    7.1 prefix 在前面添加内容

    7.2 prefixOverrides 去掉前面内容

    7.3 suffix 在后面添加内容

    7.4 suffixOverrides 去掉后面内容

    7.5 执行顺序先去掉内容后添加内容

   代码实例:

         <update id="upd" parameterType="People">
update people
<trim prefix="set" suffixOverrides=",">
name=#{name},
</trim>
where id=#{id}
</update>

  8 <bind>

    8.1 作用:给参数重新赋值

    8.2 场景:

      8.2.1 模糊查询

      8.2.2 在原内容前或后添加内容

    8.3 代码实例:  

 <select id="selall" parameterType="People" resultType="People"
<bind name="name" value="'%'+name+'%'" />
#{name}
</select>

    9、<foreach>标签

    9.1 循环参数内容,还具备在内容的前后添加内容,还具备分隔符功能

    9.2 使用场景:in查询中,批量新增中(mybatis中foreach效率比较低  很低)

      9.2.1 如果希望批量新增SQL命令

  insert into people values(default,'xu',15),(default,'ji',45),(default,'we',12)

      9.2.2 openSession() 必须指定

        底层实现是 JDBC的 PreparedStatement.addBatch();

 SqlSession session=sql.openSession(ExecutorType.BATCH);

    代码实例:collection=" " 要遍历的集合

         item=" "  迭代变量,#{迭代变量名} 获取内容

            open 循环后左侧添加的内容

         close 循环后右侧添加的内容

         separator 每次循环时,元素之间的分隔符

     <select id="selIn" parameterType="list" resultType="People">
select * from people where id in
<foreach collection="list" item="li" open="(" close=")" separator=",">
#{li}
</foreach>
</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>

mybatis 接口绑定 和 动态SQL的更多相关文章

  1. SSM框架之Mybatis(6)动态SQL

    Mybatis(6)动态SQL 1.动态SQL 出现原因:有些时候业务逻辑复杂时,我们的 SQL 是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了 1.1.if标签 我们根据实体类的不 ...

  2. Spring mybatis源码篇章-动态SQL节点源码深入

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-动态SQL基础语法以及原理 前话 前文描述到通过mybatis默认的解析驱动类org.apache.ibat ...

  3. MyBatis学习总结_11_MyBatis动态Sql语句

    MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...

  4. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL

  5. Mybatis 接口绑定

    MyBatis的接口绑定: 参考链接:http://blog.csdn.net/chris_mao/article/details/48836039 接口映射就是在IBatis中任意定义接口,然后把接 ...

  6. MyBatis(4)-- 动态SQL

    如果使用JDBC或者类似于Hibernate的其他框架,很多时候要根据需要去拼装SQL,这是一个麻烦的事情.因为某些查询需要许多条件.通常使用其他框架需要大量的Java代码进行判断,可读性比较差,而M ...

  7. Mybatis系列全解(八):Mybatis的9大动态SQL标签你知道几个?提前致女神!

    封面:洛小汐 作者:潘潘 2021年,仰望天空,脚踏实地. 这算是春节后首篇 Mybatis 文了~ 跨了个年感觉写了有半个世纪 ... 借着女神节 ヾ(◍°∇°◍)ノ゙ 提前祝男神女神们越靓越富越嗨 ...

  8. Mybatis映射原理,动态SQL,log4j

    1.理清mybatis中的#和$之间的区别? #{ }:表示一个预处理参数,参数类型不定,是根据传入的参数类型来设定的. 类似于JDBC中的? 特例使用,模糊查询:(针对oracle): and us ...

  9. SSM框架开发web项目系列(四) MyBatis之快速掌握动态SQL

    前言 通过前面的MyBatis部分学习,已经可以使用MyBatis独立构建一个数据库程序,基本的增删查改/关联查询等等都可以实现了.简单的单表操作和关联查询在实际开的业务流程中一定会有,但是可能只会占 ...

随机推荐

  1. TOJ1698/POJ3264Balanced Lineup (线段树 or RMQ-ST)

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1698 时间限制(普通/Java): ...

  2. 6. ZigZag Conversion (字符串的连接)

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  3. 【git】gitignore

    gitignore git专门有个文件用来管理那些不被纳入版本库的文件,这个文件是 [.gitignore],所有不被包含的都能放进去,但这个是有前提的. 前提 前提是文件如果没被git客户端trac ...

  4. CodeForces - 873B Balanced Substring(思维)

    inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...

  5. ubuntu系列-很好用的截图工具shutter

    直接在ubuntu软件市场中搜索“shutter”下载即可

  6. war包内更新文件

    感谢@这个博客提供的分享 亲测有效,原文: 1.如果要替换的文件直接在war包的根目录(一级目录)下,直接使用jar uvf命令替换即可 如:替换a.war中b.xml文件 jar uvf a.war ...

  7. c#、.net、asp.net、asp 、ado.net、.net framework的区别

    c#:一种编程语言 .net:一种运行环境 asp.net:基于.netFramework框架下的一种开发技术(相对与asp而言,引入了服务器控件,前后台可分,编译型的编程框架) asp:也是.net ...

  8. Head First Servlets & JSP 学习笔记 第一章 —— 前言和体系结构

    URL,Uniform Resource Locatiors,统一资源定位符. http:// www.wickedlysmart.com :80 /beeradivice/select /beer1 ...

  9. 微信小程序之 ----API接口

    1. wx.request 接口    可在文件 wxs中操作,连接服务器处理数据    参数    ① url ② data ③ header ④ method ⑤ dataType   回调   ...

  10. RSA加密遇到的一个问题

    1,最近在项目里面使用了RSA加密解密的功能 出现的异常情况是加解密时对于有中文的情况会出现乱码,导致无法正常解析参数   解决方案人认为:针对中文应该 先encode ,这样能有效的避免乱码