snjl

我大概率会编程。


  • 首页

  • 标签

  • 分类

  • 归档

  • 搜索

matplotlib画图

发表于 2018-11-26 | 分类于 python
字数统计: 170 字 | 阅读时长 ≈ 1 分钟
1
2
3
4
5
6
7
8
9
import matplotlib.pyplot as plt

import numpy as np

data = np.arange(100, 201)

plt.plot(data)

plt.show()

image

1.通过np.arange(100, 201)生成一个[100, 200]之间的整数数组,它的值是:[100, 101, 102, … , 200]

2.通过matplotlib.pyplot将其绘制出来。很显然,绘制出来的值对应了图中的纵坐标(y轴)。而matplotlib本身为我们设置了图形的横坐标(x轴):[0, 100],因为我们刚好有100个数值

3.通过plt.show()将这个图形显示出来

修改代码:

1
2
3
4
5
6
7
8
9
import matplotlib.pyplot as plt

import numpy as np

data = np.arange(1, 200, 20)

plt.plot(data, 'ro')

plt.show()

image

加入点也可以用

1
plt.plot([1,2],[3,4])

加入的2个点为(1,3),(2,4)

python:mysql报错记录

发表于 2018-11-26 | 分类于 python , 数据库
字数统计: 116 字 | 阅读时长 ≈ 1 分钟

1.Operand should contain 1 column

字面意思是,需要有1个数据列。
如下sql:

1
cursor.execute("INSERT INTO lunwen(url) VALUES (%s)", (url,))

可能是因为url并不是一个字符串,而是一个tuple或者list。

阅读全文 »

python:常用操作(一)

发表于 2018-11-26 | 分类于 python
字数统计: 560 字 | 阅读时长 ≈ 2 分钟

1.生成随机数

1
2
import random
print(3 * random.random())

生成0-3的随机数。

1
print(random.randint(0,99))

生成0-99的随机整数。

还有很多,用的不多,临时百度。

阅读全文 »

python:list差集

发表于 2018-11-26 | 分类于 python
字数统计: 84 字 | 阅读时长 ≈ 1 分钟

list并集:

1
print list(set(a).union(set(b)))

或者:

1
2
3
4
5
6
7
8
k = [x for x in s1 if x in s2]

>>> x1 = [1,2,3,4,5,6,7]
>>> s1 = x1
>>> s2 = (3,4,6,8)
>>> k = [x for x in s1 if x in s2]
>>> k
[3, 4, 6]

list差集:

1
2
print list(set(b).difference(set(a)))
# b中有而a中没有的

react:页面中存在多个input设置value属性

发表于 2018-11-25 | 分类于 前端
字数统计: 254 字 | 阅读时长 ≈ 1 分钟

首先要为这些所有的input框绑定上onChange的方法,然后还需要在this.state中去设置不同的input对应不同的值,最后还需要在changeValue的方法中去一一监听input输入时去修改对应的input的值。

也可以写一个changeValue方法,但是input的属性的name要和state里的名字要一样,如下所示:

1
2
3
4
5
6
7
8
9
10
constructor() {
super();
this.state = {
login:{
username:'',
password:''
},
user:{}
}
}

1
2
3
4
5
6
7
用户名:
<input type={'text'} name={'username'} value={this.state.login.username}
className={'username'} onChange={this.changeValue.bind(this)}/>
<br/>
密码:
<input type={'password'} name={'password'} value={this.state.login.password}
className={'password'} onChange={this.changeValue.bind(this)}/>
1
2
3
4
5
changeValue(event) {
let newLogin = this.state.login;
newLogin[event.target.name] = event.target.value;
this.setState({login: newLogin})
}

使用event.target.name获取input的name属性,使用evnet.target.value获取input的value,令newLogin为this.state.login,然后将newLogin中名为event.target.name的属性的值设为event.target.value,之后再setState,从而可以改变state.login的值。

react:存储session

发表于 2018-11-25 | 分类于 react , 前端
字数统计: 80 字 | 阅读时长 ≈ 1 分钟

react框架中使用session,如果是单个字符串:

1
sessionStorage.setItem("key",value);

取出时:

1
var result = sessionStorage.getItem("key")

如果是json,在存储时需要将json对象通过stringify()方法转为字符串,存入sessionStorage:

1
sessionStorage.setItem("jsonKey",JSON.stringify(jsonData));

取出时需要再转回json:

1
var result = JSON.parse(sessionStorage.getItem("jsonKey"));

移除session:

1
sessionStorage.removeItem("data");

jQuery禁止回车提交

发表于 2018-11-25 | 分类于 前端
字数统计: 68 字 | 阅读时长 ≈ 1 分钟

在有input框进行输入提交时,如果有input的type为submit,则默认按回车会提交,可以用jQuery的方法进行阻拦:

1
2
3
4
5
6
7
8
9
10
11
$(function(){
$("input").each(
function(){
$(this).keypress( function(e) {
var key = window.event ? e.keyCode : e.which;
if(key.toString() === "13"){
return false;
}
});
});
})

react:form表单提交

发表于 2018-11-25 | 分类于 react , 前端
字数统计: 423 字 | 阅读时长 ≈ 2 分钟

由于React在浏览器显示的是虚拟的DOM,我们在表单输入值后直接提交,是无法获取到这个值的。对此,官方给出的解决办法是:先把输入的值存放在组件的状态(state)中,之后通过状态的改变更新页面内容,从而显示出正确的值,用户提交的也是从state里获取的表单的value。

1
2
3
4
5
6
7
8
9
10
11
constructor() {
super();
this.state = {

item:{
name:'',
organization:'',
school:'',
},
}
}

设置提交方法和改变state的状态的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
handleSubmit(event) {
//阻止表单默认提交
event.preventDefault();
let item = this.state.item;
axios.post(host + 'xxx/insert', item).then(
response => {
let message = response.data;
if (message === 'success') {
alert("插入成功");
window.location.href = '/xxx/list';
} else {
alert("插入失败,请重试");
}
}
);
this.setState({item:item})
}

changeValue(event) {
let newItem = this.state.item;
newItem[event.target.name] = event.target.value;
this.setState({item: newItem})
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<form method={'post'} onSubmit={this.handleSubmit.bind(this)}>

<label>申请数据集名称:
<input type="text" name={'name'} onChange={this.changeValue.bind(this)}/>
</label>
<br/>

<label>学校:
<input type="text" name={'organization'} value={this.state.item.organization} onChange={this.changeValue.bind(this)}/>
</label>
<br/>

<label>学院:
<input type="text" name={'school'} value={this.state.item.school}
onChange={this.changeValue.bind(this)}/>
</label>
<br/>

<input type={'submit'} value={'提交'}/>
<input value={'重置'} type={'reset'}/>
</form>

注:form中用onSubmit替代action,其中每个input的name需要对应定义state时的状态,在使用axios调接口传值时,如果是java的springmvc的后端,需要将传输的属性与对应的实体类对接好。

springboot报错记录(一)

发表于 2018-11-24 | 分类于 springboot
字数统计: 2k 字 | 阅读时长 ≈ 10 分钟

1. Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured

1
2
3
4
5
6
7
8
9
10
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

原因:引入mybatis的jar包时:

1
2
3
4
5
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>

springboot会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration这个类,而DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean,又因为项目中并没有关于dataSource相关的配置信息,所以当spring创建dataSource bean时因缺少相关的信息就会报错。

解决办法:

  1. 在@SpringBootApplication注解上加上exclude,解除自动加载DataSourceAutoConfiguration。
1
2
3
4
5
6
7
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
  1. 在parent项目的pom.xml文件中保存所有子模块的共有jar依赖,非共有的依赖则在各模块自身的pom.xml文件中进行申明。建议采用此方法,好处在于各模块的依赖不会相互产生干扰。

2.maven配置时,测试出现“’mvn’ 不是内部或外部命令,也不是可运行的程序”错误

配置maven。

在环境变量的PATH中配置maven的bin目录。

使用mvn package打包成功后显示日志:

1
2
3
4
5
6
7
-----------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 46.791 s
[INFO] Finished at: 2018-11-21T16:12:36+08:00
[INFO] Final Memory: 35M/275M
[INFO] ------------------------------------------------------------------------

3.java.sql.SQLException: The server time zone value ‘???ú±ê×??±??’ is unrecognized or represents……..

具体错误:

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
95
96
97
98
99
100
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Tue May 15 21:38:04 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)


at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)


at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at zhu.jdbc.unit.UnitMysql.getConnection(UnitMysql.java:34)
at zhu.jdbc.dao.imp.ITb_UserImpI.insertData(ITb_UserImpI.java:55)
at zhu.jdbc.dao.imp.ITb_UserImpI.insertData(ITb_UserImpI.java:1)
at zhu.jdbc.service.imp.ITb_UserServiceImpI.insertData(ITb_UserServiceImpI.java:35)
at zhu.jdbc.service.imp.ITb_UserServiceImpI.insertData(ITb_UserServiceImpI.java:1)
at zhu.jdbc.servlet.Servlet_TbUser.Insert(Servlet_TbUser.java:87)
at zhu.jdbc.servlet.Servlet_TbUser.doPost(Servlet_TbUser.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)


at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:993)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
... 36 more
五月 15, 2018 9:38:04 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [Servlet_TbUser] in context with path [/myweb2] threw exception
java.lang.NullPointerException
at zhu.jdbc.dao.imp.ITb_UserImpI.insertData(ITb_UserImpI.java:59)
at zhu.jdbc.dao.imp.ITb_UserImpI.insertData(ITb_UserImpI.java:1)
at zhu.jdbc.service.imp.ITb_UserServiceImpI.insertData(ITb_UserServiceImpI.java:35)
at zhu.jdbc.service.imp.ITb_UserServiceImpI.insertData(ITb_UserServiceImpI.java:1)
at zhu.jdbc.servlet.Servlet_TbUser.Insert(Servlet_TbUser.java:87)
at zhu.jdbc.servlet.Servlet_TbUser.doPost(Servlet_TbUser.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Unknown Source)

出现这个的原因是因为 mysql返回的时间总是有问题,比实际时间要早8小时。

解决办法:

在jdbc连接的url后面加上serverTimezone=GMT即可解决问题,如果需要使用gmt+8时区,需要写成GMT%2B8。

为了防止因为日期为null报错,可以直接加上:

?serverTimezone=UTC&zeroDateTimeBehavior=convertToNull

例如链接数据库,可以写为:

jdbc.url=jdbc:mysql://xxx.xxx.xxx.xxx/xxxxx?serverTimezone=UTC&zeroDateTimeBehavior=convertToNull

4.Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

这个问题是 使用了新的mysql驱动包,但驱动声明不是最新的:

DriverClassName 从 “com.mysql.jdbc.Driver” 换成 “com.mysql.cj.jdbc.Driver” 即可。

5.spring Failed to convert property value of type ‘java.lang.String’ to required type ‘int’ for property

原错误是不能将long的null属性转化出来,但是没找到原来的错误。采取的方式是,把pojo类中的long全改为Integer。

如果是date出错,参考问题3,在数据库连接时加上zeroDateTimeBehavior=convertToNull。

6. This application has no explicit mapping for /…

原因1:
Application启动类的位置不对.要将Application类放在最外侧,即包含所有子包
原因:spring-boot会自动加载启动类所在包下及其子包下的所有组件.

原因2:
在springboot的配置文件:application.yml或application.properties中关于视图解析器的配置问题:
当pom文件下的spring-boot-starter-paren版本高时使用:
spring.mvc.view.prefix/spring.mvc.view.suffix
当pom文件下的spring-boot-starter-paren版本低时使用:
spring.view.prefix/spring.view.suffix

原因3:
控制器的URL路径书写问题
@RequestMapping(“xxxxxxxxxxxxxx”)
实际访问的路径与”xxx”不符合.

原因4:
**application.properties中设置thymeleaf的属性时,注释掉spring.thymeleaf.view-names=,这是可解析的视图名称列表,用逗号分隔,但是如果不写,就会报这个错误。

7.springboot中 server: context-path: /spring报错:Deprecated configuration property

是版本问题,这个新版本不是这种写法:应该变化为:server.servlet.context-path: /spring

springboot使用JdbcTemplate(一)

发表于 2018-11-24 | 分类于 springboot
字数统计: 1.5k 字 | 阅读时长 ≈ 8 分钟

项目地址:https://github.com/snjl/springboot.mysql.git

环境配置

修改 POM 文件,添加spring-boot-starter-jdbc依赖:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

添加mysql依赖(spring-boot-starter-jdbc 默认使用tomcat-jdbc数据源,如果要使用其他数据源,需要添加额外的依赖,这里使用了阿里巴巴的数据池管理):

1
2
3
4
5
6
7
8
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>

阅读全文 »
1…171819…21
snjl

snjl

越过山丘,才发现无人等候。

203 日志
44 分类
107 标签
RSS
GitHub E-Mail Weibo
© 2019 snjl
总访问量次 | 总访客人 |
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.4