您的位置:首页 > 理论基础 > 计算机网络

一周乱弹(1,HttpReques 获取请求地址2,去掉字符串中首尾空格及换行符、回车符等3,计算代码运行时间4,StringEscapeUtils对字符串进行各种转义与反转义5,分表查询记录总数)

2018-01-25 20:30 821 查看
1,HttpRequest 获取请求地址:

String url=”http://” + request.getServerName() //服务器地址

+ “:”

+ request.getServerPort() //端口号

+ request.getRequestURI();

2,去掉字符串中首尾空格及换行符、回车符等 。使用java.util.regex中Pattern及Matcher进行处理

public static String replaceBlank(String str) {

String result = “”;

if (str!=null) {

Pattern p = compile(“^\s*|\s*$|\t|\r|\n”);//去掉字符串中首尾空格及换行符、回车符等。如想去掉所有空格使用这个 : Pattern.compile(“\s*|\t|\r|\n”);

Matcher m = p.matcher(str);

result = m.replaceAll(“”);

}

return result;

}

3,计算代码运行时间

long startTime=System.currentTimeMillis(); //获取开始时间

doSomeThing(); //测试的代码段

long endTime=System.currentTimeMillis(); //获取结束时间

System.out.println(“程序运行时间: “+(end-start)+”ms”);

4,StringEscapeUtils对字符串进行各种转义与反转义(org.apache.commons.lang)

5,分表查询记录总数,count(),sum()

select sum(count) as num from (select count() as count from {$table} where {$where} union all select count() as count from {$table} where {$where}) as total

6,mybatis #与$区别

简单说#{}是经过预编译的,是安全的,而${}是未经过预编译的,仅仅是取变量的值,是非安全的,存在sql注入。

只能${}的情况,order by、like 语句只能用${}了,用#{}会多个’ ‘导致sql语句失效.此外动态拼接sql也要用${}

用#{}mybatis会默认加上’’ 单引号
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐