• Home

  • 归档

  • 分类

  • 标签
Keep Coding
Keep Coding

02月
14
数据库

SQL 插入时,如果主键已存在,则更新

发表于 2020-02-14 • 分类于 数据库

MySQL

规则:如果你插入的记录导致一个 UNIQUE 索引或者 primary key 出现重复,那么就会认为该条记录存在,则执行 update 语句;反之,则执行 insert 语句。

如果行作为新记录被插入,则受影响行的值为 1;如果原有的记录被更新,则受影响行的值为 2,如果更新的数据和已有的数据一模一样,则受影响的行数是 0,这意味着不会去更新,也就是说即使你有的时间戳是自动记录最后一次的更新时间,这个时间戳也不会变动。

插入一行

1
2
3
INSERT INTO tbl_name (id, b, c) VALUES 
(1,2,3)
ON DUPLICATE KEY UPDATE b = 2, c = 3;

插入多行

1
2
3
4
INSERT INTO tbl_name (id, b, c) VALUES 
(1,2,3),
(2,3,4)
ON DUPLICATE KEY UPDATE b = VALUES(b);
阅读全文 »
02月
14
数据库

SQL 判断以逗号分隔的字符串是否包含某一项

发表于 2020-02-14 • 分类于 数据库

MySQL

MySQL 中有一个方法 FIND_IN_SET(ele, str),str 是一个以逗号分隔的字符串,ele 是一个特定字符串。该方法表示判断 str 中是否包含 ele,
如果包含,则FIND_IN_SET(ele, str) 方法返回值大于 0,表示匹配第几个子串。

1
2
3
4
5
6
7
8
9
10
11
find_in_set('aa','aa,bbb,c,dd,e,f')|
-----------------------------------|
1|

find_in_set('a','aa,bbb,c,dd,e,f')|
----------------------------------|
0|

find_in_set(null,'aa,bbb,c,dd,e,f')|
-----------------------------------|
|

SQL Server

SQL Server 中没有封装好的方法可以实现该功能,可以通过 CHARINDEX(‘,’ + ele + ‘,’ , ‘,’ + str + ‘,’) 来实现,str 是一个以逗号分隔的字符串,ele 是一个特定字符串,该方法表示 ele 在 str 中的位置,位置最小是 1,所以若值大于 0,则表示包含,否则表示不包含。

阅读全文 »
02月
14
数据库

数据库数据类型转换

发表于 2020-02-14 • 分类于 数据库

mssql 数据类型转换

Sql Server系列:数据类型转换函数

T-SQL 提供了两个显示转换的函数:CAST 函数和 CONVERT 函数。

CAST 函数

语法:

1
CAST ( expression AS data_type [ ( length ) ] )

示例:

1
select CAST('2014-11-30 10:20:29' AS TIME)

结果:

1
10:20:29

CONVERT 函数

语法:

1
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

示例:

1
SELECT CONVERT(TIME, '2014-11-30 10:20:29')

结果:

1
10:20:29
阅读全文 »
01月
30
Redis

Redis 学习

发表于 2020-01-30 • 分类于 Redis

以下内容来自:《Redis 设计与实现》

数据库键空间

Redis 是一个键值对(key-value pair)数据库服务器,服务器中的每个数据库都由I个 redis/redisDb 结构表示,其中,redisDb 结构的 dict 字典保存了数据库中的所有键值对,这个字典称为键空间(key space)。

1
2
3
4
5
6
7
8
9
10
11
12
13
typedef struct redisDb {

// ...

// 数据库键空间,保存着数据库中所有键值对
dict *dict;

// 过期字典,保存着键的过期时间
dict *expires;

// ...

} redisDb;

键空间和用户所见的数据库是直接对应的:

  • 键空间的键也就是数据库的键,每个键都是一个字符串对象。
  • 键空间的值也就是数据库的值,每个值可以是字符串对象、列表对象、哈希表对象、集合对象和有序集合对象中的任意一种 Redis 对象。
阅读全文 »
11月
14
数据库

数据库的时间处理

发表于 2019-11-14 • 分类于 数据库

MySQL

阅读全文 »
11月
04
Spring

JdbcTemplate 执行 insert into 操作后,获取自动递增的主键值。

发表于 2019-11-04 • 分类于 Spring

转载自:Fetch Auto Generated Primary Key Value After Insert

Most of the time, the primary key is auto generated and once a row is inserted it is a common requirement to fetch auto generated primary key value after insert statement execution.So in this topic we will take a look into fetching that auto generated primary key once an insert statement is executed.We will implement this fetching of auto generated primary key while inserting through jdbctemplate and also namedparameterjdbctemplate using KeyHolder interface provided by spring.

KeyHolder Interface

KeyHolder Interface is for retrieving keys, typically used for auto-generated keys as potentially returned by JDBC insert statements.

Implementations of this interface can hold any number of keys. In the general case, the keys are returned as a List containing one Map for each row of keys.

Most applications only use on key per row and process only one row at a time in an insert statement. In these cases, just call getKey to retrieve the key. The returned value is a Number here, which is the usual type for auto-generated keys.

Fetch Auto Generated primary key with JdbcTemplate

Following is the implementation of an insert statement using jdbctemplate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private final String INSERT_SQL = "INSERT INTO USERS(name,address,email) values(?,?,?)";

@Autowired
private JdbcTemplate jdbcTemplate;

public User create(final User user) {
KeyHolder holder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(INSERT_SQL, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, user.getName());
ps.setString(2, user.getAddress());
ps.setString(3, user.getEmail());
return ps;
}
}, holder);

int newUserId = holder.getKey().intValue();
user.setId(newUserId);
return user;
}

Fetch Auto Generated primary key with NamedParameterJdbcTemplate

Following is the implementation using NamedParameterJdbcTemplate to fetch auto generated primary key value after insert statement execution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private final String INSERT_SQL = "INSERT INTO USERS(name, address, email) values(:name,:address,:email)";

@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public User create(final User user) {
KeyHolder holder = new GeneratedKeyHolder();
SqlParameterSource parameters = new MapSqlParameterSource()
.addValue("name", user.getName())
.addValue("address", user.getAddress())
.addValue("email", user.getEmail());
namedParameterJdbcTemplate.update(INSERT_SQL, parameters, holder);
user.setId(holder.getKey().intValue());
return user;
}
阅读全文 »
11月
04
Java

Java 三目表达式报 NullPointerException

发表于 2019-11-04 • 分类于 Java
1
2
3
> If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7) to T, then the type of the conditional expression is T.
> 如果三目表达式中第二个和第三个操作数中的一个是原始类型 T,另一个操作数的类型是对 T 进行了装箱转换后的类型,那么这个条件表达式的结果类型是 T。
>

参考:Java Ternary ?: operator - Numeric operands conversion rules

示例:

1
2
3
4
boolean b = true;
Integer i = null;
int j = 5;
System.out.println(b ? i : j);

因为 b 是 true,所以会返回 i,但是根据上面的规则,该三目表达式的结果类型是 int,
则 JVM 要调用 i.intValue() 方法将 Integer 类型转成 int。因为 i 是 null,所以会报 NullPointerException。

阅读全文 »
06月
30
Linux

Linux ps 与 netstat 命令

发表于 2019-06-30 • 分类于 Linux

根据端口或者服务名称查询进程号

一、ps -ef | grep 端口号/服务名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-A :列出所有的进程(等价于 -e)
-w :显示加宽可以显示较多的资讯
-au:显示较详细的资讯
-aux:显示所有包含其他使用者的行程
-a:显示所有终端机下执行的程序,除了阶段作业领导者之外。
-c:显示 CLS 和 PRI 栏位。
-d:显示所有程序,但不包括阶段作业领导者的程序。
-e:显示所有程序。
-f:显示 UID,PPIP,C 与 STIME 栏位。
-H:显示树状结构,表示程序间的相互关系。
-u:<用户识别码>:列出属于该用户的程序的状况,也可使用用户名称来指定。
-j:采用工作控制的格式显示程序状况
-l 或 l:采用详细的格式来显示程序状况。
-N:显示所有的程序,除了执行 ps 指令终端机下的程序之外。

如:

1
2


命令输出结果属性的意义:

UID PID PPID C STIME TTY TIME CMD
用户 进程 ID 父进程 ID 占用的 CPU 使用率 该进程执行开始时间 登录者的终端位置 进程启动时间 所执行的指令

可以直接使用 ps -ef 来查看栏位名称:

1
2
3
$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 6月06 ? 00:02:23 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
阅读全文 »
05月
28
Java

Java8 集合操作

发表于 2019-05-28 • 分类于 Java

集合分组

方式一

1
2
3
4
5
6
7
8
9
10
11
12
public static Map<String, List<Apple>> getAppleByOne(List<Apple> apples) {
Map<String, List<Apple>> map = new HashMap<>();
for (Apple a : apples) {
List<Apple> list = map.get(a.getColor());
if (list == null) {
list = new ArrayList<>();
map.put(a.getColor(), list);
}
list.add(a);
}
return map;
}

方式二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private static Map<String,List<Apple>> getByOptioal(List<Apple> apples){
Map<String, List<Apple>> map = new HashMap<>();
apples.stream().forEach((Apple a) -> {
List<Apple> list1 = Optional
.ofNullable(map.get(a.getColor()))
.orElseGet(() -> {
List<Apple> list = new ArrayList<>();
map.put(a.getColor(), list);
return list;
});
list1.add(a);
});
return map;
}

方式三

1
2
3
4
private static Map<String,List<Apple>> getBycollection(List<Apple> apples){
return apples.stream()
.collect(Collectors.groupingBy(Apple::getColor));
}
阅读全文 »
04月
23
问题记录

文档转换文件格式

发表于 2019-04-23 • 分类于 问题记录

Word 转 PDF

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
/**
* word 转 pdf
*
* @param word
* @param pdf
* @return
*/
public static boolean word2pdf(String word, String pdf) {
ActiveXComponent wps = null;
Dispatch doc = null;
// word 保存为 pdf 格式宏,值为 17
int fmt = 17;
long start = System.currentTimeMillis();
try {
/**
* 参数为 Word.Application:需要安装 Office
* 参数为 kwps.application:需要安装 WPS
* doc 转 pdf 时,用 Word.Application 报错
*/
wps = new ActiveXComponent("kwps.application");
wps.setProperty("visible", new Variant(false));
Dispatch docs = wps.getProperty("Documents").toDispatch();

// doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
// new Object[]{word, new Variant(false),new Variant(true)},
// new int[1]).toDispatch();
Variant foo = Dispatch.call(docs, "Open", word, false, true);
if (foo.getvt() == 0) {
return false;
}
doc = foo.toDispatch();
// Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{pdf,new Variant(fmt)}, new int[1]);
Dispatch.call(doc, "SaveAs", pdf, fmt);
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start) + "ms.");
return true;
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("转化出错:" + ex.getMessage());
return false;
} finally {
Dispatch.call(doc, "Close", false);
System.out.println("关闭WPS");
if (wps != null) {
wps.invoke("Quit", new Variant[]{});
}
}
}
阅读全文 »
123…8
wuchao

72 日志
18 分类
Creative Commons

博客已萌萌哒运行(●'◡'●)ノ♥

© 2020 Keep Coding. 由 Hexo 强力驱动. Theme By Sagiri v0.0.4.

Made with by wuchao.