Ibatis-외부 키 연결


1.복잡 한 대상 조회
외부 키 가 있 는 메 인 맵 에 대해 서 는 결 과 를 되 돌려 줄 때 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  
}


  
  
  
  
  
 
  
    
    
    selectReporter"/>
    selectLetterInformation"/>
    selectSupervision"/>
    selectProcessFlow"/>
  

  
  
  
  
  
  
  
  
  
  
  
  





2.복잡 한 대상 의 삽입 상황
데이터베이스 에 있 는 외 키 관 계 는 다른 표 내용 에 대한 참조 일 뿐 삽입 할 때 인 용 된 외 키 관련 값 만 삽입 하면 됩 니 다.외 키 표 에 이 내용 이 존재 하지 않 는 상황 에서 기록 을 하나 더 삽입 하지 않 습 니 다.이것 은 논리 에 맞지 않 습 니 다.따라서 삽입 할 때 삽 입 된 표 에 내용 만 삽입 하면 됩 니 다.외부 키 가 인용 한 곳 에서 구체 적 인 값 을 찾 으 면 됩 니 다.예 를 들 어 appmodel.identity Id.
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 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 getVersions() { 
  46.         return versions; 
  47.     } 
  48.  
  49.     public void setVersions(Set 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 roles = new HashSet(); 
  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 는 자바 프로그램의 속성 을 말 합 니 다.표 의 필드 가 아니 라 addressId 는 address 류 의 addressId 속성 을 말 합 니 다.이 번 호 는 데이터베이스 에 대응 하 는 필드 를 삽입 하면 됩 니 다.이 필드 의 외 키 는 address 표 의 addressId 입 니 다.
2.copany Owner.eid 는 위 와 도 비슷 한 상황 입 니 다. 
3.resultMap 에서 copany 표 와 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> 

 
 
 
 
 
 

좋은 웹페이지 즐겨찾기