解决IDEA gradle项目执行main方法时自动运行gradle task问题
使用IDEA创建gradle项目后,执行main方法时会自动运行gradle的一些build task,导致启动很慢,如下图: 解决方法: 打开设置页面,进行如下修改: from:https://zhoujun.blog.csdn.net/article/details/103372893
View DetailsDO-DTO相互转换时的性能优化
一般情况下,DO是用来映射数据库记录的实体类,DTO是用来在网络上传输的实体类。两者的不同除了适用场景不同外还有就是DTO需要实现序列化接口。从DB查询到数据之后,ORM框架会把数据转换成DO对象,通常我们需要再把DO对象转换为DTO对象。同样的,插入数据到DB之前需要将DTO对象转换为DO对象然后交给ORM框架去执行JDBC。 通常用到的转换工具类BeanUtils是通过反射来实现的,实现源码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
public static <T> T convertObject(Object sourceObj, Class<T> targetClz) { if (sourceObj == null) { return null; } if (targetClz == null) { throw new IllegalArgumentException("parameter clz shoud not be null"); } try { Object targetObj = targetClz.newInstance(); BeanUtils.copyProperties(sourceObj, targetObj); return (T) targetObj; } catch (Exception e) { throw new RuntimeException(e); } } private static void copyProperties(Object source, Object target, Class<?> editable, String[] ignoreProperties) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); if (editable != null) { if (!editable.isInstance(target)) { throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); } actualEditable = editable; } PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null; for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } catch (Throwable ex) { throw new FatalBeanException("Could not copy properties from source to target", ex); } } } } } |
也可以通过mapstruct来实现,这种方式是在Mapper接口的包中生成一个对应mapper的实现类,实现类的源码如下。显然这种方式的实现更为普通,看起来没有BeanUtils的实现那么复杂。不过BeanUtils通过反射实现更为通用,可以为各种类型的DTO实现转换。而mapstruct只是帮我们生产了我们不想写的代码。
1 2 3 4 5 6 7 8 9 10 11 12 |
public Task doToDTO(TaskDO taskDO) { if (taskDO == null) { return null; } else { Task task = new Task(); task.setId(taskDO.getId()); //其他字段的set task.setGmtCreate(taskDO.getGmtCreate()); task.setGmtModified(taskDO.getGmtModified()); return task; } } |
对比以上两种方式,显然使用BeanUtils来进行转换时需要写的代码更少,内部的通过反射便可以进行get/set操作。而mapstruct实现上需要写的代码稍微多一点,但是这种方式的性能比通过反射实现更好。下面写一段代码来测试两种方式实现的性能差别。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void testConvert() { System.out.println("####testConvert"); int num = 100000; TaskDO taskDO = new TaskDO(); long start = System.currentTimeMillis(); for (int i = 0; i < num; i ++) { Task task = ObjectConvertor.convertObject(taskDO, Task.class); } System.out.println(System.currentTimeMillis() - start); //--------------------------------------------- start = System.currentTimeMillis(); for (int i = 0; i < num; i ++) { Task task = taskMapper.doToDTO(taskDO); } System.out.println(System.currentTimeMillis() - start); } |
以上测试代码中分别使用两种方式对同一个DO对象进行n次转换,两次转换的耗时统计如下。单位:ms 次数 1 10 100 1000 10000 100000 1000000 10000000 Mapstruct 0 1 1 1 2 4 8 8 BeanUtil 9 7 11 26 114 500 1469 14586 可见当转换数量级增加时,使用BeanUtil的耗时急剧上升,而使用Mapstruct的耗时则保持在比较低的水平。 在一个系统中,ORM对DB的各种操作几乎都会涉及到DO和DTO之间的转换,参考以上表格的统计结果,更推荐使用Mapstruct。 from:https://www.cnblogs.com/umgsai/p/8570652.html
View DetailsJava中的DO、TO、DTO、DAO等含义
1、PO即persistant Object 持久对象: 在O/R 映射(即ORM-ObjectRelationMapping)中出现的概念,通常对应数据模型(数据库),是与数据库汇总的表想影射的java对象,最简单的PO就是对应数据库中某个表中的一条记录,多个记录则用PO的集合。PO中不应该包含任何对数据库的操作。 2、DO即Domain Object 领域对象: 是从现实世界中抽象出来的有形或无形的业务实体。 3、TO即Transfer Object数据传输对象: 不同应用程序之间传输的对象 4、DTO即Data Transfer Object:数据传输对象: 泛指用于展示层与服务层之间的数据传输对象 5、VO即value Object: 通常用于业务层之间的数据传递,和PO一样仅包含数据,但是抽象出的业务对象,可以和表对应,用new 关键字创建,GC回收 6、BO即Business Object 业务对象: 主要是将业务逻辑封装为一个对象,这个对象可以包含一个或多个其他对象,如一个简历中包含教育经历、工作经历、社会关系等,可以将一个教育经历对应一个PO、工作经历对应一个PO、设计关系对应一个PO,然后简历一个对应简历的BO兑现处理简历,每个BO包含这个PO这样处理业务逻辑是,可以针对BO去处理。封装业务逻辑的java对象,通过调用DAO方法,结合PO,VO进行业务操作。 7、POJO即Plain Ordinary Java Object: 简单无规则的java对性,即在一些O/R 映射工具中,能做到维护数据库表记录的PO完全是一个符合Java Bean规范的纯java对象 from:https://blog.csdn.net/weixin_44122921/article/details/85038513
View Details解决 fastjson toJSON方法日期类型字段 由时间戳转换成自定义类型格式的问题 JSON.toJSONStringWithDateFormat
将map集合转为json对象时遇到一个问题。map中 updateTime的value为日期格式如"2001-01-01",在使用 JSONObject.toJSON(map).toString() 的时候, 得到的结果 updateTime 的值为 时间戳 解决方法: 使用fastjson 的 JSON.toJSONStringWithDateFormat(Object,dateformat,SerializerFeature.WriteDateUseDateFormat) 方法即可将时间戳转换为自定义格式类型的值 from:https://blog.csdn.net/qq_39564789/article/details/105176845
View DetailsCentOS7 离线安装MySQL
1.删除原有的mariadb 不然安装报错
1 |
rpm -qa|grep mariadb |
1 |
rpm -e --nodeps mariadb-libs |
2. 下载RPM安装包 在https://dev.mysql.com/downloads/mysql/选择为Red Hat Enterprise Linux 7 / Oracle Linux 7 ,把os的版本选择为all。 直接下载mysql-5.7.21-1.el7.x86_64.rpm-bundle.tar,所有的rpm包都在里面,然后rpm命令安装。
1 2 3 4 5 6 7 8 9 10 11 |
rpm -ivh mysql-community-common-5.7.21-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-5.7.21-1.el7.x86_64.rpm rpm -ivh mysql-community-devel-5.7.21-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-compat-5.7.21-1.el7.x86_64.rpm rpm -ivh mysql-community-client-5.7.21-1.el7.x86_64.rpm rpm -ivh mysql-community-server-5.7.21-1.el7.x86_64.rpm |
至此,mysql5.7所有文件安装完毕,接下来就是开启服务测试了 3. 启动mysql服务 查看mysql服务是否启动
1 |
service mysqld status |
启动服务:
1 |
systemctl start mysqld |
4. 重置root密码 MySQL5.7会在安装后为root用户生成一个随机密码,而不是像以往版本的空密码。 可以安全模式修改root登录密码或者用随机密码登录修改密码。下面用随机密码方式 MySQL为root用户生成的随机密码通过mysqld.log文件可以查找到:
1 |
grep 'temporary password' /var/log/mysqld.log |
5. 修改root用户密码 (MySQL的密码策略比较复杂,过于简单的密码会被拒绝)
1 2 3 4 |
1 mysql -u root -p 2 mysql> Enter password: (输入刚才查询到的随机密码) 3 mysql> SET PASSWORD FOR 'root'@'localhost'= "qaz-123"; 4 mysql> exit |
6. 用root新密码登录
1 |
mysql -u root -pqaz-123 |
如果上面的方式不能修改可以使用下面安全模式修改root: 关闭服务,修改mysql配置文件:
1 2 |
1 systemctl stop mysqld.service 2 vi /etc/my.cnf |
mysqld下面添加skip-grant-tables 保存退出启动服务。
1 |
systemctl start mysqld.service |
1 2 3 4 5 6 7 |
mysql -u root #不用密码直接回车 use mysql update user set authentication_string=password('qaz-123') where user='root' and host='localhost'; flush privileges; exit; vi /etc/my.cnf #把 skip-grant-tables 一句删除保存退出重启mysql服务 systemctl restart mysqld.service |
再次登录即可
1 |
mysql -uroot -pqaz123 |
如果进行操作出现下面的提示:
1 |
You must reset your password using ALTER USER statement before executing this statement. |
就重新设置密码(mysql默认密码策略比较复杂,如果设置简单密码,需修改默认安全策略,可以参考另外一篇文章:MYSQL57密码策略修改)
1 |
set password = password('qaz-123'); |
7.开放3306端口
1 2 3 |
1 mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'qaz-123' WITH GRANT OPTION; 2 mysql>FLUSH PRIVILEGES; 3 mysql>exit; |
开启防火墙mysql 3306端口的外部访问:
1 2 |
1 firewall-cmd --zone=public --add-port=3306/tcp --permanent 2 firewall-cmd --reload |
from:https://www.cnblogs.com/mymelody/p/9253551.html
View Detailscentos7下使用mysql离线安装包安装mysql5.7
服务器环境: centos7 x64 需要安装mysql5.7+ 一、卸载CentOS7系统自带mariadb
1 2 3 4 5 6 7 |
# 查看系统自带的Mariadb [root@CDH-141 ~]# rpm -qa|grep mariadb mariadb-libs-5.5.44-2.el7.centos.x86_64 # 卸载系统自带的Mariadb [root@CDH-141 ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64 # 删除etc目录下的my.cnf [root@CDH-141 ~]# rm /etc/my.cnf |
二、检查mysql是否存在
1 2 3 |
# 检查mysql是否存在 [root@CDH-141 ~]# rpm -qa | grep mysql [root@CDH-141 ~]# |
三、查看用户和组是否存在 1)检查mysql组合用户是否存在
1 2 3 |
# 检查mysql组和用户是否存在,如无则创建 [root@CDH-141 ~]# cat /etc/group | grep mysql [root@CDH-141 ~]# cat /etc/passwd | grep mysql |
# 查询全部用户(只是做记录,没必要执行)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[root@CDH-141 ~]# cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F ":" '{print $1 "|" $3 "1" $4}' | more root|010 sync|510 flume|9921989 hdfs|9911988 zookeeper|9891986 llama|9881985 httpfs|9871984 mapred|9861983 sqoop|9851982 yarn|9841981 kms|9831980 hive|9821979 oozie|9801977 hbase|9781975 impala|9761973 hue|9741971 wlaqzc2018|100111001 [root@CDH-141 mysql]# |
2)若不存在,则创建mysql组和用户
1 2 3 4 5 6 7 8 9 10 11 |
# 创建mysql用户组 [root@CDH-141 ~]# groupadd mysql # 创建一个用户名为mysql的用户,并加入mysql用户组 [root@CDH-141 ~]# useradd -g mysql mysql # 制定password 为111111 [root@CDH-141 ~]# passwd mysql Changing password for user mysql. New password: BAD PASSWORD: The password is a palindrome Retype new password: passwd: all authentication tokens updated successfully. |
四、下载mysql离线安装包tar文件 官网下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 版本选择,可以选择一下两种方式: 1)使用Red Hat Enterprise Linux Select Version:5.7.25 Select Operating System:Red Hat Enterprise Linux / Oracle Linux Select OS Version:Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit) 列表中下载: Compressed TAR Archive:(mysql-5.7.25-el7-x86_64.tar.gz) 2)使用Linux – Generic Select Version:5.7.25 Select Operating System:Linux – Generic Select OS Version:Linux – Generic (glibc 2.12) (x86, 64-bit) 列表中下载: Compressed TAR Archive:(mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz)【本文中使用的是这个版本】 注意:上边两种方式找mysql离线安装包的方式都可以。 五、上传第四步下载的mysql TAR包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 进入/usr/local/文件夹 [root@CDH-141 ~]# cd /usr/local/ # 上传mysql TAR包 [root@CDH-141 local]# rz # 解压mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz [root@CDH-141 local]# ls bin full-path-to-mysql-VERSION-OS include lib64 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz share etc games lib libexec sbin src [root@CDH-141 local]# tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz mysql-5.7.25-lin ... mysql-5.7.25-linux-glibc2.12-x86_64/share/install_rewriter.sql mysql-5.7.25-linux-glibc2.12-x86_64/share/uninstall_rewriter.sql mysql-5.7.25-linux-glibc2.12-x86_64/support-files/magic mysql-5.7.25-linux-glibc2.12-x86_64/support-files/mysql.server mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_BIN mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_SRC [root@CDH-141 local]# ls bin full-path-to-mysql-VERSION-OS include lib64 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz share etc games lib libexec mysql-5.7.25-linux-glibc2.12-x86_64 sbin src # 进入/usr/local下,修改为mysql [root@CDH-141 local]# mv mysql-5.7.25-linux-glibc2.12-x86_64 mysql [root@CDH-141 local]# ls bin etc full-path-to-mysql-VERSION-OS games include lib lib64 libexec mysql mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz sbin share src |
六、更改所属的组和用户
1 2 3 4 5 6 7 |
# 更改所属的组和用户 [root@CDH-141 ~]# cd /usr/local/ [root@CDH-141 local]# chown -R mysql mysql/ [root@CDH-141 local]# chgrp -R mysql mysql/ [root@CDH-141 local]# cd mysql/ [root@CDH-141 mysql]# mkdir data [root@CDH-141 mysql]# chown -R mysql:mysql data |
七、在/etc下创建my.cnf文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 进入/usr/local/mysql文件夹下 [root@CDH-141 ~]# cd /usr/local/mysql # 创建my.cnf文件 [root@CDH-141 mysql]# touch my.cnf #或者cd ''>my.conf # 编辑my.cnf [root@CDH-141 mysql]# vi my.conf [mysql] socket=/var/lib/mysql/mysql.sock # set mysql client default chararter default-character-set=utf8 [mysqld] socket=/var/lib/mysql/mysql.sock # set mysql server port port = 3323 #默认是3306,这里发现3306已经被占用,因此防止这种情况发生,可以避免使用3306mysql默认端口 # set mysql install base dir basedir=/usr/local/mysql # set the data store dir datadir=/usr/local/mysql/data # set the number of allow max connnection max_connections=200 # set server charactre default encoding character-set-server=utf8 # the storage engine default-storage-engine=INNODB lower_case_table_names=1 max_allowed_packet=16M explicit_defaults_for_timestamp=true [mysql.server] user=mysql basedir=/usr/local/mysql [root@CDH-141 mysql]# |
八、进入mysql文件夹,并安装mysql
1 2 3 4 5 6 7 |
# 进入mysql [root@CDH-141 local]# cd /usr/local/mysql # 安装mysql [root@CDH-141 mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 2019-03-08 18:11:07 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2019-03-08 18:11:24 [WARNING] The bootstrap log isn't empty: 2019-03-08 18:11:24 [WARNING] 2019-03-08T10:11:07.208602Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead |
设置文件及目录权限:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[root@CDH-141 mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld [root@CDH-141 mysql]# chown 777 my.cnf [root@CDH-141 mysql]# ls bin COPYING data docs include lib man my.cnf README share support-files [root@CDH-141 mysql]# ls -l total 60 drwxr-xr-x 2 root root 4096 Mar 8 15:56 bin -rw-r--r-- 1 7161 31415 17987 Dec 21 18:39 COPYING drwxr-x--- 5 mysql mysql 4096 Mar 8 16:21 data drwxr-xr-x 2 root root 4096 Mar 8 15:56 docs drwxr-xr-x 3 root root 4096 Mar 8 15:56 include drwxr-xr-x 5 root root 4096 Mar 8 15:56 lib drwxr-xr-x 4 root root 4096 Mar 8 15:56 man -rw-r--r-- 1 777 root 516 Mar 8 16:19 my.cnf -rw-r--r-- 1 7161 31415 2478 Dec 21 18:39 README drwxr-xr-x 28 root root 4096 Mar 8 15:56 share drwxr-xr-x 2 root root 4096 Mar 8 15:56 support-files [root@CDH-141 mysql]# chmod +x /etc/init.d/mysqld [root@CDH-141 mysql]# [root@CDH-141 mysql]# mkdir data [root@CDH-141 mysql]# [root@CDH-141 mysql]# chown -R mysql:mysql data [root@CDH-141 mysql]# |
九、启动mysql
1 2 3 4 5 6 |
# 启动mysql [root@CDH-141 mysql]# /etc/init.d/mysqld restart MySQL server PID file could not be found![FAILED] Starting MySQL.Logging to '/usr/local/mysql/data/CDH-141.err'. ..The server quit without updating PID file (/usr/local/mysql/data/CDH-141.pid).[FAILED] [root@CDH-141 mysql]# |
出现错误,解决方案如下: […]
View DetailsJAVA中使用alibaba fastjson实现JSONObject、Object、Json字符串的转换
Object转JSON字符串: String jsonStr = JSONObject.toJSONString(object); JSON字符串转JSONObject: JSONObject jsonObject = JSONObject.parseObject(jsonStr); JSON字符串转Object对象 T t = JSON.parseObject(jsonStr,T.class); —–注:JSON字符串是有格式要求的,必须为键值对形式,不是任意的字符串。—– ——————— 作者:KnifeBlade 来源:CSDN 原文:https://blog.csdn.net/qq_29468573/article/details/82190005
View Details使用fastjson时,对象转json遇到的首字母大小写问题
在使用fastjson进行数据类型转换时发现,pojo类里面的属性首字母大写,在转成json之后,变成了小写。导致数据存储一直有问题。 转换之后结果为: 导致数据存储失败。(使用json传值进行持久化操作。) 解决方法: 在pojo类的属性get方法上加上你需要的key。 这样转换的json,对应的key就改变为你注解对应的属性名。 from:https://blog.csdn.net/cuuuc233/article/details/80983486
View Detailsfastjson包JSONObject.toJSON导致部分属性丢失问题
JSONObject.toJSONString(Object object, SerializerFeature… features) Fastjson的SerializerFeature序列化属性 QuoteFieldNames———-输出key时是否使用双引号,默认为true WriteMapNullValue——–是否输出值为null的字段,默认为false WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非nul
1 2 3 4 5 6 7 8 9 |
Map < String , Object > jsonMap = new HashMap< String , Object>(); jsonMap.put("a",1); jsonMap.put("b",""); jsonMap.put("c",null); jsonMap.put("d","dddddddddd"); String str = JSONObject.toJSONString(jsonMap,SerializerFeature.WriteMapNullValue); System.out.println(str); //输出结果:{"a":1,"b":"","c":null,"d":"dddddddddd"} |
from:http://www.voidcn.com/article/p-ebkhpvlq-bqh.html
View DetailsSpringboot — 用更优雅的方式发HTTP请求(RestTemplate详解)
RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。
我之前的HTTP开发是用apache的HttpClient开发,代码复杂,还得操心资源回收等。代码很复杂,冗余代码多,稍微截个图,这是我封装好的一个post请求工具:
本教程将带领大家实现Spring生态内RestTemplate的Get请求和Post请求还有exchange指定请求类型的实践和RestTemplate核心方法源码的分析,看完你就会用优雅的方式来发HTTP请求。
View Details