反例:java
// BigDecimal 反例 BigDecimal decimal = new BigDecimal(11.801D); System.out.println("decimal:"+decimal);
正例:正则表达式
// BigDecimal 正例 BigDecimal bDecimal = BigDecimal.valueOf(11.801D); System.out.print("bDecimal:"+bDecimal);
执行结果:数据库
decimal:11.8010000000000001563194018672220408916473388671875 bDecimal:11.801
使用字符串String 的plit 方法时,传入的分隔字符串是正则表达式,则部分关键字(好比 .[]()| 等)须要转义。性能
2.1 优化
String str = "small.sun.shine"; String[] strSplit = str.split("."); // .须要转义,反例 String[] strSplit1 = str.split("\\.");// 正例 String[] strSplit2 = str.split("[.]");// 正例 System.out.println("strSplit:" + Arrays.toString(strSplit)); System.out.println("strSplit1:" + Arrays.toString(strSplit1)); System.out.println("strSplit2:" + Arrays.toString(strSplit2));
执行结果:orm
strSplit:[] strSplit1:[small, sun, shine] strSplit2:[small, sun, shine]
2.2blog
String str = "small|sun|shine"; String[] strSplit = str.split("|"); // | 须要转义,反例 String[] strSplit1 = str.split("\\|");// 正例 String[] strSplit2 = str.split("[|]");// 正例 System.out.println("strSplit:" + Arrays.toString(strSplit)); System.out.println("strSplit1:" + Arrays.toString(strSplit1)); System.out.println("strSplit2:" + Arrays.toString(strSplit2));
执行结果:索引
strSplit:[] strSplit1:[small, sun, shine] strSplit2:[small, sun, shine]
当循环中只须要获取Map 的主键key时,迭代keySet() 是正确的;可是,当须要主键key 和取值value 时,迭代entrySet() 才是更高效的作法,其比先迭代keySet() 后再去经过get 取值性能更佳。ci
//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(); }
当遇到多个查询条件,使用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>