Maven构建项目

Maven常见的打包方式有:

  • jar
    jar工程,很显然就是pom工程的子工程,由pom工程来管理。
  • war
    war工程是一个web工程,是可以直接放到tomcat下运行的工程。
  • pom
    pom工程一般都是父工程,管理jar包的版本、maven插件的版本、统一的依赖管理,它是一个聚合工程。其实说白了它只有一个pom.xml文件,一般是作为父工程出现的,只是定义了一些依赖、插件、还有一些版本号等等。

使用maven的好处

  • 依赖管理、jar包、工程之间的依赖。
    Maven定义了软件开发的整套流程体系,并进行了封装,开发人员只需要指定项目的构建流程,无需针对每个流程编写自己的构建脚本。
  • 项目构建。实现项目的一步构建。
    除了项目构建,Maven最核心的功能是软件包的依赖管理,能够自动分析项目所需要的依赖软件包,并到Maven中心仓库去下载。
    管理Jar包的依赖。
    管理工程之间的依赖关系,即可使用Maven依赖其他的工程。
  • 工程聚合、继承、依赖。

构建父工程

父工程应该是一个pom工程。在父工程中定义依赖的jar包的版本信息。Maven插件的版本。即其它项目通过依赖父工程项目,来统一版本号及其他信息。

聚合

该项目中可以聚合几个其它的项目,然后打包成一个war包。

ES6与JavaScript之间的关系

挺迷惑的,不过感觉可以粗浅地理解ES6是一种标准,JavaScript是ES6的一种实现。js与node.jsJS是由ES(ECMAScript)、DOM(浏览器文档对象)、BOM(浏览器对象模型)组成。其中Node.Js就只有ES,目前浏览器比较流行的版本就是ES6(ES2015),老浏览器的版本
阅读更多

MySQL中的常用关键字

很久不用MySQL,感觉又是一个新的玩意儿了,写起SQL语句来感觉好陌生,确实是很久了!

distinct

查询出某个字段不重复的记录。可用distinct来返回不重复字段的条数count(distinct id)

limit

记得这个可以用来做分页。它后面可以接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目

1
2
3
4
5
6
7
8
//初始记录行的偏移量是 0(而不是 1):
mysql> SELECT * FROM table LIMIT 5,10; //检索记录行6-15

//为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1
mysql> SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last

//如果只给定一个参数,它表示返回最大的记录行数目。换句话说,LIMIT n 等价于 LIMIT 0,n:
mysql> SELECT * FROM table LIMIT 5; //检索前 5 个记录行

limit的效率高?

常说的Limit的执行效率高,是对于一种特定条件下来说的:即数据库的数量很大,但是只需要查询一部分数据的情况。高效率的原理是:避免全表扫描,提高查询效率。比如:每个用户的email是唯一的,如果用户使用email作为用户名登陆的话,就需要查询出email对应的一条记录。
SELECT * FROM t_user WHERE email=?;
上面的语句实现了查询email对应的一条用户信息,但是由于email这一列没有加索引,会导致全表扫描,效率会很低。
SELECT * FROM t_user WHERE email=? LIMIT 1;
加上LIMIT 1,只要找到了对应的一条记录,就不会继续向下扫描了,效率会大大提高。

limit的效率低?

在一种情况下,使用limit效率低,那就是:只使用limit来查询语句,并且偏移量特别大的情况。做以下实验:
语句1:
select * from table limit 150000,1000;
语句2:
select * from table while id>=150000 limit 1000;
语句1为0.2077秒;语句2为0.0063秒。两条语句的时间比是:语句1/语句2=32.968

比较以上的数据时,我们可以发现采用where…limit….性能基本稳定,受偏移量和行数的影响不大,而单纯采用limit的话,受偏移量的影响很大,当偏移量大到一定后性能开始大幅下降。不过在数据量不大的情况下,两者的区别不大。所以应当先使用where等查询语句,配合limit使用,效率才高。在sql语句中,limt关键字是最后才用到的。以下条件的出现顺序一般是:**where->group by->having-order by->limit**

OFFSET

为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。经常用到在数据库中查询中间几条数据的需求比如下面的sql语句:

selete * from testtable limit 2,1;
selete * from testtable limit 2 offset 1;

注意:
1.数据库数据计算是从0开始的
2.offset X是跳过X个数据,limit Y是选取Y个数据
3.limit X,Y 中X表示跳过X个数据,读取Y个数据

这两个都是能完成需要,但是他们之间是有区别的:
①是从数据库中第三条开始查询,取一条数据,即第三条数据读取,一二条跳过
②是从数据库中的第二条数据开始查询两条数据,即第二条和第三条。

UNION & UNION ALL

union all是直接连接,取到得是所有值,记录可能有重复 union 是取唯一值,记录没有重复。

1、UNION 的语法如下:
[SQL 语句 1]
UNION
[SQL 语句 2]

2、UNION ALL 的语法如下:
[SQL 语句 1]
UNION ALL
[SQL 语句 2]

效率

UNION和UNION ALL关键字都是将两个结果集合并为一个,但这两者从使用和效率上来说都有所不同。

1、对重复结果的处理:UNION在进行表链接后会筛选掉重复的记录,Union All不会去除重复记录。

2、对排序的处理:Union将会按照字段的顺序进行排序;UNION ALL只是简单的将两个结果合并后就返回。

从效率上说,UNION ALL 要比UNION快很多,所以,如果可以确认合并的两个结果集中不包含重复数据且不需要排序时的话,那么就使用UNION ALL。

简单应用

将一个表的内容弄成两份到一个输出中:

1
2
3
4
select * from
(select * from players) b
UNION all
(select * from players) ;

join相关

left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
inner join(等值连接) 只返回两个表中联结字段相等的行

参考:
https://www.cnblogs.com/acm-bingzi/p/msqlLimit.html

MySQL中的日期操作

获取当前时间

  • now()

    1
    2
    3
    4
    5
    6
    7
    mysql> select now();
    +---------------------+
    | now() |
    +---------------------+
    | 2018-07-26 15:58:46 |
    +---------------------+
    1 row in set (0.00 sec)
  • sysdate()

    1
    2
    3
    4
    5
    6
    7
    mysql> select sysdate()
    +---------------------+
    | sysdate() |
    +---------------------+
    | 2018-07-26 15:59:19 |
    +---------------------+
    1 row in set (0.00 sec)

两者之间的区别在于:**sysdate()是实时获取的**。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> select now(), sleep(3), now();
+---------------------+----------+---------------------+
| now() | sleep(3) | now() |
+---------------------+----------+---------------------+
| 2018-07-26 16:00:14 | 0 | 2018-07-26 16:00:14 |
+---------------------+----------+---------------------+
1 row in set (3.00 sec)

mysql> select sysdate(), sleep(3), sysdate();
+---------------------+----------+---------------------+
| sysdate() | sleep(3) | sysdate() |
+---------------------+----------+---------------------+
| 2018-07-26 16:00:30 | 0 | 2018-07-26 16:00:33 |
+---------------------+----------+---------------------+
1 row in set (3.00 sec)
  • current_timestamp, current_timestamp()
1
2
3
4
5
6
7
mysql> select current_timestamp, current_timestamp();
+---------------------+---------------------+
| current_timestamp | current_timestamp() |
+---------------------+---------------------+
| 2018-07-26 16:04:01 | 2018-07-26 16:04:01 |
+---------------------+---------------------+
1 row in set (0.00 sec)

日期、时间转换

感觉时间与日期这块有挺多操作的,包括与字符串的相互转换等。

日期/时间转字符串

  • date_format()/time_format()将日期/时间转换成字符串
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    mysql> select date_format('2018-7-26 16:05:11', '%Y%m%d%H%i%s')
    +---------------------------------------------------+
    | date_format('2018-7-26 16:05:11', '%Y%m%d%H%i%s') |
    +---------------------------------------------------+
    | 20180726160511 |
    +---------------------------------------------------+
    1 row in set (0.00 sec)

    mysql> select time_format('16:05:11', '%H%i%s');
    +-----------------------------------+
    | time_format('16:05:11', '%H%i%s') |
    +-----------------------------------+
    | 160511 |
    +-----------------------------------+
    1 row in set (0.00 sec)
  • str_to_date() 将字符串转成日期类型。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    mysql> select str_to_date('07/26/2018', '%m/%d/%Y');
    +---------------------------------------+
    | str_to_date('07/26/2018', '%m/%d/%Y') |
    +---------------------------------------+
    | 2018-07-26 |
    +---------------------------------------+
    1 row in set (0.00 sec)

    mysql> select str_to_date('2018/7/26 16:23:33','%Y/%m/%d %H:%i:%s');
    +-------------------------------------------------------+
    | str_to_date('2018/7/26 16:23:33','%Y/%m/%d %H:%i:%s') |
    +-------------------------------------------------------+
    | 2018-07-26 16:23:33 |
    +-------------------------------------------------------+
    1 row in set (0.00 sec)

日期时间计算函数

为日期增加一个时间间隔:date_add()

1
2
3
4
5
6
7
8
9
10
11
12
13
set @dt = now();

select date_add(@dt, interval 1 day); -- add 1 day
select date_add(@dt, interval 1 hour); -- add 1 hour
select date_add(@dt, interval 1 minute); -- ...
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);

select date_add(@dt, interval -1 day); -- sub 1 day

为日期减去一个时间间隔:date_sub()
日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)

Leetcode数据库无锁题答题记录

175. Combine Two Tables

这里写图片描述

1
2
3
# Write your MySQL query statement below
select FirstName, LastName, City, State
from Person left join Address on Person.PersonId = Address.PersonId;

176. Second Highest Salary

这里写图片描述

1
2
3
4
# Write your MySQL query statement below
select max(Salary) as SecondHighestSalary
from Employee
where Salary != (select max(Salary) from Employee);

177. Nth Highest Salary

这里写图片描述

1
2
3
4
5
6
7
8
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
set N = N - 1;
RETURN (
# Write your MySQL query statement below.
select IFNULL((select distinct salary from employee order by salary desc limit N,1),NULL)
);
END

178. Rank Scores

这里写图片描述

1
2
3
4
5
# Write your MySQL query statement below
select b.Score, cast(b.Rank as UNSIGNED) as Rank
from (select id, score, if(@prev = score, @rank, @rank := @rank + 1) as Rank, @prev := score as tmp
from scores, (select @rank := 0, @prev := null) as a
order by score desc) b;

180. Consecutive Numbers

这里写图片描述

1
2
3
4
# Write your MySQL query statement below
select distinct a.num as ConsecutiveNums
from logs as a, logs as b, logs as c
where (a.id <> b.id and b.id <> c.id) and (b.id = a.id + 1 and c.id = b.id + 1) and (a.num = b.num and b.num = c.num);

181. Employees Earning More Than Their Managers

这里写图片描述

1
2
3
# Write your MySQL query statement below
select name as Employee from employee as e
where e.salary > ifnull((select salary from employee where id=e.managerid), e.salary + 1);

182. Duplicate Emails

这里写图片描述

1
2
# Write your MySQL query statement below
select Email from Person group by Email having count(Email) > 1;

183. Customers Who Never Order

这里写图片描述

1
2
# Write your MySQL query statement below
select name as Customers from Customers where id not in (select distinct CustomerId from Orders);

184. Department Highest Salary

这里写图片描述

1
2
3
4
5
6
7
# Write your MySQL query statement below
select Department, e.Name as Employee, Salary
from
(select DepartmentId as did, Department.Name as Department, max(Salary) maxsalary
from Employee join Department on Department.Id = DepartmentId
group by DepartmentId) as maxtable, Employee e
where e.DepartmentId = maxtable.did and maxsalary = e.Salary;

185. Department Top Three Salaries

这里写图片描述

1
2
3
4
5
6
7
8
9
10
11
12
# Write your MySQL query statement below
select Department, Employee, Salary
from (
select d1.Name as Department, e1.name as Employee, e1.Salary, (
select count(distinct e2.salary)
from Employee as e2
where e2.salary > e1.salary and e2.departmentId = e1.departmentId
) as Rankk
from Employee e1, Department d1
where e1.departmentId = d1.id) as ddd
where Rankk < 3
order by ddd.Department, ddd.Salary desc;

196. Delete Duplicate Emails

这里写图片描述

1
2
3
# Write your MySQL query statement below
delete p1.* from Person p1, Person p2
where p1.email = p2.email and p1.id > p2.id;

197. Rising Temperature

这里写图片描述

1
2
3
4
# Write your MySQL query statement below
select w2.Id
from Weather w1, Weather w2
where datediff(w2.RecordDate, w1.RecordDate) = 1 and w2.Temperature > w1.Temperature;

262. Trips and Users

这里写图片描述
题目所需的建表语句如下:

user

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
/*
Navicat Premium Data Transfer

Source Server : LocalMySQL
Source Server Type : MySQL
Source Server Version : 80011
Source Host : localhost:3306
Source Schema : test

Target Server Type : MySQL
Target Server Version : 80011
File Encoding : 65001

Date: 30/07/2018 13:38:27
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`user_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`banned` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`role` enum('client','driver','partner') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES (1, 'No', 'client');
INSERT INTO `users` VALUES (2, 'Yes', 'client');
INSERT INTO `users` VALUES (3, 'No', 'client');
INSERT INTO `users` VALUES (4, 'No', 'client');
INSERT INTO `users` VALUES (10, 'No', 'driver');
INSERT INTO `users` VALUES (11, 'No', 'driver');
INSERT INTO `users` VALUES (12, 'No', 'driver');
INSERT INTO `users` VALUES (13, 'No', 'driver');

SET FOREIGN_KEY_CHECKS = 1;

trips

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
/*
Navicat Premium Data Transfer

Source Server : LocalMySQL
Source Server Type : MySQL
Source Server Version : 80011
Source Host : localhost:3306
Source Schema : test

Target Server Type : MySQL
Target Server Version : 80011
File Encoding : 65001

Date: 30/07/2018 13:38:43
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for trips
-- ----------------------------
DROP TABLE IF EXISTS `trips`;
CREATE TABLE `trips` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`client_id` int(10) UNSIGNED NOT NULL,
`driver_id` int(10) UNSIGNED NOT NULL,
`city_id` int(11) NOT NULL,
`status` enum('completed','cancelled_by_driver','cancelled_by_client') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`request_at` date NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `client_fk`(`client_id`) USING BTREE,
INDEX `driver_fk`(`driver_id`) USING BTREE,
CONSTRAINT `client_fk` FOREIGN KEY (`client_id`) REFERENCES `users` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `driver_fk` FOREIGN KEY (`driver_id`) REFERENCES `users` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of trips
-- ----------------------------
INSERT INTO `trips` VALUES (1, 1, 10, 1, 'completed', '2013-10-01');
INSERT INTO `trips` VALUES (2, 2, 11, 1, 'cancelled_by_driver', '2013-10-01');
INSERT INTO `trips` VALUES (3, 3, 12, 6, 'completed', '2013-10-01');
INSERT INTO `trips` VALUES (4, 4, 13, 6, 'cancelled_by_client', '2013-10-01');
INSERT INTO `trips` VALUES (5, 1, 10, 1, 'completed', '2013-10-02');
INSERT INTO `trips` VALUES (6, 2, 11, 6, 'completed', '2013-10-02');
INSERT INTO `trips` VALUES (7, 3, 12, 6, 'completed', '2013-10-02');
INSERT INTO `trips` VALUES (8, 2, 12, 12, 'completed', '2013-10-03');
INSERT INTO `trips` VALUES (9, 3, 10, 12, 'completed', '2013-10-03');
INSERT INTO `trips` VALUES (10, 4, 13, 12, 'cancelled_by_driver', '2013-10-03');

SET FOREIGN_KEY_CHECKS = 1;

Accepted Solution

1
2
3
4
5
6
7
8
9
10
11
# Write your MySQL query statement below
select date as Day, cast(format(sum(valid) / count(date), 2) as decimal(10, 2)) as 'Cancellation Rate'
from (
select t.id, t.client_id, c.banned cb, t.driver_id, d.banned db, t.status, t.request_at as date,
if (status in ('cancelled_by_driver', 'cancelled_by_client'), 1, 0) as valid
from trips t, users c, users d
where t.client_id = c.users_id
and t.driver_id = d.users_id
and c.banned != 'Yes' and d.banned != 'Yes'
and t.request_at between '2013-10-01' and '2013-10-03') as tmp
group by date;

595. Big Countries

这里写图片描述

1
2
3
4
# Write your MySQL query statement below
select name, population, area
from World
where population > 25000000 or area > 3000000;

596. Classes More Than 5 Students

这里写图片描述

1
2
3
4
5
# Write your MySQL query statement below
select class
from courses
group by class
having count(distinct student) >= 5;

620. Not Boring Movies

这里写图片描述

1
2
3
4
5
# Write your MySQL query statement below
select *
from cinema
where id % 2 <> 0 and description not like '%boring%'
order by rating desc;

626. Exchange Seats

这里写图片描述

1
2
3
4
5
6
# Write your MySQL query statement below
select b.id, a.student
from seat a, seat b, (select max(id) as id from seat) as maxid
where ((maxid.id % 2 <> 0 and a.id = maxid.id and a.id = b.id)) or
(a.id <> maxid.id and a.id % 2 <> 0 and b.id = a.id + 1) or
(b.id <> maxid.id and a.id % 2 = 0 and a.id = b.id + 1);

627. Swap Salary

这里写图片描述

1
2
3
4
5
6
7
# Write your MySQL query statement below
update salary
set sex=case sex
when 'm' then 'f'
else 'm'
end;

SpringMVC添加拦截器笔记

没有登录时,有些页面是不能让用户访问的,标准的ServletAPI中提供了一个接口,叫做过滤器Filter。但在SpringMVC中,用到的是org.springframework.web.servlet.HandlerInterceptor。首先写一个类,实现HandlerInterceptor接
阅读更多

Java多线程系列(1)对一个多线程同步代码的分析

注意synchronized关键字使用的是实例锁即可。即m1()与m2()是按照某个次序执行,所以在m1()中,一定会输出b = 1000。至于主线程中的输出,则需要考虑执行顺序。package basic.multithread;public class TestSync2 implements
阅读更多

MySQL数据库使用笔记

索引失效问题

https://www.jianshu.com/p/d5b2f645d657

基本使用操作

  • 查看数据库:show databases;
    这里写图片描述

  • 选择某个数据库:use 数据库名
    这里写图片描述

  • 查看某个数据库内有哪些表:show tables;
    这里写图片描述

  • 显示数据表的属性:show columns from 表名;
    这里写图片描述

1
2
3
4
5
6
7
8
9
10
mysql> show index from user;
+-------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
+-------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| user | 0 | PRIMARY | 1 | user_id | A | 0 | NULL | NULL | | BTREE | | | YES |
| user | 0 | PRIMARY | 2 | phone | A | 0 | NULL | NULL | | BTREE | | | YES |
| user | 0 | user_phone_uindex | 1 | phone | A | 0 | NULL | NULL | | BTREE | | | YES |
| user | 0 | user_id_uindex | 1 | user_id | A | 0 | NULL | NULL | | BTREE | | | YES |
+-------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
4 rows in set (0.15 sec)

外键约束

在建表添加外键约束的时候,需要选择on delete/update时的操作。大致知道这是怎么回事,无非是主表的内容删除或者更新的时候,对从表的相应的字段进行什么样的操作。但是具体的内容,忘记了,在这里特地找出来,给自己一个参考,顺便温习一下外键。

外键约束对子表的含义:
如果在父表中找不到候选键,则不允许在子表上进行insert/update

外键约束对父表的含义:
在父表上进行update/delete以更新或删除在子表中有一条或多条对应匹配行的候选键时,父表的行为取决于:在定义子表的外键时指定的on update/on delete子句。

MySQL中的InnoDB有5中方式,如下:

方式 描述
cascade 在父表上update/delete记录时,同步update/delete掉子表的匹配记录
set null 在父表上update/delete记录时,将子表上匹配记录的列设为null,要注意子表的外键列不能为not null
no action 如果子表中有匹配的记录,则不允许对父表对应候选键进行update/delete操作
restrict 同no action, 都是立即检查外键约束
缺省 解析器认识这个action,但Innodb不能识别,不知道是什么意思

常见错误

  • 1022 can't write duplicate key in table #‘sql_XXXXX’
    外键重名导致,另起一个名字即可解决。(mysql)

Java多线程系列(0)基础概念

废话就不多说了,直接上总结吧线程的状态(API文档翻译)看了很多网上的那些关于Java中线程的状态转换图,但是我觉得比较靠谱的还是根据源代码中所定义的状态整出来的状态图。也是看到别人的指点吧。源代码位置:public static enum Thread.StateA thread can be i
阅读更多