续关于iBatis

昨天用了一下iBatis,大概了解了它的用法,也有了实际感受
这个东西不如Hibernate强啊@_@ 前面那个E文的文章说得很对。iBatis仍然是写SQL语句,封装的比较薄,嗯。更为“安心”,更可控制。但是……也更麻烦。对于我来说,显然会喜欢Hibernate一些……
不过iBatis的架构做的看起来很不错的,虽然没有很认真的看。一看就是条分缕析,把SQLMAP和DAO分离开来,嗯。也有一个相当完善的JPetStore例子可以参考。这种东西一个完善的例程就是王道啊!

iBATIS vs Hibernate

首先从这里抄一段

Hibernate vs. iBATIS

There’s an interesting thread taking place on the iBATIS User Mailing List. The basic jist of the responses are: Hibernate works well when you control the data model, iBATIS works well when you need to integrate with an existing database. I’ve said this for a couple years now, and I still believe it. Furthermore, I’ve found that when working with iBATIS, I tend to know what’s going on a lot more. After all, it’s just SQL. From all the questions on the AppFuse mailing list, it seems like a lot of Hibernate users are constantly trying to get Hibernate to "work its magic" and handle all their relationships for them.

然后补充一点。。iBATIS是Apache2.0,Hibernate是LGPL,这里不让用,555555

tomcat的jdbc连接池问题

不得不ft一下,按照tomcat主页上的操作居然不能行,就是下面这儿的
http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jndi-datasource-examples-howto.html
用tomcat5.0.28,通过tomcat自己维护的连接池去连接mysql,出现如下错误

org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null', cause:
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:243)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:743)
.. ...

因为那个网页上写的很清楚。。于是非常奇怪,上网serach了一下看到n多同样问题,汗。在两个地方看到大概是server.xml不能按照那个官方的配法,而是要把resource param之类的分开写才行,即


<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>

这个写法不行


<ResourceParams name="jdbc/scholarship"> <parameter> <name>factory</name> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter>
<parameter> <name>maxActive</name> <value>10</value> </parameter> <parameter> <name>maxIdle</name> <value>5</value> </parameter> <parameter> <name>maxWait</name> <value>10000</value> </parameter>
<parameter> <name>username</name> <value>ss</value> </parameter> <parameter> <name>password</name> <value>123456</value> </parameter> <parameter> <name>driverClassName</name> <value>com.mysql.jdbc.Driver</value> </parameter>
<parameter> <name>url</name> <value>jdbc:mysql://localhost:3306/scholarship?autoReconnect=true</value> </parameter>
</ResourceParams>

这样的写法才ok。真是ft,怪不得那么多人不肯用tomcat的连接池而要去编码获取连接

from tomcat to resin,一点体会

下午折腾了很久的服务器配置,写点备忘

起因是信息网那套tag里面的boardList在tomcat下没法用,因为有个pClassCode这样的属性,tomcat里面会报错Unable
to find setter method。google一番查到一个解释如下:
Q. I have a bean with a property whose second letter is capitalized.

Why won ‘t my JSP page that uses this bean compile?

A. This may not happen often, but can be difficult to determine why.

The reason is found in the Java Beans specification, where in section

"8.8 Capitalization of inferred names " it states:

Thus when we extract a property or event name from the middle of an

existing Java name, we normally convert the first character to lower

case. However to support the occasional use of all upper-case names,

we check if the first two characters of the name are both upper case

and if so leave it alone.

This means that if you have a bean with a setter method of "setXLoc ",

then the inferred property is "XLoc ", not "xLoc ". If you used this

bean in a JSP page and you tried to use "xLoc " as the property, it

would not compile. Using "XLoc " as the property would succeed.

If you insist on using "xLoc " on the JSP page, you can make this
possible

by creating a BeanInfo class for the bean. The following is an example

of such a BeanInfo class for a simple bean called Coordinate. It

explicitly defines the properties of the bean to be "xLoc " and "yLoc
".

import java.beans.*;

public class CoordinateBeanInfo extends SimpleBeanInfo

{

private final static Class beanClass = Coordinate.class;

public PropertyDescriptor[] getPropertyDescriptors()

{

try {

PropertyDescriptor xLocDesc new PropertyDescriptor( "xLoc ",beanClass,
"getXLoc ", "setXLoc ");

PropertyDescriptor yLocDesc new PropertyDescriptor( "yLoc ",beanClass,
"getYLoc ", "setYLoc ");

PropertyDescriptor [] pdv = { xLocDesc, yLocDesc };

return pdv;

} catch (IntrospectionException e) {

throw new Error(e.toString());

}

}

}

尝试这里面提到的这个附加类的方法,不知所云。没有tag的源码,不能改属性名。无奈之下改用resin。
一直不用resin的原因主要是intellij默认整合了tomcat,调试起来比较爽。以前找到过一个resinplugin,但是只能用在
intellij4.0上(我现在用的是4.5)。开始google,惊奇的发现那个resinplugin的project已经发布2.9.2版了(我
上次用的还是1.0.2版-_-),下载,不大知道怎么用。翻看intellij的官方Forum,发现一个很奇怪的方法可以work:)
很好很好。大概的思路就是把resin的Jar导入,然后当作一个Application来运行ResinServer类的main方法。写好conf和
各种参数就ok了。原文如下:

I just
set it up as a regular application, like this:

Main class: com.caucho.server.http.ResinServer

VM parameters:
-Dcom.sun.management.jmxremote
-Dresin.home=.
-Dlog4j.configuration=c:/dev/projects/template/webapp/conf/log4j.properties
-Xdebug -Xnoagent -Djava.compiler=NONE
-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=12345
-Dfile.encoding=UTF-8 -XX:PermSize=64M -XX:MaxPermSize=64M

Program parameters:
-conf resin.xml

Working directory:
C:\dev\projects\template\webapp

Use classpath and JDK of module:
webapp

Then I can "run" it or "debug" it with no problem and it logs directly
to the console in IDEA.


可怜plugin的那个作者在下面无奈的说”I know that the need for a
plugin is not that big…“,哈哈

然后又遇到过一个问题,就是resin说什么找不到jstl的类。。折腾了半天,把代码里的jstl都去掉,remove掉intellij里面的各个地
方加上的jstl的类库,remove掉docbase里面的jstl库,ok了。resin2.x版本都不支持jstl的tld的xml
schema。。所以没法在resin2.x里面用jstl。配一个resin3又太麻烦了。(我现在还没空学这个)

resin爽的地方有两个,一个是特别快,启动和编译页面的速度都好过tomcat不少;二是没有tomcat那个莫名其妙的server
path和context
path混淆的问题,tomcat里面写/开头的路径都会解释成server-dependent的(好像应该叫server-side?),弱智的
bug。。。

嗯,记录完毕

几个经验。。

 Intellij4.5下面配置tomcat,发现没法改动server.xml。Intellij总是在自己的临时目录底下开一个目录,建一套conf,我设置了tomcat base也不起作用。一怒之下看到它还是从catalina的一个startup.bat之类的bat文件开始启动tomcat的,就直接去改了那个bat,把calatina_base环境变量强制置成tomcat的安装目录(不读启动参数了),于是搞定。。

JSP开发中遇到了中文问题。。解决方法如下,所有页面设置成GB2312,每个页面读request参数之前request.setCharacterEncoding("GB2312"),注意一定要在开始读参数之前设置。。刚遇上这个问题的时候就是在读取参数之后才设置编码,结果不行,提交中文信息的表单一律用POST方法(这个倒是我试出来的,估计要用GET方法的话要对url做一下encoding)。ok