好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

mybatis框架xml下trim中的prefix与suffix等标签的用法

trim中prefix与suffix等标签用法

1. prefix 前缀增加的内容

2. suffix 后缀增加的内容

3. prefixOverrides 前缀需要覆盖的内容,一般是第一个判断条件前面的多余的结构,如:第一个判断条件前面多了 ‘and'

?

1

select * from User where name = 'zhangsan' and age= '20' ;

?

1

2

3

4

5

6

7

8

9

10

11

< select id= 'queryUser' >

         select * from User

         <trim prefix= 'where' prefixOverrides= 'and' >

             <if test= "name != null and name != ''" >

                 name = #{ name }

             </if>

             <if test= "age !=null and age !=''" >

                 and age = #{age}

             </if>

         </trim>

< select >

第一个条件前面没有任何符号,第二个条件要加上and,否则sql语句会报错。很理想的状态是第一个和第二个都有值,但是既然判断,说明也可能会没有值,当第一个name没有值的时候,这个时候sql语句就会是 select * from User where and age='',很明显这个sql语句语法存在问题。

在这里标签属性prefixOverrides就起作用了,它会让前缀where覆盖掉第一个and。覆盖之后的是:select * from User where age='';

4. suffixOverrides 后缀需要覆盖的内容,一般是最后一个数据的后面符号,如:set值的时候,最后一个值的后面多一个逗号‘,'

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

< insert id= "insertSelective" parameterType= "org.javaboy.vhr.model.Salary" >

        insert into salary

        <trim prefix= "(" suffix= ")" suffixOverrides= "," >

            <if test= "id != null" >

                id,

            </if>

            <if test= "basicSalary != null" >

                basicSalary,

            </if>

            <if test= "bonus != null" >

                bonus,

            </if>

            <if test= "lunchSalary != null" >

                lunchSalary,

            </if>

            <if test= "trafficSalary != null" >

                trafficSalary,

            <if test= "name != null" >

                name ,

            </if>

        </trim>

        <trim prefix= "values (" suffix= ")" suffixOverrides= "," >

            <if test= "id != null" >

                #{id,jdbcType= INTEGER },

            </if>

            <if test= "basicSalary != null" >

                #{basicSalary,jdbcType= INTEGER },

            </if>

            <if test= "bonus != null" >

                #{bonus,jdbcType= INTEGER },

            </if>

            <if test= "lunchSalary != null" >

                #{lunchSalary,jdbcType= INTEGER },

            </if>

            <if test= "trafficSalary != null" >

                #{trafficSalary,jdbcType= INTEGER },

            </if>

             <if test= "name != null" >

                #{ name ,jdbcType= VARCHAR },

            </if>

        </trim>

    </ insert >

加了suffixOverrides=","

结果:

?

1

insert into salary (id,basicSalary,bonus,lunchSalary,trafficSalary, name ) values (#{id},#{basicSalary},#{bonus},#{lunchSalary},#{trafficSalary},#{ name })

不加suffixOverrides=","

结果:

?

1

insert into salary (id,basicSalary,bonus,lunchSalary,trafficSalary, name ,) values (#{id},#{basicSalary},#{bonus},#{lunchSalary},#{trafficSalary},#{ name },)

加了suffixOverrides=","的话,本例中最后一个条件中的逗号[,]会被后缀覆盖掉

mybatis之 trim prefix="(" suffix=")"

1.如下所示

?

1

<trim prefix= "" suffix= "" suffixOverrides= "" prefixOverrides= "" ></trim>

prefix :在trim标签内sql语句加上前缀。

suffix :在trim标签内sql语句加上后缀。

suffixOverrides :指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。

prefixOverrides :指定去除多余的前缀内容

2.下面是一个往购物车表中插入数据的mybatis语句

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

< insert id= "insert" parameterType= "com.tortuousroad.groupon.cart.entity.Cart" >

         insert into cart

         <trim prefix= "(" suffix= ")" suffixOverrides= "," >

             <if test= "id != null" >

                 id,

             </if>

             <if test= "userId != null" >

                 user_id,

             </if>

             <if test= "dealId != null" >

                 deal_id,

             </if>

             <if test= "dealSkuId != null" >

                 deal_sku_id,

             </if>

             <if test= "count != null" >

                 count ,

             </if>

             <if test= "createTime != null" >

                 create_time,

             </if>

             <if test= "updateTime != null" >

                 update_time,

             </if>

         </trim>

         <trim prefix= "values (" suffix= ")" suffixOverrides= "," >

             <if test= "id != null" >

                 #{id,jdbcType= BIGINT },

             </if>

             <if test= "userId != null" >

                 #{userId,jdbcType= BIGINT },

             </if>

             <if test= "dealId != null" >

                 #{dealId,jdbcType= BIGINT },

             </if>

             <if test= "dealSkuId != null" >

                 #{dealSkuId,jdbcType= BIGINT },

             </if>

             <if test= "count != null" >

                 #{ count ,jdbcType= INTEGER },

             </if>

             <if test= "createTime != null" >

                 #{createTime,jdbcType= TIMESTAMP },

             </if>

             <if test= "updateTime != null" >

                 #{updateTime,jdbcType= TIMESTAMP },

             </if>

         </trim>

     </ insert >

?

1

suffixOverrides= ","

执行的sql语句也许是这样的:insert into cart (id,user_id,deal_id,) values(1,2,1,);显然是错误的

指定之后语句就会变成insert into cart (id,user_id,deal_id) values(1,2,1);这样就将[,]去掉了。

前缀也是一个道理这里就不说了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/weixin_49071539/article/details/116309637

查看更多关于mybatis框架xml下trim中的prefix与suffix等标签的用法的详细内容...

  阅读:9次