首页 > IT职业 > IT职场 > 代码怎么写才能被面试官看上?小编带你瞧瞧
当遇到多个查询条件,使用where 1=1 可以很方便的解决我们的问题,但是这样很可能会造成非常大的性能损失,因为添加了 “where 1=1 ”的过滤条件之后,数据库系统就无法使用索引等查询优化策略,数据库系统将会被迫对每行数据进行扫描(即全表扫描) 以比较此行是否满足过滤条件,当表中的数据量较大时查询速度会非常慢;此外,还会存在SQL 注入的风险。
<select id="queryBookInfo" parameterType="com.tjt.platform.entity.BookInfo" resultType="java.lang.Integer">select count(*) from t_rule_BookInfo t where 1=1<if test="title !=null and title !='' ">AND title = #{title} </if><if test="author !=null and author !='' ">AND author = #{author}</if></select>
<select id="queryBookInfo" parameterType="com.tjt.platform.entity.BookInfo" resultType="java.lang.Integer">select count(*) from t_rule_BookInfo t<where><if test="title !=null and title !='' ">title = #{title} </if><if test="author !=null and author !='' ">AND author = #{author}</if></where></select>
当循环中只需要获取Map 的主键key时,迭代keySet() 是正确的;但是,当需要主键key 和取值value 时,迭代entrySet() 才是更高效的做法,其比先迭代keySet() 后再去通过get 取值性能更佳。
//Map 获取value 反例:HashMap<String, String> map = new HashMap<>();for (String key : map.keySet()){String value = map.get(key);}
//Map 获取key & value 正例:HashMap<String, String> map = new HashMap<>();for (Map.Entry<String,String> entry : map.entrySet()){String key = entry.getKey();String value = entry.getValue();
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69984164/viewspace-2731395/ ,如需转载,请注明出处,否则将追究法律责任。