遇到過的那些坑

###1.事務要慎用java

  • 更新單條記錄不須要使用事務
  • 避免在事務中作複雜的業務處理,事務中最好只包括數據更新操做
  • 避免使用全局攔截式事務,容易產生長事務
  • 事務只對同一鏈接生效,多數據源事務須要額外考慮

###2.關於特殊字符的處理mysql

// 普通字符, 數字字母空格反斜槓
String s6 = "ZHA/NG SAN";
System.out.println(Pattern.compile("[0-9a-zA-Z_ /]+").matcher(s6).matches());
System.out.println(Pattern.compile("([0-9a-zA-Z_]|[ /])+").matcher(s6).matches());
// 普通字符, 數字字母下劃線
String s = "123abcAZY_";
System.out.println(Pattern.compile("\\w+").matcher(s).matches());
System.out.println(Pattern.compile("[0-9a-zA-Z_]+").matcher(s).matches());
// 非普通字符
String s2 = "@$%^中國";
System.out.println(Pattern.compile("\\W+").matcher(s2).matches());
System.out.println(Pattern.compile("[^0-9a-zA-Z_]+").matcher(s2).matches());
// 中文字符, 不包括標點
String s3 = "中國";
System.out.println(Pattern.compile("[\\u4e00-\\u9fa5]+").matcher(s3).matches());
// 其它特殊字符
String s4 = "\u00b7\u3002\uff0c\uff0e"; // ·。,.
System.out.println(Pattern.compile("[^0-9a-zA-Z_\\u4e00-\\u9fa5]+").matcher(s4).matches());
// 半角字符, 其中·是半角標點
String s5 = "123abcAZY_?!@#!@·¹¸";//
System.out.println(Pattern.compile("[\\x00-\\xff]+").matcher(s5).matches());

3.空值判斷

正常性況下, 空值判斷有以下幾種狀況sql

  1. null
  2. 空字符串""
  3. 空白字符串" "

java 語言判斷方法apache

System.out.println("".length());
System.out.println(" ".length());
System.out.println(" ".trim().length());
System.out.println("\b\b".trim().length());
System.out.println("\b\t".trim().length());

org.apache.commons.lang3.StringUtils.isBlank("");
org.apache.commons.lang3.StringUtils.isEmpty("");
  • oracle 處理方法, oracle 裏 null 等價於 空字符串""
select nvl(null, '') from dual;
select length(' ') from dual;
select * from dcs_h where ' ' = '   ' and rownum < 2;
  • mysql 處理方法, oracle 裏 空字符串"" 查詢等價於 空白字符串""
select ifnull(null, '');
select length('  ');
select * from dcs_h where '' = '  ' limit 1;
  • sqlserver 處理方法, oracle 裏 空字符串"" 查詢等價於 空白字符串""
select isnull(null, 1);
select len('  ');
select top 1 * from dcs_h where '' = '   ';
相關文章
相關標籤/搜索