一、往数据库中存储当前时间,首先应该想到的是有两种方案,一种是使用oracle sysdate 实现,如ibatis的写法:
<insert id="insertColumnSetup" parameterClass="ColumnSetupBean"> <selectKey keyProperty="id" resultClass="java.lang.Integer"> select CIP_FIELDINFO_SEQ.nextval as id from dual </selectKey> insert into CIP_FIELDINFO ( FIELDID, CIP_USERID, CIP_SETDATE, CIP_FIELDS ) values ( #id#, #userId#, sysdate, #fields# ) </insert>
另一种是在action中生成,
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd"); Date date = new java.util.Date(); String str = s.format(date); System.out.println(str);
上面的代码是把当前日期按照要的格式format,但是format转化为字符串。
如果想再转为Date 对象,则需执行:
Date date1= s.parse(str); System.out.println(date1);
可惜输出来的结果为:Sun Aug 03 00:00:00 CST 2008 不是我要的格式啊,等于转了半天有退回到原始的new Date()数据了;
???????????究竟该怎么生成当前的时间,并转为我想要的格式,而且保证最后的结果还是Date型呢?
二、id 如果想使用ibaits的
<selectKey keyProperty="id" resultClass="java.lang.Integer"> select CIP_FIELDINFO_SEQ.nextval as id from dual </selectKey>
生成,则id 的类型必须为int.
三、ibatis配置文件中用到的javaBean必须带有无参的构造方法。
四、如果ibatis 的select操作返回的结果是一个对象,如果该bean的属性与对应表的列名是相同的则可以直接使用resultClass,如果两者不同,则需使用resultMap
1、先定义resultMap元素
<resultMap id="ColumnSetupBeanMap" class="ColumnSetupBean"> <result property="id" column="FIELDID"/> <result property="userId" column="CIP_USERID"/> <result property="setDate" column="CIP_SETDATE"/> <result property="fields" column="CIP_FIELDS"/> </resultMap>
2、再在select中
<select id="selectColumnSetup" parameterClass="java.lang.String" resultMap="ColumnSetupBeanMap"> select * from CIP_FIELDINFO where CIP_USERID=#userId# </select>
使用