Mybatis SQL参数、条件、日志
来源:互联网 发布:深度卷积网络进展 编辑:程序博客网 时间:2024/06/11 14:48
没有试验过本篇中的内容
参数:
通常情况下,Mybatis的mapper文件中Select、Inser、Update和Delete只接收一个参数,并用parameterType指定。
要指定多个参数,可以将多个参数加入到一个hashmap,然后将hashmap作为参数传入。
另外的方法是使用@Param注解标注多个参数,例如:
- List<Contact> selectQuery(@Param(value="contact") Contact contact,@Param(value="start") int start,@Param("size") int size);
- <select id="selectQuery" resultType="Contact">
- select * from contacts
- <where>
- <if test="contact.name!=null">
- name like concat('%',#{contact.name},'%')
- </if>
- <if test="contact.email!=null">
- and email like concat('%',#{contact.email},'%')
- </if>
- </where>
- limit #{start},#{size}
- </select>
条件:
if:条件判断
官方文档代码片段:
- <select id="findActiveBlogWithTitleLike" parameterType="Blog"
- resultType="Blog">
- SELECT * FROM BLOG
- WHERE state = ‘ACTIVE’
- <if test="title != null">
- AND title like #{title}
- </if>
- </select>
choose:多个条件判断,包含when otherwise
官方文档代码片段:
- <select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">
- SELECT * FROM BLOG WHERE state = ‘ACTIVE’
- <choose>
- <when test="title != null">
- AND title like #{title}
- </when>
- <when test="author != null and author.name != null">
- AND author_name like #{author.name}
- </when>
- <otherwise>
- AND featured = 1
- </otherwise>
- </choose>
- </select>
where:构造where条件片段
官方文档代码片段:
- <select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">
- SELECT * FROM BLOG
- <where>
- <if test="state != null">
- state = #{state}
- </if>
- <if test="title != null">
- AND title like #{title}
- </if>
- <if test="author != null and author.name != null">
- AND author_name like #{author.name}
- </if>
- </where>
- </select>
日志:
启动单个mapper的日志
log4j.logger.com.mybatistest.mapper.ContactMapper=TRACE
启动一组mapper的日志
log4j.logger.com.mybatistest.mapper=TRACE
启动日志后,能看到运行的SQL和参数。
- Mybatis SQL参数、条件、日志
- Mybatis SQL参数、条件、日志
- Mybatis Sql日志打印
- mybatis打印sql日志
- mybatis打印sql日志
- MyBatis中的动态SQL,实现不确定条件的CRUD-----使用Map来传递查询的参数
- mybatis配置log4j日志输出sql文及sql参数和结果集
- mybatis sql日志打印设置
- Mybatis Logging 查询sql日志
- mybatis 配置sql打印日志
- mybatis日志查看sql语句
- Mybatis动态SQL 条件查询,批量删除
- mybatis 写sql语句 rownuma条件
- mybatis分页条件查询动态sql
- Mybatis中动态sql(多条件查询)
- Mybatis 动态加载sql when otherwise choose 理解成if else 条件中可以把传来的参数当变量
- mybatis打印sql和参数
- mybatis 动态sql和参数
- html标签大全(html5)
- QX项目实战-12.基础架构试验三:异构数据库同步
- Linux常用命令大全
- js中常用的语句
- js中常用的语句
- Mybatis SQL参数、条件、日志
- LVS连接的持久时间
- Andriod: 去掉系统自带的Activity跳转动画 .
- mysql 表结构的复制和数据的复制
- 我的小窝
- C++文件读写总结
- iter_swap
- 你是否拥有技术管理者必备的能力及解决常见问题 之 技术来源于一线,永远不能脱离一线。
- define的使用技巧