mybatis foreach标签详解,逗号,and还有or,留一个

2019-10-30 13:25:17

情景:查询数据库中文章的相关文章   文章为一个表 字段tags为相关文章字符串中间用','逗号进行啦分割

查询完一个文章后可以把tags字段构造为一个List<String> 然后利用这个集合作为条件来查询


 

1
2
3
4
5
6
7
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
  select * from t_news n where
  <foreach collection="listTag" index="index" item="tag" open="("
    separator="," close=")">
   #{tag} in n.tags
  </foreach>
 </select>

  

看。 foreach 生成的效果是集合 转化为下面的

 select * from t_news n where ( ? in n.tags , ? in n.tags )

 


foreach元素的属性主要有 item,index,collection,open,separator,close。

item表示集合中每一个元素进行迭代时的别名.

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

open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符.

close表示以什么结束.

 

1
2
3
4
5
6
7
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
  select * from t_news n where
  <foreach collection="listTag" index="index" item="tag" open=""
    separator="or" close="">
   #{tag} in n.tags
  </foreach>
 </select>

  

所以 去除左右括号 和 把,改成 or 进行 。 就可以转化为这种形式。
select * from t_news n where ? in n.tags or ? in n.tags   这样可以用List<String> 来查。

 

但是查不到数据
需要使用如下方式:

1
2
3
4
5
6
7
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
  select * from t_news n where
  <foreach collection="listTag" index="index" item="tag" open=""
    separator="or" close="">
    n.tags like  '%'||#{tag}||'%'
  </foreach>
<select>

  

生成的SQL为

select * from t_news n where n.tags like ? or n.tags like ?    

 

foreach : 用的更多的地方为: 根据Id批量删除    /    判断什么 in 集合中(此时需要生成(**,***,)的形式)


  • 2020-11-18 14:34:00

    当你写爬虫抓不到APP请求包的时候该怎么办?

    提示:因为高级篇以后的APP将无法使用很通用的方式处理,每种类型甚至是每个APP的反抓包处理方式都会有差别,所以这个系列以后会以【高级篇-具体类型】的形式来写。

  • 2020-11-21 20:41:51

    Kotlin Sealed class类详解

    Sealed class(密封类) 是一个有特定数量子类的类,看上去和枚举有点类似,所不同的是,在枚举中,我们每个类型只有一个对象(实例);而在密封类中,同一个类可以拥有几个对象。

  • 2020-11-22 20:53:43

    Dagger2之Kotlin写法

    修饰构造方法 修饰变量,在宿主类里,引入要注入的实例

  • 2020-11-22 20:56:13

    Dagger2使用详解

    简单的说,就是一个工厂模式,由Dagger负责创建工厂,帮忙生产instance。遵从Java规范JSR 330,可以使用这些注解。现在不研究Dagger2是如何根据注解去生成工厂的,先来看看工厂是什么东西,理解为什么可以实现了DI(Dependency Injection),如何创建IoC(Inverse of Control)容器。

  • 2020-11-22 21:00:28

    dagger.android--Fragment,BaseFragment

    1 使用Fragment参数来代替Activity参数 2 使用 @FragmentKey来代替@ActivityKey 3 使用HasFragmentInjector来代替@HasActivityInjector 4 AndroidInjection.inject(Fragment)方法,在Fragment的onAttach()中调用,而不是在onCreate()中 5 Fragment的Module添加位置,和Activity是不同的,它取决于Fragment需要的其他依赖注入

  • 2020-11-22 21:12:30

    Dependency Injection with Dagger2,Fragment

    標註@Provides的method若有parameter的話,Dagger會找出其擁有的該型態物件來使用。我們在Module內新增了DataModel將其列入Dagger的管理下,接著在provideFactory()增加parameter變成provideFactory(DataModel dataModel),Dagger就會找出其管理的DataModel給provideFactory使用。

  • 2020-11-22 22:58:52

    Android LiveData Transformations

    有时候有这样的需求,需要在LiveData将变化的数据通知给观察者前,改变数据的类型;或者是返回一个不一样的LiveData。

  • 2020-11-22 23:00:16

    androidx中的lifecycle组件

    Lifecycle-aware components生命周期感知组件执行操作,以响应另一个组件生命周期状态的更改,例如Activity和Fragment。这些组件可以帮助您生成更有组织、更容易维护的轻量级代码。