Oracle基础:bean的属性类型


 1、 ibatis中会通过resultMap把bean的类型与Oracle的列对应起来,如下面的例子:

  Xml代码

  <resultMap id="userMap" class="userBean" >

  <result column="ID" property="id"  />

  <result column="CIP_NAME" property="name"  />

  <result column="CIP_SEX" property="sex"  />

  <result column="CIP_LANGUAGE" property="language"  />

  <result column="CIP_studyLever" property="studyLever"  />

  <result column="CIP_post" property="post"  />

  <result column="CIP_DESC" property="desc"  />

  <result column="CIP_BIRTHDAY" property="birthday"  />

  </resultMap>

  经过测试,以id列为例,不管在oracle表中,id列是varchar、number,在bean中id列可以定义为String、Integer类型,这句话的意思是id列为varhcar类型时,可以将bean中的id定义为String、Integer类型,id列为number类型时,也可以将bean中的id定义为String、Integer类型。

  2、当bean中的属性定义从String改为Integer或者从Integer改为String时,需要改变的是以下的配置:

  Xml代码

  <insert id="insertUser" parameterClass="UserBean">

  <selectKey keyProperty="id" resultClass="java.lang.String">

  select CIP_Test_User_seq.nextval as id from dual

  </selectKey>

  insert into

  CIP_Test_User(

  ID,

  CIP_NAME,

  CIP_SEX,

  CIP_LANGUAGE,

  CIP_studyLever,

  CIP_post,

  CIP_DESC,

  CIP_BIRTHDAY

  )

  values(

  #id#,

  #name#,

  #sex#,

  #language#,

  #studyLever#,

  #post#,

  #desc#,

  #birthday#

  )

  </insert>

  如果bean中的类型是Integer,则需要把 resultClass="java.lang.Integer";

  如果bean中的类型是String ,则需要把 resultClass="java.lang.String ";

  3、日期的处理:bean中定义的类型是java.utils.Date

  日期需要在插入的时候做一个转换,从String转为Date;在JSP中显示的时候,使用fmt作一个转换。

  在ibatis中不需要特殊处理。

  (1)库中insert数据,在action里进行处理:

  Java代码

  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

  String birthday=(String) dynaBean.get("birthday");

  if(StringUtils.isNotBlank(birthday)){ //注意这里一定要判断为空,如果为空,转换的时候会出错。

  userBean.setBirthday(format.parse(birthday));

  }

  (2)在JSP中显示日期时:

  Java代码

  <fmt:formatDate value='${userBean.birthday}' pattern='yyyy-MM-dd'/>


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3