Reputation: 631
In my Spring Boot project I am using MyBatis. I want to know how to use one to one mapping and how to created POJOs for the mapping classes. This is the SQL dump I have used:
CREATE TABLE IF NOT EXISTS LANG(
ID BIGINT(10) NOT NULL AUTO_INCREMENT,
CODE VARCHAR(3) UNIQUE,
NAME VARCHAR(150),
PRIMARY KEY (ID)
);
CREATE TABLE IF NOT EXISTS PROVINCE(
ID BIGINT(10) NOT NULL AUTO_INCREMENT,
CODE VARCHAR(10) UNIQUE,
NAME VARCHAR(200),
LANG_CODE VARCHAR(3),
PRIMARY KEY (ID),
FOREIGN KEY(LANG_CODE) REFERENCES LANG(CODE)
);
and POJOs are,
Language.java
public class Language {
private long id;
private String code;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Province
public class Province {
private long id;
private String code;
private String name;
private String lang_code;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLang_code() {
return lang_code;
}
public void setLang_code(String lang_code) {
this.lang_code = lang_code;
}
}
Upvotes: 0
Views: 4647
Reputation: 16106
You should change your entity Province including Language property instead of repeat it, it would be as next:
public class Province {
private long id;
private Language language;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Language getLanguage() {
return language;
}
public void setLanguage(Language language) {
this.language = language;
}
}
On other hand you need to create Mapper with a Language select operation searching by code and the select of province you should binding. You may look at the next example using annotations:
public interface MapperProvince {
@Select("select * from PROVINCE")
@Results({
@Result(id=true, property = "id", column = "ID"),
@Result(property="language", column="LANG_CODE",
one=@One(select="com.example.MapperProvince.findByCode"))
})
public List<Province> findAll();
@Select("select * from PROVINCE where ID = {id}")
@Results({
@Result(id=true, property = "id", column = "ID"),
@Result(property="language", column="LANG_CODE",
one=@One(select="com.example.MapperProvince.findByCode"))
})
public Province findById(long id);
@Select("select * from LANGUAGE where CODE = {code}")
@Results({
@Result(id=true, property = "id", column = "id"),
@Result(property = "code", column = "code"),
@Result(property = "name", column = "name")
})
public Language findByCode(String code);
}
As you see, there is a Language operation searching by code which it is linked in Table Province. On other hand when you search Province your Result for language property should call the Language findByCode operation linking both. I have put an example searching all the Provinces or just by ID.
Upvotes: 2
Reputation: 4274
Setup using mybatis-spring-boot-starter (https://github.com/mybatis/spring-boot-starter)
LanguageMapper.java
@Mapper
public class LanguageMapper{
List<Language> queryLang();
}
LanguageMapper.xml
<select id="queryLang" resultType="Language">
select id,
code,
name
from LANG
</select>
Upvotes: -1