首页 » SEO优化 » phptrimtit函数技巧_mybatis中的xml文件总结mybatis的动态sql

phptrimtit函数技巧_mybatis中的xml文件总结mybatis的动态sql

访客 2024-11-25 0

扫一扫用手机浏览

文章目录 [+]

如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系,能将查询结果映射到pojo工具中。

ResultMap可以将查询结果映射为繁芜类型的pojo,比如在查询结果中包括pojo和list实现一对一查询和一对多查询。

phptrimtit函数技巧_mybatis中的xml文件总结mybatis的动态sql

动态sqlIf

把稳要做不即是空字符串校验。

phptrimtit函数技巧_mybatis中的xml文件总结mybatis的动态sql
(图片来自网络侵删)

Foreach

向sql通报数组或者list,mybatis利用foreach进行解析。

//通报多个用户id

Private List<Integer> ids;

Sql片段

将重复的sql提取出来,包括重复的where条件,利用include引用。

如果引用其它mapper.xml的sql片段,则在引用时须要加上namespase

mybatis 的动态sql语句是基于OGNL表达式的。
可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句紧张有以下几类:

1. if 语句 (大略的条件判断)

2. choose (when,otherwize) ,相称于java 措辞中的 switch ,与 jstl 中的choose 很类似.

3. trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)

4. where (紧张是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法缺点)

5. set (紧张用于更新时)

6. foreach (在实现 mybatis in 语句查询时特殊有用)

下面分别先容这几种处理办法

1、mybatis if语句处理

<select id=\"大众dynamicIfTest\"大众 parameterType=\"大众Blog\公众 resultType=\"大众Blog\"大众> select from t_blog where 1 = 1 <if test=\公众title != null\公众> and title = #{title} </if> <if test=\"大众content != null\"大众> and content = #{content} </if> <if test=\"大众owner != null\"大众> and owner = #{owner} </if></select>

解析:

如果你供应了title参数,那么就要知足title=#{title},同样如果你供应了Content和Owner的时候,它们也须要知足相应的条件,之后便是返回知足这些条件的所有Blog,这是非常有用的一个功能。

以往我们利用其他类型框架或者直策应用JDBC的时候, 如果我们要达到同样的选择效果的时候,我们就须要拼SQL语句,这是极其麻烦的,比起来,上述的动态SQL就要大略多了。

2、choose (when,otherwize) ,相称于java 措辞中的 switch ,与 jstl 中的choose 很类似

<select id=\公众dynamicChooseTest\公众 parameterType=\公众Blog\"大众 resultType=\"大众Blog\"大众> select from t_blog where 1 = 1 <choose> <when test=\"大众title != null\"大众> and title = #{title} </when> <when test=\"大众content != null\"大众> and content = #{content} </when> <otherwise> and owner = \"大众owner1\"大众 </otherwise> </choose></select>

when元素表示当when中的条件知足的时候就输出个中的内容,跟JAVA中的switch效果差不多的是按照条件的顺序,当when中有条件知足的时候,就会跳出choose,即所有的when和otherwise条件中,只有一个会输出,当所有的我很条件都不知足的时候就输出otherwise中的内容。
以是上述语句的意思非常大略,当title!=null的时候就输出and titlte = #{title},不再往下判断条件,当title为空且content!=null的时候就输出and content = #{content},当所有条件都不知足的时候就输出otherwise中的内容。

3、trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)

<select id=\公众dynamicTrimTest\公众 parameterType=\"大众Blog\"大众 resultType=\公众Blog\"大众> select from t_blog <trim prefix=\"大众where\"大众 prefixOverrides=\"大众and |or\公众> <if test=\公众title != null\"大众> title = #{title} </if> <if test=\公众content != null\"大众> and content = #{content} </if> <if test=\公众owner != null\"大众> or owner = #{owner} </if> </trim></select>

trim元素的紧张功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正由于trim有这样的功能,以是我们也可以非常大略的利用trim来代替where元素的功能。

trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码:

select from user <trim prefix=\"大众WHERE\"大众 prefixoverride=\公众AND |OR\公众> <if test=\公众name != null and name.length()>0\"大众> AND name=#{name} </if> <if test=\"大众gender != null and gender.length()>0\"大众> AND gender=#{gender} </if></trim>

如果说name和gender的值都不为null的话打印的SQL为:select from user where name = 'xx' and gender = 'xx'

在赤色标记的地方是不存在第一个and的,上面两个属性的意思如下:

prefix:前缀

prefixoverride:去掉第一个and或者是or

update user<trim prefix=\"大众set\"大众 suffixoverride=\"大众,\"大众 suffix=\"大众 where id = #{id} \"大众> <if test=\公众name != null and name.length()>0\"大众> name=#{name} , </if> <if test=\"大众gender != null and gender.length()>0\"大众> gender=#{gender} , </if></trim>

如果说name和gender的值都不为null的话打印的SQL为:update user set name='xx' , gender='xx' where id='x'

在赤色标记的地方不存在逗号,而且自动加了一个set前缀和where后缀,上面三个属性的意义如下,个中prefix意义如上:

suffixoverride:去掉末了一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)

suffix:后缀

4、where (紧张是用来简化sql语句中where条件判断的,能智能的处理 and or 条件)

<select id=\公众dynamicWhereTest\"大众 parameterType=\"大众Blog\公众 resultType=\公众Blog\"大众> select from t_blog <where> <if test=\"大众title != null\公众> title = #{title} </if> <if test=\公众content != null\"大众> and content = #{content} </if> <if test=\公众owner != null\"大众> and owner = #{owner} </if> </where></select>

where元素的浸染是会在写入where元素的地方输出一个where,其余一个好处是你不须要考虑where元素里面的条件输出是什么样子的,MyBatis会智能的帮你处理,如果所有的条件都不知足那么MyBatis就会查出所有的记录,如果输出后是and 开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在where元素中你不须要考虑空格的问题,MyBatis会智能的帮你加上。
像上述例子中,如果title=null, 而content != null,那么输出的全体语句会是select from t_blog where content = #{content},而不是select from t_blog where and content = #{content},由于MyBatis会智能的把首个and 或 or 给忽略。

5、set (紧张用于更新时)

<update id=\"大众dynamicSetTest\公众 parameterType=\"大众Blog\"大众> update t_blog <set> <if test=\公众title != null\公众> title = #{title}, </if> <if test=\"大众content != null\"大众> content = #{content}, </if> <if test=\"大众owner != null\"大众> owner = #{owner} </if> </set> where id = #{id}</update>

set元素紧张是用在更新操作的时候,它的紧张功能和where元素实在是差不多的,紧张是在包含的语句前输出一个set,然后如果包含的语句因此逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。
有了set元素我们就可以动态的更新那些修正了的字段。

6、foreach (在实现 mybatis in 语句查询时特殊有用)

foreach的紧张用在构建in条件中,它可以在SQL语句中进行迭代一个凑集。
foreach元素的属性紧张有item,index,collection,open,separator,close。

(1)item表示凑集中每一个元素进行迭代时的别名。

(2)index指定一个名字,用于表示在迭代过程中,每次迭代到的位置。

(3)open表示该语句以什么开始。

(4)separator表示在每次进行迭代之间以什么符号作为分隔符。

(5)close表示以什么结束。

在利用foreach的时候最关键的也是最随意马虎出错的便是collection属性,该属性是必须指定的,但是在不同情形下,该属性的值是不一样的,紧张有一下3种情形:

(1)如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

(2)如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

(3)如果传入的参数是多个的时候,我们就须要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key便是参数名,以是这个时候collection属性值便是传入的List或array工具在自己封装的map里面的key。

6.1、单参数List的类型

<select id=\"大众dynamicForeachTest\公众 resultType=\"大众com.mybatis.entity.User\"大众> select from t_user where id in <foreach collection=\"大众list\"大众 index=\公众index\公众 item=\"大众item\"大众 open=\"大众(\公众 separator=\"大众,\"大众 close=\"大众)\"大众> #{item} </foreach></select>

上述collection的值为list,对应的Mapper是这样的:

/mybatis Foreach测试 /public List<User> dynamicForeachTest(List<Integer> ids);

测试代码:

@Testpublic void dynamicForeachTest() { SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); ids.add(6); List<User> userList = mapper.dynamicForeachTest(ids); for (User user : userList){ System.out.println(user); } sqlSession.close();}

6.2、数组类型的参数

<select id=\公众dynamicForeach2Test\公众 resultType=\"大众com.mybatis.entity.User\"大众> select from t_user where id in <foreach collection=\公众array\公众 index=\"大众index\公众 item=\"大众item\"大众 open=\"大众(\公众 separator=\公众,\公众 close=\"大众)\"大众> #{item} </foreach></select>

对应mapper:

public List<User> dynamicForeach2Test(int[] ids);

测试代码:

@Testpublic void dynamicForeach2Test() { SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int[] ids = {1,2,6}; List<User> userList = mapper.dynamicForeach2Test(ids); for (User user : userList){ System.out.println(user); } sqlSession.close();}

6.3、Map类型的参数

<select id=\"大众dynamicForeach3Test\"大众 resultType=\公众com.mybatis.entity.User\公众> select from t_user where username like '%${username}%' and id in <foreach collection=\"大众ids\"大众 index=\公众index\"大众 item=\公众item\公众 open=\公众(\公众 separator=\"大众,\"大众 close=\公众)\"大众> #{item} </foreach></select>

mapper 该当是这样的接口:

/mybatis Foreach测试 /public List<User> dynamicForeach3Test(Map<String, Object> params);

测试方法:

@Testpublic void dynamicForeach3Test() { SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); ids.add(6); Map map =new HashMap(); map.put(\公众username\公众, \"大众小\公众); map.put(\"大众ids\"大众, ids); List<User> userList = mapper.dynamicForeach3Test(map); System.out.println(\"大众------------------------\公众); for (User user : userList){ System.out.println(user); } sqlSession.close();}

通过以上方法,就能完成一样平常的mybatis 的 动态SQL 语句.最常用的便是 if where foreach这几个,一定要重点节制.

标签:

相关文章

语言枚举类型,探索人类语言多样性的奥秘

语言是人类交流的重要工具,也是人类文明发展的重要标志。随着全球化进程的不断推进,各种语言枚举类型应运而生。本文将从语言枚举类型的定...

SEO优化 2024-12-29 阅读0 评论0

语言栏消失,科技变革下的挑战与机遇

近年来,随着科技的飞速发展,智能手机、平板电脑等移动设备的普及,语言栏这一功能已经成为了我们日常生活中不可或缺的一部分。近期有消息...

SEO优化 2024-12-29 阅读0 评论0

语言混合现象的多元魅力与挑战

语言混合作为一种跨文化交流的现象,逐渐成为世界范围内语言学研究的热点。它不仅丰富了语言的多样性,也反映了全球化背景下人类社会的交流...

SEO优化 2024-12-29 阅读0 评论0

语言是思想的载体,介绍语言与思想的关系

在人类文明的进程中,语言一直扮演着至关重要的角色。它不仅是人们沟通交流的工具,更是承载着人类思想的载体。自古以来,人们就深知语言与...

SEO优化 2024-12-29 阅读0 评论0