一、查询复杂对象

对于有外键的主表映射,在查询返回结果时要使用resultmap进行映射,并在propety里使用select属性进行子查询。


public class PetitionLetter {
	private int id;                              
	private String identifier;                   
	private Reporter reporter;         //外键,数据库里是int类型          
	private LetterInformation letterInformation;   //外键,数据库里是int类型
	private Supervision supervision;     //外键,数据库里是int类型    
	private ProcessFlow processFlow;     //外键,数据库里是int类型
}



<sqlMap namespace="PetitionLetter">
  <typeAlias alias="PetitionLetter" type="com.cs.jfb.input.model.PetitionLetter"/>
  <typeAlias alias="ProcessFlow" type="com.cs.jfb.process.model.ProcessFlow"/>
  <typeAlias alias="Reporter" type="com.cs.jfb.input.model.Reporter"/>
  <typeAlias alias="LetterInformation" type="com.cs.jfb.input.model.LetterInformation"/>
  <typeAlias alias="Supervision" type="com.cs.jfb.supervision.model.Supervision"/>
 
  <resultMap id="PetitionLetterResult" class="PetitionLetter">
    <result property="id" column="id"/>
    <result property="identifier" column="identifier"/>
    <result property="reporter" column="reporter" select="selectReporter"/>
    <result property="letterInformation" column="letterInformation" select="selectLetterInformation"/>
    <result property="supervision" column="supervision" select="selectSupervision"/>
    <result property="processFlow" column="processFlow" select="selectProcessFlow"/>
  </resultMap>

  <!-- Select with no parameters using the result map for Account class. -->
  <select id="selectAllPetitionLetter" resultMap="PetitionLetterResult">
    select * from t_petitionletter
  </select>
  
  <select id="selectProcessFlow" resultClass="ProcessFlow" parameterClass="int">
  	select * from t_processflow where id = #id#
  </select>
  
  <select id="selectReporter" resultClass="Reporter" parameterClass="int">
  	select * from t_reporter where id = #id#
  </select>
  
  <select id="selectLetterInformation" resultClass="LetterInformation" parameterClass="int">
  	select * from t_letterinformation where id = #id#
  </select>
  
  <select id="selectSupervision" resultClass="Supervision" parameterClass="int">
  	select * from t_supervision where id = #id#
  </select>
  
  <select id="selectPetitonLetterById" parameterClass="int" resultMap="PetitionLetterResult">
  	select * from t_petitionletter where id = #id#
  </select>
</sqlMap>



二、复杂对象的插入情况

由于数据库中的外键关系只是对其他表内容的引用,所以在进行插入时,只需要插入引用的外键关联值即可,不会在外键表中该条内容不存在的情况下再去插入一条记录, 这是不符合逻辑的。所以在插入的时候,只需要在插入的表中插入内容即可,外键引用的地方定位到具体的值即可,例如:appModel.identityId

AppModelVersion类

 
  
  1. import java.util.Date; 
  2.  
  3. /** 
  4.  * 应用模型版本 
  5.  */ 
  6. public class AppModelVersion { 
  7.     /** 
  8.      * 关联的模型 
  9.      */ 
  10.     private AppModel appModel; 
  11.     /** 
  12.      * 创建此模型版本的用户 
  13.      */ 
  14.     private User     createUser; 
  15.     /** 
  16.      * 系统标识 
  17.      */ 
  18.     private Integer  identityId; 
  19.     /** 
  20.      * 版本名称。系统自动生成,规则是:“模型名称_YYYYMMDD_V流水号”,比如:en_10k_20101102_v1 
  21.      */ 
  22.     private String   name; 
  23.     /** 
  24.      * 版本说明 
  25.      */ 
  26.     private String   versionDescription; 
  27.     /** 
  28.      * 训练样本描述 
  29.      */ 
  30.     private String   trainingDataDescription; 
  31.     /** 
  32.      * 备注 
  33.      */ 
  34.     private String   remark; 
  35.     /** 
  36.      * 服务器工作目录 
  37.      */ 
  38.     private String   serverWorkingDir; 
  39.     /** 
  40.      * 状态: prepared -- 准备 verify -- 验证中 success -- 验证成功 failure -- 验证失败 
  41.      */ 
  42.     private String   status; 
  43.     /** 
  44.      * 创建时间 
  45.      */ 
  46.     private Date     createTime; 
  47.     /** 
  48.      * 修改时间 
  49.      */ 
  50.     private Date     modifiedTime; 
  51.  
  52.     public AppModel getAppModel() { 
  53.         return appModel; 
  54.     } 
  55.  
  56.     public void setAppModel(AppModel appModel) { 
  57.         this.appModel = appModel; 
  58.     } 
  59.  
  60.     public User getCreateUser() { 
  61.         return createUser; 
  62.     } 
  63.  
  64.     public void setCreateUser(User createUser) { 
  65.         this.createUser = createUser; 
  66.     } 
  67.  
  68.     public Integer getIdentityId() { 
  69.         return this.identityId; 
  70.     } 
  71.  
  72.     public void setIdentityId(Integer identityId) { 
  73.         this.identityId = identityId; 
  74.     } 
  75.  
  76.     public String getName() { 
  77.         return this.name; 
  78.     } 
  79.  
  80.     public void setName(String name) { 
  81.         this.name = name; 
  82.     } 
  83.  
  84.     public String getVersionDescription() { 
  85.         return this.versionDescription; 
  86.     } 
  87.  
  88.     public void setVersionDescription(String versionDescription) { 
  89.         this.versionDescription = versionDescription; 
  90.     } 
  91.  
  92.     public String getTrainingDataDescription() { 
  93.         return this.trainingDataDescription; 
  94.     } 
  95.  
  96.     public void setTrainingDataDescription(String trainingDataDescription) { 
  97.         this.trainingDataDescription = trainingDataDescription; 
  98.     } 
  99.  
  100.     public String getRemark() { 
  101.         return this.remark; 
  102.     } 
  103.  
  104.     public void setRemark(String remark) { 
  105.         this.remark = remark; 
  106.     } 
  107.  
  108.     public String getServerWorkingDir() { 
  109.         return this.serverWorkingDir; 
  110.     } 
  111.  
  112.     public void setServerWorkingDir(String serverWorkingDir) { 
  113.         this.serverWorkingDir = serverWorkingDir; 
  114.     } 
  115.  
  116.     public String getStatus() { 
  117.         return this.status; 
  118.     } 
  119.  
  120.     public void setStatus(String status) { 
  121.         this.status = status; 
  122.     } 
  123.  
  124.     public Date getCreateTime() { 
  125.         return this.createTime; 
  126.     } 
  127.  
  128.     public void setCreateTime(Date createTime) { 
  129.         this.createTime = createTime; 
  130.     } 
  131.  
  132.     public Date getModifiedTime() { 
  133.         return this.modifiedTime; 
  134.     } 
  135.  
  136.     public void setModifiedTime(Date modifiedTime) { 
  137.         this.modifiedTime = modifiedTime; 
  138.     } 
  139.  
AppModel类:
 
  
  1. public class AppModel { 
  2.     /** 
  3.      * 模型的所有版本 
  4.      */ 
  5.     private Set<AppModelVersion> versions; 
  6.     /** 
  7.      * 系统标识 
  8.      */ 
  9.     private Integer              identityId; 
  10.     /** 
  11.      * 模型名称,唯一性。用户输入,格式:网站名_应用名_模型名。比如:CBU_EMAIL_EDM。注意:1、 
  12.      * 兼容老系统的模型名,比如:ipr,email_edm 2、 是模型存放得根目录名 3、 是应用方请求的appCode 4、 
  13.      * 此属性是唯一得,一旦确定就不能更改 
  14.      */ 
  15.     private String               name; 
  16.     /** 
  17.      * 网站 
  18.      */ 
  19.     private String               website; 
  20.     /** 
  21.      * 应用名称 
  22.      */ 
  23.     private String               application; 
  24.     /** 
  25.      * 模型用途 
  26.      */ 
  27.     private String               useDescription; 
  28.     /** 
  29.      * 备注 
  30.      */ 
  31.     private String               remark; 
  32.     /** 
  33.      * 是否激活,默认true。保留属性 
  34.      */ 
  35.     private boolean              activated = true
  36.     /** 
  37.      * 创建时间 
  38.      */ 
  39.     private Date                 createTime; 
  40.     /** 
  41.      * 修改时间 
  42.      */ 
  43.     private Date                 modifiedTime; 
  44.  
  45.     public Set<AppModelVersion> getVersions() { 
  46.         return versions; 
  47.     } 
  48.  
  49.     public void setVersions(Set<AppModelVersion> versions) { 
  50.         this.versions = versions; 
  51.     } 
  52.  
  53.     public Integer getIdentityId() { 
  54.         return this.identityId; 
  55.     } 
  56.  
  57.     public void setIdentityId(Integer identityId) { 
  58.         this.identityId = identityId; 
  59.     } 
  60.  
  61.     public String getName() { 
  62.         return this.name; 
  63.     } 
  64.  
  65.     public void setName(String name) { 
  66.         this.name = name; 
  67.     } 
  68.  
  69.     public String getWebsite() { 
  70.         return this.website; 
  71.     } 
  72.  
  73.     public void setWebsite(String website) { 
  74.         this.website = website; 
  75.     } 
  76.  
  77.     public String getApplication() { 
  78.         return this.application; 
  79.     } 
  80.  
  81.     public void setApplication(String application) { 
  82.         this.application = application; 
  83.     } 
  84.  
  85.     public String getUseDescription() { 
  86.         return this.useDescription; 
  87.     } 
  88.  
  89.     public void setUseDescription(String useDescription) { 
  90.         this.useDescription = useDescription; 
  91.     } 
  92.  
  93.     public String getRemark() { 
  94.         return this.remark; 
  95.     } 
  96.  
  97.     public void setRemark(String remark) { 
  98.         this.remark = remark; 
  99.     } 
  100.  
  101.     public boolean isActivated() { 
  102.         return this.activated; 
  103.     } 
  104.  
  105.     public void setActivated(boolean activated) { 
  106.         this.activated = activated; 
  107.     } 
  108.  
  109.     public Date getCreateTime() { 
  110.         return this.createTime; 
  111.     } 
  112.  
  113.     public void setCreateTime(Date createTime) { 
  114.         this.createTime = createTime; 
  115.     } 
  116.  
  117.     public Date getModifiedTime() { 
  118.         return this.modifiedTime; 
  119.     } 
  120.  
  121.     public void setModifiedTime(Date modifiedTime) { 
  122.         this.modifiedTime = modifiedTime; 
  123.     } 
  124.  
User类:
 
  
  1. public class User implements UserDetails { 
  2.     /** 
  3.      * 系统标识 
  4.      */ 
  5.     private Integer identityId; 
  6.  
  7.     /** 
  8.      * 登录名 
  9.      */ 
  10.     private String  loginName; 
  11.     /** 
  12.      * 称呼 
  13.      */ 
  14.     private String  nickName; 
  15.     /** 
  16.      * 是否有效 
  17.      */ 
  18.     private boolean activated; 
  19.     /** 
  20.      * 创建时间 
  21.      */ 
  22.     private Date    createTime; 
  23.     /** 
  24.      * 修改时间 
  25.      */ 
  26.     private Date    modifiedTime; 
  27.      
  28.     /** 
  29.      * 用户密码 
  30.      */ 
  31.     private String password; 
  32.      
  33.     /** 
  34.      * 用户角色列表 
  35.      */ 
  36.     private Set<UserRole> roles = new HashSet<UserRole>(); 
  37.      
  38.     public Integer getIdentityId() { 
  39.         return identityId; 
  40.     } 
  41.  
  42.     public void setIdentityId(Integer identityId) { 
  43.         this.identityId = identityId; 
  44.     } 
  45.  
  46.     public String getLoginName() { 
  47.         return loginName; 
  48.     } 
  49.  
  50.     public void setLoginName(String loginName) { 
  51.         this.loginName = loginName; 
  52.     } 
  53.      
  54.     public String getNickName() { 
  55.         return nickName; 
  56.     } 
  57.  
  58.     public void setNickName(String nickName) { 
  59.         this.nickName = nickName; 
  60.     } 
  61.  
  62.     public void setPassword(String password) { 
  63.         this.password = password; 
  64.     } 
  65.  
  66.     public boolean isActivated() { 
  67.         return activated; 
  68.     } 
  69.  
  70.     public void setActivated(boolean activated) { 
  71.         this.activated = activated; 
  72.     } 
  73.  
  74.     public Date getCreateTime() { 
  75.         return this.createTime; 
  76.     } 
  77.  
  78.     public void setCreateTime(Date createTime) { 
  79.         this.createTime = createTime; 
  80.     } 
  81.  
  82.     public Date getModifiedTime() { 
  83.         return this.modifiedTime; 
  84.     } 
  85.  
  86.     public void setModifiedTime(Date modifiedTime) { 
  87.         this.modifiedTime = modifiedTime; 
  88.     } 
  89.      
  90.     /** 
  91.      * 实现UserDetails接口方法 
  92.      */ 
  93.     public GrantedAuthority[] getAuthorities() { 
  94.         UserRole defaultRole = new UserRole("USER"); 
  95.         if (!roles.contains(defaultRole)) { 
  96.             roles.add(defaultRole); 
  97.         } 
  98.         GrantedAuthority[] authorities = new GrantedAuthority[this.roles.size()]; 
  99.         return this.roles.toArray(authorities); 
  100.     } 
  101.      
  102.     /** 
  103.      * 实现UserDetails接口方法 
  104.      */ 
  105.     public String getPassword() {        
  106.         return this.password; 
  107.     } 
  108.      
  109.     /** 
  110.      * 实现UserDetails接口方法 
  111.      */ 
  112.     public String getUsername() { 
  113.         return this.loginName; 
  114.     } 
  115.      
  116.     /** 
  117.      * 实现UserDetails接口方法 
  118.      */ 
  119.     public boolean isAccountNonExpired() { 
  120.         //帐号永不过期 
  121.         return true
  122.     } 
  123.      
  124.     /** 
  125.      * 实现UserDetails接口方法 
  126.      */ 
  127.     public boolean isAccountNonLocked() { 
  128.         //帐号不锁定 
  129.         return true
  130.     } 
  131.      
  132.     /** 
  133.      * 实现UserDetails接口方法 
  134.      */ 
  135.     public boolean isCredentialsNonExpired() {       
  136.         return true
  137.     } 
  138.      
  139.     /** 
  140.      * 实现UserDetails接口方法 
  141.      */ 
  142.     public boolean isEnabled() { 
  143.         return this.activated; 
  144.     } 
  145.      
  146.     public String getInfo() { 
  147.          
  148.         StringBuffer sb = new StringBuffer().append("name = " + loginName); 
  149.         return sb.toString(); 
  150.          
  151.     } 
  152.      
  153.     public boolean equals(Object obj) { 
  154.         if (obj == null) { 
  155.             return false
  156.         } 
  157.         if (obj == this) { 
  158.             return true
  159.         } 
  160.         if (!(obj instanceof User)) { 
  161.             return false
  162.         } 
  163.         User user = (User) obj; 
  164.         return new EqualsBuilder().append(loginName, user.loginName).isEquals(); 
  165.     } 
  166.  
  167.     public int hashCode() { 
  168.  
  169.         return new HashCodeBuilder(1737).append(loginName).toHashCode(); 
  170.     } 
  171.  

 
  
  1. <insert id="insert" parameterClass="AppModelVersion"> 
  2.         insert into app_model_version(user_login_name,app_model_id,name,version_description,training_data_description,remark, 
  3.         server_working_dir,status,gmt_created,gmt_modified) values (#createUser.loginName:VARCHAR#, 
  4.         #appModel.identityId:DECIMAL#,#name:VARCHAR#,#versionDescription:VARCHAR#,#trainingDataDescription:VARCHAR#,#remark:VARCHAR#, 
  5.         #serverWorkingDir:VARCHAR#,#status:VARCHAR#,now(),now()) 
  6.     </insert> 






3、工作中的实例

 

 
  
  1. <sqlMap namespace="modules.company"> 
  2.     <typeAlias alias="address" type="com.alibaba.druid.domain.Address" /> 
  3.     <typeAlias alias="employee" type="com.alibaba.druid.domain.Employee" /> 
  4.     <typeAlias alias="company" type="com.alibaba.druid.domain.Company" /> 
  5.  
  6.     <resultMap id="companyResult" class="company"> 
  7.         <result property="cid" column="cid" /> 
  8.         <result property="companyId" column="companyId" /> 
  9.         <result property="companyName" column="companyName" /> 
  10.         <result property="officeAddress" column="officeAddress" select="selectAddr" /> 
  11.         <result property="registryMoney" column="registryMoney" /> 
  12.         <result property="companyOwner" column="companyOwner" select="selectEmp" /> 
  13.     </resultMap> 
  14.  
  15.     <resultMap id="employeeResult" class="employee"> 
  16.         <result property="eid" column="eid" /> 
  17.         <result property="employeeId" column="employeeId" /> 
  18.         <result property="employeeName" column="employeeName" /> 
  19.         <result property="employeeType" column="employeeType" /> 
  20.         <result property="salary" column="salary" /> 
  21.         <result property="address" column="addressId" select="selectAddr" /> 
  22.         <result property="telePhone" column="telePhone" /> 
  23.         <result property="masterId" column="masterId" /> 
  24.     </resultMap> 
  25.  
  26.     <select id="selectEmp" parameterClass="int" resultMap="employeeResult"> 
  27.         select 
  28.         eid, employeeId,employeeName,employeeType,salary,addressId,telePhone, 
  29.         masterId from employee where 
  30.         eid = #eid# 
  31.     </select> 
  32.  
  33.     <select id="selectAddr" parameterClass="int" resultClass="address"> 
  34.         select 
  35.         addressId, province,community,street,houseNumber from address where 
  36.         addressId = #addressId# 
  37.     </select> 
  38.  
  39.     <insert id="insertCompany" parameterClass="company"> 
  40.         insert into 
  41.         Company(cid,companyId, companyName, officeAddress, 
  42.         registryMoney, 
  43.         companyOwner) values(#cid#,#companyId#, #companyName#, 
  44.         #officeAddress.addressId#, 
  45.         #registryMoney#, #companyOwner.eid#); 
  46.     </insert> 
  47.  
  48. </sqlMap> 

【注意】

1、officeAddress.addressId中的officeAddress是指java程序中的属性,不是指表中的字段addressId是指address类中的addressId属性, 这个编号插入数据库对应的字段即可,该字段的外键为address表中的addressId。

2、companyOwner.eid和上面也是相似的情况。 

3、在resultMap中需要定义清楚company表与address表和employee表之间的关系。通过select="selectAddr"属性来表示。selectAddr再通过select来定义清楚。

4、事务的回滚

支持增删改查表中的内容的回滚,不支持数据库的表的回滚操作, 例如创建表的回滚。

 
  
  1. <sqlMap namespace="modules.sql"> 
  2.     <statement id="createTableOfAllType" parameterClass="String"> 
  3.         CREATE TABLE $tableName$ (F1 FLOAT, F2 DOUBLE, F3 REAL, F4 DATE, F5 TIME, F6 SMALLINT, F7 SMALLINT,  
  4.         F8 INTEGER, F9 BIGINT, F10 DECIMAL(9,2), F11 TIMESTAMP, F12 BLOB, F13 VARCHAR(256),  
  5.         F14 VARCHAR(256), F15 VARCHAR(256), F16 VARCHAR(256), F17 SMALLINT, F18 BLOB) 
  6.     </statement> 
  7. </sqlMap>