0%

xml 处理类

在xml对象属性上添加注解: @XmlJavaTypeAdapter(CollapsedStringAdapter.class) 即可

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.enableets.edu.sdk.paper.util;

import org.apache.commons.lang.StringEscapeUtils;

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
* Html 转义字符处理, 并删除前后空格
*/

public class XMLUnescapeHtmlStringAdapter extends XmlAdapter<String,String> {

/**
* Removes leading and trailing whitespaces of the string
* given as the parameter, then truncate any
* sequnce of tab, CR, LF, and SP by a single whitespace character ' '.
*/
public String unmarshal(String text) {
if(text==null) return null; // be defensive

int len = text.length();

// most of the texts are already in the collapsed form.
// so look for the first whitespace in the hope that we will
// never see it.
int s=0;
while(s<len) {
if(isWhiteSpace(text.charAt(s)))
break;
s++;
}
if(s==len)
// the input happens to be already collapsed.
return text;

// we now know that the input contains spaces.
// let's sit down and do the collapsing normally.

StringBuilder result = new StringBuilder(len /*allocate enough size to avoid re-allocation*/ );

if(s!=0) {
for( int i=0; i<s; i++ )
result.append(text.charAt(i));
result.append(' ');
}

boolean inStripMode = true;
for (int i = s+1; i < len; i++) {
char ch = text.charAt(i);
boolean b = isWhiteSpace(ch);
if (inStripMode && b)
continue; // skip this character

inStripMode = b;
if (inStripMode)
result.append(' ');
else
result.append(ch);
}

// remove trailing whitespaces
len = result.length();
if (len > 0 && result.charAt(len - 1) == ' ')
result.setLength(len - 1);
// whitespaces are already collapsed,
// so all we have to do is to remove the last one character
// if it's a whitespace.

// tran html encoding
String s1 = result.toString();
return StringEscapeUtils.unescapeHtml(s1);
}

/**
* No-op.
*
* Just return the same string given as the parameter.
*/
public String marshal(String s) {
return s;
}


/** returns true if the specified char is a white space character. */
protected static boolean isWhiteSpace(char ch) {
// most of the characters are non-control characters.
// so check that first to quickly return false for most of the cases.
if( ch>0x20 ) return false;

// other than we have to do four comparisons.
return ch == 0x9 || ch == 0xA || ch == 0xD || ch == 0x20;
}
}

image

创建静态文件

publicFile_zh_CN.js
publicFile_zh_TW.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
questionTypeListDefault = [{
"code": "01",
"name": "单选题",
"relationCode": "12"
}, {
"code": "02",
"name": "多选题",
"relationCode": "12"
}, {
"code": "03",
"name": "填空题",
"relationCode": "12"
}, {
"code": "04",
"name": "判断题",
"relationCode": "12"
},{
"code": "05",
"name": "简答题",
"relationCode": "12"
}];

引入静态文件

今天完成的事

Spring Boot 整合 Dubbo

Duboo

Duboo 总结, 之前服务端一直无法找到服务提供者, 初步判断是包的兼容性问题, 每个版本示例用的都不一样, 索性换了一个完整的示例. 先运行再看, 大概理解了Dubbo的意思.

  1. 服务提供者根据配置文件将要对外发布的 组件服务(跟SCA的另一种实现, 对我来说用起来比Tuscany舒服多了)的配置注册到注册中心供服务消费者使用.
  2. 服务消费者根据配置文件在注册中心获取 服务提供者提供的服务信息(服务提供者的服务器ip地址及端口号, 服务的具体名称及包含的方法), 再去连接服务器提供者, 调用提供的远程服务(接口)
  3. 注册中心起 配置信息存放的作用, 不做具体的数据传输.

服务提供者

阅读全文 »

配置说明:

配置一切正常, 但一直 404 问题, 解决办法

原因, maven仓库包下载的问题

1
按照在Stack Overflow上一位外国朋友遇到的差不多的问题以及解答,说是有可能是maven引入的包有问题导致的。但是该问题无法通过在项目中清理后重新构建来修复(本人尝试无用),只能把配置的对应的maven m2目录清空,然后打开eclipse以后重新构建的时候让maven重新下载所需jar包才可以。

解决方法

  • 删除原来的maven仓库, 有很多个人包, 不方便
  • 手动指定Thymeleaf版本
阅读全文 »

今天完成的事

熟悉 spring data jpa的使用

Spring data jpa

首先了解JPA是什么?

JPA(Java Persistence API)是Sun官方提出的Java持久化规范。它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据。他的出现主要是为了简化现有的持久化开发工作和整合ORM技术,结束现在Hibernate,TopLink,JDO等ORM框架各自为营的局面。值得注意的是,JPA是在充分吸收了现有Hibernate,TopLink,JDO等ORM框架的基础上发展而来的,具有易于使用,伸缩性强等优点。从目前的开发社区的反应上看,JPA受到了极大的支持和赞扬,其中就包括了Spring与EJB3.0的开发团队。

1
注意:JPA是一套规范,不是一套产品,那么像Hibernate,TopLink,JDO他们是一套产品,如果说这些产品实现了这个JPA规范,那么我们就可以叫他们为JPA的实现产品。
阅读全文 »

常用命令

查看线程:

1
2
3
4
select id, db, user, host, command, time, state, info
from information_schema.processlist
where db like 'resource_01'
order by time desc ;

UNION ALL: 将两张表的数据拼装到一起返回.

联表查询:

1
2
3
4
5
6
7
8
9
SELECT tb_students.id, tb_students.stuName, tb_profession.proName AS stuProfession FROM tb_students, tb_profession WHERE 
tb_students.stuProfession = tb_profession.id ORDER BY tb_students.id


SELECT tb_profession.*, COUNT(*) AS proCount
FROM tb_students
INNER JOIN tb_profession
ON tb_profession.id = tb_students.stuProfession
GROUP BY tb_profession.id

今天完成的事情:

Spring Boot Redis, 依赖的事情弄了很久, 纯洁的微笑的示例教程是使用1.0的Spring Boot, 我使用的是2.0.1, 有很大出入, 它Github上的1.5.6版本的Redis示例也是有问题的.

Spring Boot Redis

如何使用

引入 redis 相关依赖, 注意:

,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- Spring Boot使用的是1.4(包括1.4版本)之前的版本使用如下配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<!-- 1.5.* 的版本需要指定版本号 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- 2.* 版本 redis依赖改名了,直接使用下面的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 导入其他模块的依赖 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>02-SpringBootWeb</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
阅读全文 »

国际化文件

1
2
3
4
5
6
7
8
9
10
resources
│ application.yml
│ logback.xml
│ mybatis-config.xml

└─i18n
lang.properties
lang_en_US.properties
lang_zh_CN.properties
lang_zh_TW.properties

yml 配置文件

1
2
3
4
5
6
i18n: 
locale: zh_CN
spring:
messages:
basename: i18n/lang
encoding: UTF-8

启动类注入国际化配置

1
2
3
4
5
6
7
8
9
@Value("${i18n.locale}")
private String locale;


@Bean
public LocaleResolver localeResolver() {
Locale locale = I18nUtils.localeFromString(this.locale, (Locale)null);
return new FixedLocaleResolver(locale);
}
阅读全文 »