myBatis注解sql书写注意

12 篇文章 0 订阅
9 篇文章 0 订阅

(1)在使用<where></where>,<if></if>等标签符号时一定要加<script></script>标签,其他情况可加可不加。
(2)里面牵涉的大于、小于、大于等于、小于等于符号要分别使用&gt;、&lt;、&gt;=、&lt;=表示否则会出错。
(3)换行结尾可用逗号或者加号

 @Insert({
            "<script>",
            "insert into phone (",
            "id," ,
            "phone," ,
            "phone_type, " ,
            "insert_time," ,
            "insert_user ) (" ,
            "<foreach collection='phoneInfos' item='phone' index='index' separator='union all'>",
            "   (select #{phone.id,jdbcType=VARCHAR},  " ,
            "  #{phone.phone,jdbcType=VARCHAR},  " ,
            "  #{phone.phone_type,jdbcType=VARCHAR},  " ,
            "  to_date(#{phone.insert_time,jdbcType=VARCHAR},'yyyy-mm-dd hh24:mi:ss'),  " ,
            "  #{phone.insert_user,jdbcType=VARCHAR} from dual)" ,
            "</foreach> )",
            "</script>"
    })
    int betchInsertPhone(@Param("phoneInfos")List<PhoneInfo> phoneInfos);
	
    @Select(" <script> " +
            " select count(*) from phone t " +
            " <where>"+
            " <if test='phone !=null and phone!=\"\"'>" +
            " and t.phone like '%${phone}%'" +
            " </if> " +
            " <if test='startDate !=null and startDate!=\"\"'> " +
            " and t.insert_time &gt;= to_date('${startDate}','yyyy-mm-dd hh24:mi:ss') " +
            " </if> " +
            " <if test='endDate !=null and endDate!=\"\"'> " +
            " and t.insert_time &lt;= to_date('${endDate}','yyyy-mm-dd hh24:mi:ss') " +
            " </if>" +
            " </where>" +
             " </script>")
    int countPhoneManager(@Param("startDate") String startDate,@Param("endDate") String endDate,@Param("phone") String phone);

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 好的,关于mybatis注解方式写sql in,可以使用@Select注解,并在其中使用SQL语句的动态参数标签进行in操作。具体的示例代码如下: @Select("SELECT * FROM table WHERE id IN (#{idList})") List<Table> findByIds(@Param("idList") List<Long> idList); 其中,List<Long> idList 为入参,传入多个id值,可以根据实际情况进行调整。希望以上内容能够帮到您,如果还有其他相关问题,欢迎继续咨询我。 ### 回答2: Mybatis注解方式写sql in可以通过使用@Select注解,并在注解SQL语句中使用动态SQL的foreach标签来实现。 具体步骤如下: 1. 在需要执行in查询的Mapper接口中,使用@Select注解声明SQL语句。例如: ``` @Select("SELECT * FROM table_name WHERE column_name IN (#{ids})") List<Entity> selectByIds(@Param("ids") List<Integer> ids); ``` 2. 在注解SQL语句中,使用动态SQL的foreach标签来循环遍历传入的参数列表。例如: ``` @Select("<script>" + "SELECT * FROM table_name" + "WHERE column_name IN" + "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" + "#{item}" + "</foreach>" + "</script>") List<Entity> selectByids(@Param("ids") List<Integer> ids); ``` 其中,item代表集合中的每个对象,index代表循环的下标。 3. 在调用Mapper接口的地方传入参数列表即可实现in查询。例如: ``` List<Integer> ids = Arrays.asList(1, 2, 3); List<Entity> result = mapper.selectByids(ids); ``` 通过以上步骤,就可以使用Mybatis注解方式实现sql in查询。 ### 回答3: MyBatis注解方式写SQL in时,可以通过@Select注解来实现。使用注解的好处是可以直接在Java代码中书写SQL语句,方便快捷。 首先,在需要执行in查询的Mapper接口中,使用@Select注解定义SQL语句。例如: @Select("SELECT * FROM table_name WHERE column_name IN (${ids})") List<TableName> findDataByIds(@Param("ids") String ids); 其中,@Select表示该方法执行的是一个查询操作,括号内的内容为SQL语句。"SELECT * FROM table_name WHERE column_name IN (${ids})"表示查询表名为table_name中指定列名为column_name的数据,数据的值在ids参数中传入。ids参数使用${}包裹,表示该参数在SQL语句中的占位符。 接下来,在调用该方法时,需要将实际的参数传入,例如: String ids = "1,2,3,4,5"; List<TableName> data = mapper.findDataByIds(ids); 在查询时,将需要查询的数据的id以字符串形式传入,多个id之间用逗号分隔。如上述示例中,ids为"1,2,3,4,5"。 最后,执行查询后,将查询结果存入List集合中,可以对查询结果进行进一步的处理和使用。 需要注意的是,使用注解方式写SQL in时,由于SQL语句直接嵌入到Java代码中,所以要确保传入的参数值是安全可靠的,以防止SQL注入攻击的发生。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郝少

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值