Categories

Links

ibatis.net 父子关系映射的写法

以区域对象为例子,通常,我们就创建这样一个数据表
create table area
(
  id int identity PRIMARY KEY ,name varchar(50),parentid int
)

对于这个对象,我们有一个非常oo的表达
 public class Area
 {
 
  private int id;
  private String name;
  private Area parent;
  private IList children;
  public int Id
  {
   get{return this.id;}
   set{this.id=value;}
  }

  public String Name
  {
   get{return this.name;}
   set{this.name=value;}
  }
  public Area Parent
  {
   set{this.parent=value;}
   get{return this.parent;}
  }
  public IList Children
  {
   get{return this.children;}
   set{this.children=value;}
  }
 }


这个例子能说明通常ormapping中最主要的一些内容,如

  • 一对多(一个区域有一个或多个子区域)
  • 一对一(一个区域最多有一个父区域)
  • use left outer join(一种针对一对一,提升性能的常见处理方法)
  • lazyloading(延迟读取)


通常,很容易完成id,name的映射,但parent,children呢,下面是一种处理方法
resultMap部分
<resultMap id="areaResult" class="Area">
 <result property="Id" column="id"/>
 <result property="Name" column="name"/>
 <result lazyLoad="true" property="Parent" select="getParent" column="parentid"/>
 <result lazyLoad="true" property="Children" select="getChildren" column="id"/>
</resultMap>


statement部分

 <selectid="getParent" parameterClass="int" resultMap="areaResult">
  select * from area where id=#value"
 </select>
 <select id="getChildren" parameterClass="int" resultMap="areaResult">
  select * from area where parentid=#value#
 </select>
 <select id="find" resultMap="areaResult">
  select * from area
 </select>

大家看到,由于lazyLoad设置为真 ,当调用area.Parent或是area.Children,才会发生查询,但显然,利用如下的设置 ,我们可以同时设置

parent属性

<resultMap id="parentResult" class="Area">
 <result property="Id" column="id2"/>
 <result property="Name" column="name2"/>
 <result lazyLoad="true" property="Children" column="id2" select="getChildren"/>
</resultMap>
<resultMap id="areaResult2" class="Area">
 <result property="Id" column="id"/>
 <result property="Name" column="Name"/>
 <result property="Parent" resultMapping="parentResult"/>
 <result lazyLoad="true" property="Children" column="id" select="getChildren"/>
</resultMap>
statement部分


 <select id="getChildren" parameterClass="int" resultMap="areaResult">
  select * from area where parentid=#value#
 </select>
 <select id="find" resultMap="areaResult">
  select area1.id as id,area2.name as name ,area2.id as id2,area2.name as name2 from
    area area1 left ouer join area area2
   on area1.parentid=area2.id
  
 </select>
注:事实上,在java版本中,可以这样写
<resultMap id="areaResult2" class="Area">
 <result property="Id" column="id"/>
 <result property="Name" column="Name"/>
 <result property="Parent.Id" column="id2"/>
 <result property="Parent.Name" column="name2"/>
 <result lazyLoad="true" property="Parent.Children" column="id2" select="getChildren"/>
 <result lazyLoad="true" property="Children" column="id" select="getChildren"/>
</resultMap>
这样,可以不用配置parentResult

插入和更新
在插入和更新中,由于允许使用#对象.属性#的inline parameter,所以可以写这样的sql
insert into area (name,parentid) values (#Name#,#Parent.Id#)

Update area set name=#Name# ,parentid=#Parent.Id# where id=#Id#


最后,介绍一些资源
星月夜 http://starrynight.blogdriver.com/starrynight/index.html
(作者翻译了java版本的ibatis 手册和教程)
http://blog.csdn.net/nuke/archive/2004/08/29/ibatisGuide.aspx
(可下载opendoc系列的ibatis开发指南)

ibatis 是比较适合.net社区习惯的工具,我建议使用,事实上,我已经放弃了nhibernate 的进一步学习

 

[2004-11-01 21:39:56 | Author:jiangjianxiao ] [] 5 comments

有点搞笑,又有点心酸

男友对我父亲有意见,好郁闷。怎么办……

经典论坛上的一个贴子,结局可能出乎大家意料,大家慢慢看吧。

老实说,我也有点像那个贴子中的“男友”,不过最终我还没有被抛弃(不过也不能保证永远不被抛弃),不过,我想,我也许是真的不够圆滑了,生活中很多事情不应该这么由性子去处理的。

由于胃一直不好,最近一直在休息,耽误了许多工作。还好大家都没有埋怨我(也可能是我没有听到吧:))。同时也建议大家一定要主要颈部和胃,这可是程序员两大容易出事情的地方。这几月来,我倒是养成了好习惯,每天中午必休息,开始练初中时练的那种养生内功,老实说,身体改变真的是很大的。大家不妨试试。

好久没有写blog了,就以此开始,大家一起继续努力吧。

[2004-11-01 15:15:32 | Author:jiangjianxiao ] [] 1 comments

vb 6 MVC方案

已被更好的方法所取代

[2004-09-02 17:23:34 | Author:jiangjianxiao ] [] 1 comments

整合NHibernate到Spring.Net (之一)

有这段文字的惟一原因是因为spring.net尚远远没有完成。因此,借鉴spring (java)的LocalSessionFactoryObject,我们在.net中创建一个LocalSessionFactoryObject

注:在Spring.Net的开发计划中,第一个版本仅会完成aop和ioc功能,即现在在cvs上大家看到的Spring.Context,Spring.AOP,Spring.Core三个项目。下面的Spring.Data是我自行创建的,用来迁移一些项目中所必须用到的东西。

LocalSessionFactoryObject的作用

LocalSessionFactoryObject将NHibernate的配置整合到Spring中,因此,你不在需要app.config中配置nhibernate的属性,也无需将hbm xml文件设置为嵌入

LocalSessionFactoryObject有两个重要属性。MappingResources是一个IList,是一系列hbm文件的列表,而HibernateProperties则是一个IDictonary,存放NHibernate的设置
<object name="mySessionFactory" class="Spring.Data.Hibernate.LocalSessionFactory,Spring.Data">
  <property name="MappingResources">
   <list>
    <value>customer.hbm.xml</value>
    <value>color.hbm.xml</value>
   </list>
  </property>
  <property name="HibernateProperties">
   <props>
    <prop key="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider</prop>
    <prop key="hibernate.dialect">NHibernate.Dialect.MsSql2000Dialect</prop>  
    <prop key="hibernate.connection.driver_class">NHibernate.Driver.SqlClientDriver</prop>
    <prop key="hibernate.connection.connection_string">Server=localhost;initial catalog=mis;User ID=sa;Password=;Min Pool Size=2</prop>
   </props>
  </property>
 </object>


LocalSessionFactoryObject实现了IFactoryObject接口,这意味着当从ioc获取LocalSessionFactory的实例是,将调用其GetObject方法,因此,大家对这段代码就不要有疑惑
ISessionFactory sessionFactory=(ISessionFactory)factory.GetObject("mySessionFactory");


LocalSessionFactory Object实现了IInitialObject接口,其AfterPropertiesSet方法在ioc配置属性后调用,非常简单
 public void AfterPropertiesSet()
  {
   if (sessionFactory==null)
   {
    Configuration cfg=new Configuration();
     
    cfg.AddProperties(hibernateProperties);

    foreach(String hbm in mappingResources)
     cfg.AddXmlFile(hbm);

   
    sessionFactory=cfg.BuildSessionFactory();
    
   }
  }

下面是一段测试代码

IObjectFactory factory=new XmlObjectFactory(System.IO.File.OpenRead("applicationcontext.xml"));
   Customer customer=new Customer();
   customer.Name="jjx";
   ISessionFactory sessionFactory=(ISessionFactory)factory.GetObject("mySessionFactory");
   ISession session=sessionFactory.OpenSession();
   ITransaction trans=session.BeginTransaction();
   session.Save(customer);
   trans.Commit();
   session.Close();
需要完整代码的可留言,我不在这里一一贴出来了

 

[2004-08-05 20:20:58 | Author:jiangjianxiao ] [] 2 comments

Spring.Net 初步

此文已更新,参见

http://www.soho-works.net/BLOG/298.asp

[2004-07-26 00:05:35 | Author:jiangjianxiao ] [] 1 comments

Total 91 Display 81 of 85
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Powered by Google App Engine