1:动态SQL遇到的坑,先看下面OGNL表达式的说明。

Any object can be used where a boolean is required. OGNL interprets objects as booleans like this:

  1. If the object is a Boolean, its value is extracted and returned;

  2. If the object is a Number, its double-precision floating-point value is compared with zero; non-zero is treated as true, zero as false;

  3. If the object is a Character, its boolean value is true if and only if its char value is non-zero;

  4. Otherwise, its boolean value is true if and only if it is non-null.

如果对象是一个Number类型,值为0时将被解析为false,否则为true,浮点型0.00也是如此。OGNL对于boolean的定义和JavaScript有点像,即'' == 0 == false。这也就不难理解<if test="status != null and status !=''">and status = #{status}</if>当status=0时出现的问题了,显然0!=''是不成立的,导致表达式的值为false。

将表达式修改为<if test="status != null">and status = #{status}</if>该问题便迎刃而解。该问题的根源还是来自编码的不规范,只有String类型才需要判断是否!='',其他类型完全没有这个必要,可能是开发人员为了省事直接复制上一行拿过来改一改或是所使用的MyBatis生成工具不严谨导致该问题的发生。

这里有必要再提一个“坑”,如果你有类似于String str ="A"; <if test="str!= null and str == 'A'">这样的写法时,你要小心了。因为单引号内如果为单个字符时,OGNL将会识别为Java 中的 char类型,显然String 类型与char类型做==运算会返回false,从而导致表达式不成立。解决方法很简单,修改为<if test='str!= null and str == "A"'>即可。

2:集合中移除某些无用的数据的操作。

  //正确写法
List<Stu1> list = new ArrayList<>();
list.add(new Stu1("zhangsan", 21));
list.add(new Stu1("lisi", 22));
list.add(new Stu1("zhangsan", 26));
Iterator<Stu1> iterator = list.iterator();
while (iterator.hasNext()) {
Stu1 stu1 = iterator.next();
if (stu1.getName().equals("zhangsan1")) {
iterator.remove();
}
}
//错误写法,用foreach循环遍历集合,我就不写了.这样可能会报异常的。
// 开发中移除集合中的元素的时候,用Iterator来遍历集合。

3:字符串变量和字符串常量equals的时候将字符串常量写在前面

  String str = "123" ;
if(str.equals("123")){ }
// 建议修改为这样主要是可以避免空指针异常。
String str = "123" ;
if("123".equals(str)){ }

4:HashMap用对象作为key的时候重新equal和hashCode,如果不重写hashcode方法get的结果将为null。

如下图是怎样重写equal和hashCode的方法。

注意:平常用对象判断是否相同的时候,一般也会重写这个对象的equal和hashCode的方法,一般的开发工具也都有重写这两个方法的快捷键。

5:用最有效的方式来遍历map集合

如下,数据量大的时候用Iterator遍历map的效率较高:

 Map<String, String> hm = new HashMap<>();
hm.put("1", "aa");
hm.put("2", "bb");
Set<Map.Entry<String, String>> entries = hm.entrySet();
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> map = iterator.next();
System.out.println("key值" + map.getKey());
System.out.println("value值" + map.getValue());
}

6:Oracle查询优化:

选择最有效率的表名顺序:ORACLE的解析器按照从右到左顺序处理from字句中的表名,from字句中写在最后的表(基础表)将最先被处理,在from字句中包含多个表的情况下,必须选择记录数最少的表作为基础表,如果有3个以上的表连接查询,那就需要选择交叉表作为基础表,交叉表就是那个被其他表引用的表。

最新文章

  1. sqlserver 游标的使用
  2. Android新组件CardView
  3. jq实现全选、全不选、反选
  4. C# 根据前台校验的值,决定是否执行后台方法
  5. 【翻译十四】java-并发之保护块儿
  6. JS重点特性——闭包详解
  7. Oracle 查看表空间的大小及使用情况sql语句
  8. POJ3628 Bookshelf 2(01背包+dfs)
  9. PHP单引号和双引号的区别
  10. java面试每日一题12
  11. form的验证用法
  12. c#线程的几种启动方法
  13. 闭包(closure)
  14. CCT之CAMERA TUNNING调试学习总结
  15. 04bootstrap_表单
  16. 洛谷P2722总分题解
  17. 【Java入门提高篇】Day29 Java容器类详解(十一)LinkedHashSet详解
  18. python类的组合
  19. 使用in ()进行批量删除
  20. idea中dependencies中总是有红色波浪线(缺少dependency)的解决办法

热门文章

  1. 【SQL基础】基础查询:所有列、指定列、去重、限制行数、改名
  2. MYSQL-INNODB索引构成详解
  3. 源码解读之TypeScript类型覆盖检测工具type-coverage
  4. 基于.NetCore开发博客项目 StarBlog - (24) 统一接口数据返回格式
  5. Spring IOC官方文档学习笔记(三)之依赖项
  6. 一个小而美的 C 语言项目
  7. P8701 [蓝桥杯 2019 国 B] 第八大奇迹
  8. angular---路由传参数
  9. A+B Problem C++
  10. VUEX 使用学习五 : getter