MyBatis注解select in參數


/**
 * 
 * @param ids '1,2,3'
 * @return
 */

@Select("select * from user_info where id in (${ids})")

List<UserInfo> getUserbyIds(@Param("ids")String ids);

 

參數需要使用${}來引用,#{}不能識別。【這個方案貌似不起作用】

 

 

 

------------xml文件寫法

 

0

DELETE FROM DEMO 
WHERE ID in 
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
        #{item} 
    </foreach>

 

 

---------------SQLProvider

public class Test
    {
        @SuppressWarnings("unchecked")
        public static String getTestQuery(Map<String, Object> params)
        {

            List<String> idList = (List<String>) params.get("idList");

            StringBuilder sql = new StringBuilder();

            sql.append("SELECT * FROM blog WHERE id in (");
            for (String id : idList)
            {
                if (idList.indexOf(id) > 0)
                    sql.append(",");

                sql.append("'").append(id).append("'");
            }
            sql.append(")");

            return sql.toString();
        }

        public interface TestMapper
        {
            @SelectProvider(type = Test.class, method = "getTestQuery")
List<Blog> selectBlogs(@Param("idList") int[] ids);
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM