- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
注解版下,使用动态SQL需要将sql语句包含在script标签里。
在 < script>< /script>内使用特殊符号,则使用java的转义字符,如 双引号""
使用\"\"
代替
<script></script>
if
:通过判断动态拼接sql语句,一般用于判断查询条件
当查询语句的查询条件由于输入参数的不同而无法确切定义时,可以使用< where>标签对来包裹需要动态指定的SQL查询条件,而在< where>标签对中,可以使用< if test="…">条件来分情况设置SQL查询条件
当使用标签对包裹 if 条件语句时,将会忽略查询条件中的第一个and或or
<!-- 查询用户信息 -->
<select id="queryUserInfo" parameterType="com.mybatis.po.UserParam" resultType="com.mybatis.po.User">
SELECT * FROM tb_user
<where>
<if test="userId > 0">
and user_id = #{userId}
</if>
<if test="userName!= null and userName!=''">
and user_name like '%${userName}%'
</if>
<if test="sex!=null and sex!=''">
and sex = #{sex}
</if>
</where>
</select>
@Select({"<script>" +
" select * from tb_user " +
"<where>" +
"<if test = 'userId != null and userId !=\"\" '> " +
"and user_Id = #{userId} " +
"</if>" +
"<if test = 'userPassword != null and userPassword !=\"\" '> " +
"and user_password like CONCAT('%',#{userPassword},'%')" +
"</if>" +
"</where>" +
"</script>"})
MyBatis提供了可以将复用性比较强的SQL语句封装成“SQL片段”,在需要使用该SQL片段的映射配置中声明一下,即可引入该SQL语句,声明SQL片段的格式如下:
<sql id="query_user_where">
<!-- 要复用的SQL语句 -->
</sql>
例子:
<!--用户查询条件SQL片段-->
<sql id="query_user_where">
<if test="userId>0">
AND user_id = #{userId}
</if>
<if test="userName!=null and userName!=''">
AND user_name like '%${userName}%'
</if>
<if test="sex!=null and sex!=''">
AND sex = #{sex}
</if>
</sql>
<!-- 查询用户信息 -->
<select id="queryUserInfo" parameterType="com.mybatis.po.UserParam" resultType="com.mybatis.po.User">
SELECT * FROM tb_user
<where>
<include refid="query_user_where"/>
<!-- 这里可能还会引入其他的SQL片段 -->
</where>
</select>
< foreach>标签属性说明:
属性 | 说明 |
---|---|
index | 当迭代对象是数组,列表时,表示的是当前迭代的次数。 |
item | 当迭代对象是数组,列表时,表示的是当前迭代的元素。 |
collection | 当前遍历的对象。 |
open | 遍历的SQL以什么开头。 |
close | 遍历的SQL以什么结尾。 |
separator | 遍历完一次后,在末尾添加的字符等。 |
需求:
SELECT * FROM tb_user WHERE user_id=2 OR user_id=4 OR user_id=5;
-- 或者
SELECT * FROM tb_user WHERE user_id IN (2,4,5);
案例:
<!-- 使用foreach标签,拼接or语句 -->
<sql id="query_user_or">
<if test="ids!=null and ids.length>0">
<foreach collection="ids" item="user_id" open="AND (" close=")" separator="OR">
user_id=#{user_id}
</foreach>
</if>
</sql>
<!-- 使用foreach标签,拼接in语句 -->
<sql id="query_user_in">
<if test="ids!=null and ids.length>0">
AND user_id IN
<foreach collection="ids" item="user_id" open="(" close=")" separator=",">
#{user_id}
</foreach>
</if>
</sql>
@Insert("<script>" +
"insert into tb_user(user_id,user_account,user_password,blog_url,blog_remark) values" +
"<foreach collection = 'list' item = 'item' index='index' separator=','>" +
"(#{item.userId},#{item.userAccount},#{item.userPassword},#{item.blogUrl},#{item.blogRemark})" +
"</foreach>" +
"</script>")
int insertByList(@Param("list") List<UserInfo> userInfoList);
@Select("<script>" +
"select * from tb_user" +
" WHERE user_id IN " +
"<foreach collection = 'list' item = 'id' index='index' open = '(' separator= ',' close = ')'>" +
"#{id}" +
"</foreach>" +
"</script>")
List<UserInfo> selectByList(@Param("list") List<Integer> ids);
@Update({"<script>" +
"<foreach item='item' collection='list' index='index' open='' close='' separator=';'>" +
" UPDATE tb_user " +
"<set>" +
"<if test='item.userAccount != null'>user_account = #{item.userAccount},</if>" +
"<if test='item.userPassword != null'>user_password=#{item.userPassword}</if>" +
"</set>" +
" WHERE user_id = #{item.userId} " +
"</foreach>" +
"</script>"})
int updateBatch(@Param("list")List<UserInfo> userInfoList);
有时我们不想应用到所有的条件语句,而只想从中择其一项。针对这种情况,MyBatis提供了choose元素,它有点像Java中的switch语句。
<select id="queryUserChoose" parameterType="com.mybatis.po.UserParam" resultType="com.mybatis.po.User">
SELECT * FROM tb_user
<where>
<choose>
<when test="userId>0">
AND user_id = #{userId}
</when>
<when test="userName!=null and userName!=''">
AND user_name like '%${userName}%'
</when>
<otherwise>
AND sex = '女'
</otherwise>
</choose>
</where>
</select>
@Select("<script>"
+ "select * from tb_user "
+ "<where>"
+ "<choose>"
+ "<when test='userId != null and userId != \"\"'>"
+ " and user_id = #{userId}"
+ "</when>"
+ "<otherwise test='userAccount != null and userAccount != \"\"'> "
+ " and user_account like CONCAT('%', #{userAccount}, '%')"
+ "</otherwise>"
+ "</choose>"
+ "</where>"
+ "</script>")
List<UserInfo> selectAll(UserInfo userInfo);
MyBatis还提供了< trim>标签,我们可以通过自定义< trim>标签来定制< where>标签的功能。比如,和< where>标签等价的自定义 < trim>标
prefixOverrides 属性会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的)。它的作用是移除所有指定在 prefixOverrides 属性中的内容,并且插入 prefix 属性中指定的内容。
使用自定义< trim>标签来定制< where>标签的功能,获取用户信息:
<!-- 查询用户信息 -->
<select id="queryUserTrim" parameterType="com.mybatis.po.UserParam" resultType="com..mybatis.po.User">
SELECT * FROM tb_user
<trim prefix="WHERE" prefixOverrides="AND |OR ">
<if test="userId>0">
and user_id = #{userId}
</if>
<if test="userName!=null and userName!=''">
and user_name like '%${userName}%'
</if>
<if test="sex!=null and sex!=''">
and sex = #{sex}
</if>
</trim>
</select>
在修改用户信息的SQL配置方法中,使用< set>标签过滤多余的逗号:
<!-- 修改用户信息 -->
<update id="updateUser" parameterType="com.pjb.mybatis.po.UserParam">
UPDATE tb_user
<set>
<if test="userName != null">user_name=#{userName},</if>
<if test="sex != null">sex=#{sex},</if>
<if test="age >0 ">age=#{age},</if>
<if test="blogUrl != null">blog_url=#{blogUrl}</if>
</set>
where user_id = #{userId}
</update>
这里,< set>标签会动态前置SET关键字,同时也会删掉无关的逗号,因为用了条件语句之后很可能就会在生成的SQL语句的后面留下这些逗号。因为用的是“if”元素,若最后一个“if”没有匹配上而前面的匹配上,SQL 语句的最后就会有一个逗号遗留。
1.概述 转载:MyBatis 二级缓存全详解 上一篇文章中我们介绍到了 MyBatis 一级缓存其实就是 SqlSession 级别的缓存,什么是 SqlSession 级别的缓存呢?一级缓存的本质
1.概述 转载:核心配置综述之StatementHandler 2.MyBatis 四大组件之StatementHandler StatementHandler 是四大组件中最重要的一个对象,负责操作
1.概述 转载:MyBatis 启动流程 MyBatis 是第一个支持自定义 SQL、存储过程和高级映射的类持久框架。MyBatis 消除了大部分 JDBC 的样板代码、手动设置参数以及检索结果。My
1.概述 转载:MyBatis 基础搭建及架构概述 2.MyBatis 是什么? MyBatis是第一个支持自定义SQL、存储过程和高级映射的类持久框架。MyBatis消除了大部分JDBC的样板代码、
1.概述 转载:核心配置综述之 ParameterHandler MyBatis 四大核心组件我们已经了解到了两种,一个是 Executor ,它是MyBatis 解析SQL请求首先会经过的第一道关卡
1.概述 转载:核心配置综述之 ResultSetHandler 我们之前介绍过了MyBatis 四大核心配置之 Executor、StatementHandler、 ParameterHandler
如果我使用mybatis,我可以很容易地得到更新的行数,就像 update table set desc = 'xxx' where name = ? 但是,如果我想获取更新的行数,而不是计数,我该如
如何在MyBatis 3中使用小于等于 SELECT * FROM( SELECT * FROM TABLE1 WHERE COL1 =#{COL1,jdbc
我将 mybatis3.0.6 与 java 一起使用 哪个性能更好? [select id="getData" parameterType="Integer" resultType="Integer
我无法在 mybatis 中使用动态排序类型创建 SQL,如下例 select user_profile.user_profile_id, user_profile.first_name
这是一个流行的例子。 insert into ACCOUNT ( ACC_ID, ACC_FIRST_NAME, ACC_LAST_NAME, ACC_EMAIL )values (
我下载了MyBatis,文件夹中有一个mybatis-3.0.4-javadoc.jar,我解压并打开它,但它几乎是空的。 哪里可以找到MyBatis的API文档? 最佳答案 http://repo1
我正在尝试为 ArrayList 编写类型处理程序,但这给了我错误,任何人都可以帮助我。 我想将 ArrayList 作为 VARCHAR 存储在数据库中并将其检索为 ArrayList。 packa
目录 依赖 配置 CodeGenerator mybatis-plus-generator + clickhouse 自动生成代码 依赖
目录 三者实现对比 使用fluent mybatis 来实现上面的功能 换成mybatis原生实现效果 换成mybatis plus
例如,我有查询从员工中选择 ID、姓名、年龄、地址,而不是拥有员工对象列表。我希望有一个 map 列表,如 list{ map{ ("id", 123), ("name","jac
我在使 MyBatis (3.4.6) 工作时遇到一些麻烦。 我已将 mybatis-config.xml 文件放置在项目的 src/main/resources 文件夹中,但是当我运行单元测试时,出
我现在使用 Mybatis 和 spring-boot。我没有添加mybatis-config.xml。我根据说明通过 application.properties 为数据源和 mybatis 进行所
这是我的第一篇文章,用我糟糕的英语...... 我使用的是MyBatis3.0 在查询中,我使用 SqlBuilder 的方法如下: public class DataStatisticSqlBuil
主题:MyBatis:Boolean Paraeter:MyBatis 正在使用 Getter 内容: 大家好, 我一直在寻找解决我近乎简单的 MyBatis 问题的方法: 给定代码(仅必要部分):
我是一名优秀的程序员,十分优秀!