这几天开发的过程中,碰见了一些问题,有些是属于常识问题,知道了记住了,下次就不会犯错,有些是属于知识模糊,需要理解,在此总结记录。
1. MyBatis报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
网上的经验介绍:
一般的原因是Mapper interface和xml文件的定义对应不上,需要检查包名,namespace,函数名称等能否对应上,需要比较细致的对比,例如
1:检查xml文件所在的package名称是否和interface对应的package名称一一对应
2:检查xml文件的namespace是否和xml文件的package名称一一对应
3:检查函数名称能否对应上
4:去掉xml文件中的中文注释
5:随意在xml文件中加一个空格或者空行然后保存
我的报错原因,有以下几种类型,
(1) xml配置文件,由于参数类型,使用了自定义的Entity,且由于包的路径有改动,原来使用如下配置方式,可以执行的现在就报错了,
<select id="findAll" resultType="VOEntity">
解决方法:
将resultType使用的VOEntity使用完整路径,
<select id="findAll" resultType="com.xxx.VOEntity">
注:同样适用于resultMap。
(2) xml中标签id和dao接口方法名称不同
例如配置文件,定义的是<select id="findAll" ...>,但dao中定义的是String findall(...)。
解决方法:
xml配置和dao接口中,定义的方法名称一致。
2. 日期函数的逻辑错误
有个需求,根据指定日期,计算7天后的日期,逻辑其实很简单,
public static Date getNextWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, +7);
return cal.getTime();
}
这么写如果指定日期,默认当天日期,其实可以,其实上述不需要入参date,但若指定日期不是当天,逻辑就有问题,其实增加一个cal.setTime(date);,就可以解决了,即设置Calendar实例为指定日期,
public static Date getNextWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, +7);
return cal.getTime();
}
3. Tomcat中文字符集问题
Tomcat部署的应用程序,出现中文乱码,有一种解决方法,配置文件conf/server.xml中找到"Connector",增加属性URIEncoding信息,
<Connector port="8090" protocol="HTTP/1.1"
maxThreads="150" connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8" />
但我的问题没有解决,只能选择绕道,由于引用的第三方包,使用了如下方法,由于按字节读取的时候,未指定字符集,导致中文乱码,
while ((n = is.read(b)) != -1) {
out.append(new String(b, 0, n));
}
String()构造函数,有一种可以指定字符集,
Constructs a new
String
by decoding the specified subarray of bytes using the specified charset. The length of the newString
is a function of the charset, and hence may not be equal to the length of the subarray.This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string. The
java.nio.charset.CharsetDecoder
class should be used when more control over the decoding process is required.
- Parameters:
- bytes The bytes to be decoded into characters
- offset The index of the first byte to decode
- length The number of bytes to decode
- charset The charset to be used to decode the
bytes
- Throws:
- IndexOutOfBoundsException - If the
offset
andlength
arguments index characters outside the bounds of thebytes
array- Since:
- 1.6
改为此种,中文正常,
while ((n = is.read(b)) != -1) {
out.append(new String(b, 0, n, "UTF-8"));
}
4. Eclipse运行报错java.lang.OutOfMemoryError: PermGen space
Eclipse配置Tomcat应用运行,报了内存溢出,解决方案如下,进入Run-Run Configurations配置,
增加参数,
-Xms256m -Xmx512m -XX:PermSize=256m -XX:MaxPermSize=256M
针对一些概念,引用如下,
PermGen space简介
PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,OutOfMemoryError: PermGen space从表面上看就是内存溢出,解决方法也一定是加大内存。
说说为什么会内存益出:
(1)这一部分用于存放Class和Meta的信息,Class在被 Load的时候被放入PermGen space区域,它和和存放Instance的Heap区域不同。
(2) GC(Garbage Collection)不会在主程序运行期对PermGen space进行清理,所以如果你的APP会LOAD很多CLASS的话,就很可能出现PermGen space错误。这种错误常见在web服务器对JSP进行pre compile的时候。
如果你的WEB APP下都用了大量的第三方jar,其大小超过了jvm默认的大小(4M)那么就会产生此错误信息了。
建议:将相同的第三方jar文件移置到tomcat/shared/lib目录下,这样可以减少jar 文档重复占用内存
在linux下部署的时候要修改catalina.sh
JAVA_OPTS=”$JAVA_OPTS -server -Xms1536m -Xmx1536m -XX:PermSize=256m -XX:MaxPermSize=512m