资讯 小学 初中 高中 语言 会计职称 学历提升 法考 计算机考试 医护考试 建工考试 教育百科
栏目分类:
子分类:
返回
空麓网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
空麓网 > 计算机考试 > 软件开发 > 后端开发 > Java

【JAVA代码编写小技巧v1】

Java 更新时间: 发布时间: 计算机考试归档 最新发布

【JAVA代码编写小技巧v1】

文章目录

  • 1.耗时权限和时间
  • 2.String&集合&对象判空
  • 3.启动和重启docker
  • 4.Map&list&string之间转换
  • 5.简单是sql操作

1.耗时权限和时间

StopWatch stopWatch = new StopWatch("分页查询之权限与查库,开始计时"):
stopWatch.start("权限耗时");
........
stopWatch.stop();
stopWatch.start("查询耗时");
.....
stopWatch.stop();
System.out.println(stopWatch.prettyPrint())

long startMillis = System.currentTimeMillis();
....
long endNMillis = System.currentTimeMillis();
System.out.println("耗时" + (endNMillis - startMillis );

2.String&集合&对象判空

1.String类型判空
!StringUtils.isEmpty()

2.对象类型判空
ObjectUtils.isEmpty();

3.集合类型判空
CollectionUtils.isEmpty();

4.入参判断不为空
Assert.notNull(id, "带宽模板id不能为空!");

3.启动和重启docker

systemctl start docker  启动
systemctl restart docker 重启

4.Map&list&string之间转换

9.Map转换成对象(添加依赖)
String mapString = JSONObject.toJSONString(paramMap); --先转换成String
JSONObject.parseObject(mapString, Hospital.class);  --再转换成对象

10.String转List
String str = "a, b, c"; 
List list = Arrays.asList(str);

11.List集合转String
Strings= StringUtils.join(list,",");

12.List转Map
第一种:
遍历集合进行put到map
第二种(lambda表达式)
Map map2 = studentList.stream().collect(Collectors.toMap(Student::getId,Student::getName,(key1,key2)->key2));

13.Map转List
List list3 = map.keySet().stream().collect(Collectors.toList()); 
list3.forEach(System.out ::println);

List list4 = map.values().stream().collect(Collectors.toList()); 
list4.forEach(System.out ::println);

14.String转JSON对象
String result =“123456”
JSONObject jsonObject = JSON.parseObject(result);

15.Integer转换String	
Integer a = 2;
String str = String.valueOf(a);

16.String转换Integer
String a = "111";
Integer b = Integer.valueOf(a); 

5.简单是sql操作

mysql登录命令: mysql -h ip -P 端口 -u 用户名 -p
查看数据库版本: mysql --version 或者mysql -V
显示所有数据库:show databases;
进入指定的库:use 库名;
显示当前库中所有的表:show tables;
查看其他库中所有的表:show tables from 库名;
查看表的创建语句:show create table 表名;
查看表结构:desc 表名;
查看当前所在库:select database();
建库通用的写法
drop database if exists 旧库名;
create database 新库名;
创建表: create table 表名()
删除表: drop table [if exists] 表名;
修改表名: alter table 表名 rename [to] 新表名;
添加列: alter table 表名 add column 列名 类型 [列约束];
修改列: alter table 表名 modify column 列名 新类型 [约束];
       或者 alter table 表名 change column 列名 新列名 新类型 [约束];
删除列: alter table 表名 drop column 列名;
插入单行: insert into 表名[(字段,字段)] values (值,值);或insert into 表名 set 字段 = 值,字段 = 值;
批量插入: insert into 表名 [(字段,字段)] values (值,值),(值,值),(值,值);
单表更新: update 表名 [[as] 别名] set [别名.]字段 = 值,[别名.]字段 = 值 [where条件];
多表更新: update 表1 [[as] 别名1],表名2 [[as] 别名2]set [别名.]字段 = 值,[别名.]字段 = 值[where条件]
条件查询: select 列名 from 表名 where 列 运算符 值
条件查询运算符: select 列名 from 表名 where 列 = 值;select 列名 from 表名 where 列 != 值;
逻辑查询运算符: 
select 列名 from 表名 where 条件1 and 条件2;
select 列名 from 表名 where 条件1 or 条件2;
like(模糊查询): select 列名 from 表名 where 列 like pattern;
BETWEEN AND(区间查询): select 列名 from 表名 where 列名 between 值1 and 值2;
IN查询: select 列名 from 表名 where 字段 in (值1,值2,值3,值4);select 列名 from 表名 where 字段 not in (值1,值2,值3,值4);
IS NULL(返回值为空的记录): select 列名 from 表名 where 列 is null;select 列名 from 表名 where 列 is not null;
排序查询(order by): select 字段名 from 表名 order by 字段1 [asc|desc],字段2 [asc|desc];
获取前n行记录: select 列 from 表 limit 0,n;或者select 列 from 表 limit n;
获取排名第n到m的记录: select 列 from 表 limit n-1,m-n+1;
单字段分组: SELECt user_id 用户id, COUNT(id) 下单数量 FROM t_order GROUP BY user_id;
多字段分组: SELECt user_id 用户id, the_year 年份, COUNT(id) 下单数量 FROM t_order GROUP BY user_id , the_year;
分组前筛选数据: SELECt user_id 用户id, COUNT(id) 下单数量 FROM t_order t WHERe t.the_year = 2018 GROUP BY user_id;
分组后筛选数据: SELECt user_id 用户id, COUNT(id) 下单数量 FROM t_order t WHERe t.the_year = 2018 GROUP BY user_id HAVINg count(id)>=2;
分组后排序: SELECt user_id 用户id, max(price) 最大金额 FROM t_order t GROUP BY user_id ORDER BY 最大金额 desc;
分组排序登协作:SELECt user_id 用户id, COUNT(id) 下单数量 FROM t_order t WHERe t.the_year = 2018 GROUP BY user_id HAVINg count(id)>=2 ORDER BY 下单数量 DESC LIMIT 1;
左连接: select 列 from 主表 left join 从表 on 连接条件;
转载请注明:文章转载自 http://www.konglu.com/
本文地址:http://www.konglu.com/it/1093653.html
免责声明:

我们致力于保护作者版权,注重分享,被刊用文章【【JAVA代码编写小技巧v1】】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理,本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2023 成都空麓科技有限公司

ICP备案号:蜀ICP备2023000828号-2