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 클래스
- import java.util.Date;
-
- /**
- *
- */
- public class AppModelVersion {
- /**
- *
- */
- private AppModel appModel;
- /**
- *
- */
- private User createUser;
- /**
- *
- */
- private Integer identityId;
- /**
- * 。 , :“ _YYYYMMDD_V ”, :en_10k_20101102_v1
- */
- private String name;
- /**
- *
- */
- private String versionDescription;
- /**
- *
- */
- private String trainingDataDescription;
- /**
- *
- */
- private String remark;
- /**
- *
- */
- private String serverWorkingDir;
- /**
- * : prepared -- verify -- success -- failure --
- */
- private String status;
- /**
- *
- */
- private Date createTime;
- /**
- *
- */
- private Date modifiedTime;
-
- public AppModel getAppModel() {
- return appModel;
- }
-
- public void setAppModel(AppModel appModel) {
- this.appModel = appModel;
- }
-
- public User getCreateUser() {
- return createUser;
- }
-
- public void setCreateUser(User createUser) {
- this.createUser = createUser;
- }
-
- public Integer getIdentityId() {
- return this.identityId;
- }
-
- public void setIdentityId(Integer identityId) {
- this.identityId = identityId;
- }
-
- public String getName() {
- return this.name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getVersionDescription() {
- return this.versionDescription;
- }
-
- public void setVersionDescription(String versionDescription) {
- this.versionDescription = versionDescription;
- }
-
- public String getTrainingDataDescription() {
- return this.trainingDataDescription;
- }
-
- public void setTrainingDataDescription(String trainingDataDescription) {
- this.trainingDataDescription = trainingDataDescription;
- }
-
- public String getRemark() {
- return this.remark;
- }
-
- public void setRemark(String remark) {
- this.remark = remark;
- }
-
- public String getServerWorkingDir() {
- return this.serverWorkingDir;
- }
-
- public void setServerWorkingDir(String serverWorkingDir) {
- this.serverWorkingDir = serverWorkingDir;
- }
-
- public String getStatus() {
- return this.status;
- }
-
- public void setStatus(String status) {
- this.status = status;
- }
-
- public Date getCreateTime() {
- return this.createTime;
- }
-
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
-
- public Date getModifiedTime() {
- return this.modifiedTime;
- }
-
- public void setModifiedTime(Date modifiedTime) {
- this.modifiedTime = modifiedTime;
- }
-
- }
AppModel :
- public class AppModel {
- /**
- *
- */
- private Set
versions;
- /**
- *
- */
- private Integer identityId;
- /**
- * , 。 , : _ _ 。 :CBU_EMAIL_EDM。 :1、
- * , :ipr,email_edm 2、 3、 appCode 4、
- * ,
- */
- private String name;
- /**
- *
- */
- private String website;
- /**
- *
- */
- private String application;
- /**
- *
- */
- private String useDescription;
- /**
- *
- */
- private String remark;
- /**
- * , true。
- */
- private boolean activated = true;
- /**
- *
- */
- private Date createTime;
- /**
- *
- */
- private Date modifiedTime;
-
- public Set
getVersions() {
- return versions;
- }
-
- public void setVersions(Set
versions) {
- this.versions = versions;
- }
-
- public Integer getIdentityId() {
- return this.identityId;
- }
-
- public void setIdentityId(Integer identityId) {
- this.identityId = identityId;
- }
-
- public String getName() {
- return this.name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getWebsite() {
- return this.website;
- }
-
- public void setWebsite(String website) {
- this.website = website;
- }
-
- public String getApplication() {
- return this.application;
- }
-
- public void setApplication(String application) {
- this.application = application;
- }
-
- public String getUseDescription() {
- return this.useDescription;
- }
-
- public void setUseDescription(String useDescription) {
- this.useDescription = useDescription;
- }
-
- public String getRemark() {
- return this.remark;
- }
-
- public void setRemark(String remark) {
- this.remark = remark;
- }
-
- public boolean isActivated() {
- return this.activated;
- }
-
- public void setActivated(boolean activated) {
- this.activated = activated;
- }
-
- public Date getCreateTime() {
- return this.createTime;
- }
-
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
-
- public Date getModifiedTime() {
- return this.modifiedTime;
- }
-
- public void setModifiedTime(Date modifiedTime) {
- this.modifiedTime = modifiedTime;
- }
-
- }
User :
- public class User implements UserDetails {
- /**
- *
- */
- private Integer identityId;
-
- /**
- *
- */
- private String loginName;
- /**
- *
- */
- private String nickName;
- /**
- *
- */
- private boolean activated;
- /**
- *
- */
- private Date createTime;
- /**
- *
- */
- private Date modifiedTime;
-
- /**
- *
- */
- private String password;
-
- /**
- *
- */
- private Set
roles = new HashSet();
-
- public Integer getIdentityId() {
- return identityId;
- }
-
- public void setIdentityId(Integer identityId) {
- this.identityId = identityId;
- }
-
- public String getLoginName() {
- return loginName;
- }
-
- public void setLoginName(String loginName) {
- this.loginName = loginName;
- }
-
- public String getNickName() {
- return nickName;
- }
-
- public void setNickName(String nickName) {
- this.nickName = nickName;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public boolean isActivated() {
- return activated;
- }
-
- public void setActivated(boolean activated) {
- this.activated = activated;
- }
-
- public Date getCreateTime() {
- return this.createTime;
- }
-
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
-
- public Date getModifiedTime() {
- return this.modifiedTime;
- }
-
- public void setModifiedTime(Date modifiedTime) {
- this.modifiedTime = modifiedTime;
- }
-
- /**
- * UserDetails
- */
- public GrantedAuthority[] getAuthorities() {
- UserRole defaultRole = new UserRole("USER");
- if (!roles.contains(defaultRole)) {
- roles.add(defaultRole);
- }
- GrantedAuthority[] authorities = new GrantedAuthority[this.roles.size()];
- return this.roles.toArray(authorities);
- }
-
- /**
- * UserDetails
- */
- public String getPassword() {
- return this.password;
- }
-
- /**
- * UserDetails
- */
- public String getUsername() {
- return this.loginName;
- }
-
- /**
- * UserDetails
- */
- public boolean isAccountNonExpired() {
- //
- return true;
- }
-
- /**
- * UserDetails
- */
- public boolean isAccountNonLocked() {
- //
- return true;
- }
-
- /**
- * UserDetails
- */
- public boolean isCredentialsNonExpired() {
- return true;
- }
-
- /**
- * UserDetails
- */
- public boolean isEnabled() {
- return this.activated;
- }
-
- public String getInfo() {
-
- StringBuffer sb = new StringBuffer().append("name = " + loginName);
- return sb.toString();
-
- }
-
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (obj == this) {
- return true;
- }
- if (!(obj instanceof User)) {
- return false;
- }
- User user = (User) obj;
- return new EqualsBuilder().append(loginName, user.loginName).isEquals();
- }
-
- public int hashCode() {
-
- return new HashCodeBuilder(17, 37).append(loginName).toHashCode();
- }
-
- }
- <insert id="insert" parameterClass="AppModelVersion">
- insert into app_model_version(user_login_name,app_model_id,name,version_description,training_data_description,remark,
- server_working_dir,status,gmt_created,gmt_modified) values (#createUser.loginName:VARCHAR#,
- #appModel.identityId:DECIMAL#,#name:VARCHAR#,#versionDescription:VARCHAR#,#trainingDataDescription:VARCHAR#,#remark:VARCHAR#,
- #serverWorkingDir:VARCHAR#,#status:VARCHAR#,now(),now())
- insert>
3.작업 중의 인 스 턴 스
- <sqlMap namespace="modules.company">
- <typeAlias alias="address" type="com.alibaba.druid.domain.Address" />
- <typeAlias alias="employee" type="com.alibaba.druid.domain.Employee" />
- <typeAlias alias="company" type="com.alibaba.druid.domain.Company" />
-
- <resultMap id="companyResult" class="company">
- <result property="cid" column="cid" />
- <result property="companyId" column="companyId" />
- <result property="companyName" column="companyName" />
- <result property="officeAddress" column="officeAddress" select="selectAddr" />
- <result property="registryMoney" column="registryMoney" />
- <result property="companyOwner" column="companyOwner" select="selectEmp" />
- resultMap>
-
- <resultMap id="employeeResult" class="employee">
- <result property="eid" column="eid" />
- <result property="employeeId" column="employeeId" />
- <result property="employeeName" column="employeeName" />
- <result property="employeeType" column="employeeType" />
- <result property="salary" column="salary" />
- <result property="address" column="addressId" select="selectAddr" />
- <result property="telePhone" column="telePhone" />
- <result property="masterId" column="masterId" />
- resultMap>
-
- <select id="selectEmp" parameterClass="int" resultMap="employeeResult">
- select
- eid, employeeId,employeeName,employeeType,salary,addressId,telePhone,
- masterId from employee where
- eid = #eid#
- select>
-
- <select id="selectAddr" parameterClass="int" resultClass="address">
- select
- addressId, province,community,street,houseNumber from address where
- addressId = #addressId#
- select>
-
- <insert id="insertCompany" parameterClass="company">
- insert into
- Company(cid,companyId, companyName, officeAddress,
- registryMoney,
- companyOwner) values(#cid#,#companyId#, #companyName#,
- #officeAddress.addressId#,
- #registryMoney#, #companyOwner.eid#);
- insert>
-
- sqlMap>
[주의]
1.officeAddress.addressId 의 officeAddress 는 자바 프로그램의 속성 을 말 합 니 다.표 의 필드 가 아니 라 addressId 는 address 류 의 addressId 속성 을 말 합 니 다.이 번 호 는 데이터베이스 에 대응 하 는 필드 를 삽입 하면 됩 니 다.이 필드 의 외 키 는 address 표 의 addressId 입 니 다.
2.copany Owner.eid 는 위 와 도 비슷 한 상황 입 니 다.
3.resultMap 에서 copany 표 와 address 표 와 employee 표 간 의 관 계 를 명확 하 게 정의 해 야 합 니 다.select="selectAddr"속성 을 통 해 표시 합 니 다.selectAddr 는 select 를 통 해 명확 하 게 정의 합 니 다.
4.트 랜 잭 션 스크롤 백
체크 표 의 내용 을 추가 로 삭제 하 는 스크롤 백 을 지원 합 니 다.데이터 베 이 스 를 만 드 는 표 의 스크롤 백 동작 은 지원 되 지 않 습 니 다.예 를 들 어 표를 만 드 는 스크롤 백 등 입 니 다.
- <sqlMap namespace="modules.sql">
- <statement id="createTableOfAllType" parameterClass="String">
- CREATE TABLE $tableName$ (F1 FLOAT, F2 DOUBLE, F3 REAL, F4 DATE, F5 TIME, F6 SMALLINT, F7 SMALLINT,
- F8 INTEGER, F9 BIGINT, F10 DECIMAL(9,2), F11 TIMESTAMP, F12 BLOB, F13 VARCHAR(256),
- F14 VARCHAR(256), F15 VARCHAR(256), F16 VARCHAR(256), F17 SMALLINT, F18 BLOB)
- statement>
- sqlMap>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
양식 제출 후 제출 버튼 비활성화텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.