spring-cloud-config

dependency

动态刷新

使用限制

@RefreshScope @Configuration 不能同时在一个类上使用
原因说明:

@RefreshScope works (technically) on an @Configuration class, but it might lead to surprising behaviour: e.g. it does not mean that all the @Beans defined in that class are themselves @RefreshScope. Specifically, anything that depends on those beans cannot rely on them being updated when a refresh is initiated, unless it is itself in @RefreshScope (in which it will be rebuilt on a refresh and its dependencies re-injected, at which point they will be re-initialized from the refreshed @Configuration).

参考:
https://stackoverflow.com/questions/45137555/refreshscope-not-working-spring-boot
http://projects.spring.io/spring-cloud/spring-cloud.html#_refresh_scope

druid数据库连接池通用配置

dependency

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>

config

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
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_user}" />
<property name="password" value="${jdbc_password}" />

<!-- 配置初始化大小、最小、最大 -->
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />

<!-- 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。 -->
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />

<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat" />

<!-- 1.1.4中新增配置,如果有initialSize数量较多时,打开会加快应用启动时间 -->
<property name="asyncInit" value="true" />
</bean>

error: Error creating bean with name ‘dataSource’

Druid连接池与spring cloud config搭配使用时出现异常
错误堆栈信息:

1
2
3
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Could not bind properties to DruidDataSourceWrapper (prefix=spring.datasource, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 3 errors
Field error in object 'spring.datasource' on field 'driverClassName': rejected value [com.mysql.jdbc.Driver]; codes [methodInvocation.spring.datasource.driverClassName,methodInvocation.driverClassName,methodInvocation.java.lang.String,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.datasource.driverClassName,driverClassName]; arguments []; default message [driverClassName]]; default message [Property 'driverClassName' threw exception; nested exception is java.lang.UnsupportedOperationException]
Field error in object 'spring.datasource' on field 'url'

异常出现代码位置:

1
2
3
4
5
6
7
8
9
10
11
public void setDriverClassName(String driverClass) {
if (inited) {
if (StringUtils.equals(this.driverClass, driverClass)) {
return;
}

throw new UnsupportedOperationException();
}

this.driverClass = driverClass;
}

代码中如有@PostConstruct并在其中进行了对数据库查询更新等于数据源有关的操作,则会出现此错误

lombok

dependency

1
2
3
4
5
6
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>

IDE支持

Eclipse
IDEA

@Data

@Getter

@Setter

@Slf4j

@AllArgsConstructor

@EqualsAndHashCode

mybatis

batch update

批量更新
eg:

1
2
3
4
5
6
update table_post p
inner join (
SELECT u.user_id,u.post_count
FROM table_user u
) as c_u on p.user_id = c_u.user_id
set u.post_count=c_u.post_count;

mapper中batch update写法:

1
2
3
4
5
6
7
8
9
<update id="batchUpdate"  parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update test
<set>
test=${item.test}+1
</set>
where id = ${item.id}
</foreach>
</update>

TIP:数据库连接必须配置:&allowMultiQueries=true

文档

中文文档
http://www.mybatis.org/mybatis-3/zh/

mybatis-spring
http://www.mybatis.org/spring/zh/

MyBatis Generator
http://www.mybatis.org/generator/
http://generator.sturgeon.mopaas.com/

android

版本 9

IDE
Android Studio

val
val a: Int = ch.toInt();

var 变量
val 常量
常量不能再次更改

boolean

arrayOf

Array

日志

swith ==> when

for 倒序遍历

guava - Java开发工具

Lists

Arrays

设计模式

lambda

Eclipse - Java开发工具

Why Eclipse

默认配置的热部署:方法内部代码修改无需重新启动

安装设置

  • 字体设置
  • workspace 编码设置
  • installed jres
  • package explorer
  • startup and shutdown
  • compare
    • Ignore white space
    • Swap left and right

可选:

  • sonar server
  • 中文语言包

自动升级

原文链接:
https://download.eclipse.org/releases/2019-06/

格式

Maven常用功能

常用命令

1
2
3
4
5
mvn clean
mvn package
mvn install
mvn clean package -DskipTests
mvn clean package -DskipTests -U

mvn install 会将项目生成的构件安装到本地Maven仓库

mirror

阿里镜像:

1
2
3
4
5
6
<mirror>  
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>human readable name for this mirror.</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

nexus

maven私服

配置参考

配置文件路径(示例):C:\Users\username.m2\settings.xml

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×