I have a table: -\frac{1}{12}x^2+9
and y=6
.
The y=9
table contains column Person(int (primary key), varchar, varchar, varchar)
- it is varchar which indicates type of person. Person of different types has different skills - so Skill(int (references to person id), varchar, integer)
table. No, in my Java classes I have abstract model of Person
and concrete kinds, lets call them kind
.
Now, during retrieving data by mybatis I need create actual instances of class and return Skill
. I have no idea how to reach this effect. Can you help me, please ? Keep in mind that Person
will be joined with A,B,C
, so different instances in list has different attributes.
答案 0 :(得分:1)
让我们阅读MyBatis ducuments,MyBatis3 - Mapper XML Files。 对于这种O / R Mapper,您需要创建映射器文件,这些文件指示实际的模型类。之后,您应该编写一些配置来连接表。
<强> DDL 强>
我猜您的表设置如下:
CREATE TABLE `person` (
`id` INT(10)
, `first_name` VARCHAR(18)
, `last_name` VARCHAR(18)
, `kind` VARCHAR(1)
, `age` INT);
CREATE TABLE `skill` (
`id` INT(10)
, `person_id` INT(10)
, `desc` VARCHAR(200)
);
INSERT INTO `person`(`id`, `first_name`, `last_name`, `kind`, `age`)
VALUES (1, 'John', 'Smith', 'E', 33)
, (2, 'Jane', 'Smith', 'A', 24)
, (3, 'Jonny', 'Smith', 'B', 55)
, (4, 'J', 'J', 'T', 45)
, (5, 'Alan', 'Turing', 'C', 4);
INSERT INTO `skill`(`id`, `person_id`, `desc`)
VALUES (1, 1, 'John Smith is Engineer')
, (2, 2, 'Jane Smith is Artist')
, (3, 3, 'Jonny Smith is Butcher')
, (4, 4, 'J.J is Teacher')
, (5, 5, 'Alan Turing is Computer');
配置完成后,您可以使用密钥连接两个表。这是你的Pokemon
列表。
SELECT * FROM `person` p
LEFT JOIN `skill` s
ON s.person_id = p.id;
SQLFiddle链接→SQL Fiddle
我相信您的person_id
可以唯一地确定记录。
模型类
人类
// @Data <-- lombok can generate getter/setter easily
public class Person {
private Integer id;
private String firstName;
private String lastName;
private String kind; // <-- it might be better to use enum
private Integer age;
// with getter/setter or annotations
}
技能课
// @Data
public class Skill {
private Integer id;
private Integer personId;
private String desc;
// with getter/setter or annotations
}
加入PersonSkill类
@Data
public class PersonSkill {
private Integer id;
private String firstName;
private String lastName;
private String kind;
private Integer age;
private Integer skillId;
private String desc;
}
映射文件
将结果集保存为模型类的XML配置
<强> PersonSkillMapper.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="jp.gr.java_conf.hangedman.mybatis_join_sample.mappers.PersonSkillMapper">
<resultMap id="personSkillResultMap" type="PersonSkill">
<result property="id" column="id" />
<result property="firstName" column="first_name" />
<result property="lastName" column="last_name" />
<result property="kind" column="kind" />
<result property="age" column="age" />
<result property="skillId" column="skill_id" />
<result property="desc" column="desc" />
</resultMap>
<select id="selectList" resultMap="personSkillResultMap">
SELECT
p.*
, s.id AS skill_id
, s.desc AS desc
FROM `person` p
LEFT JOIN `skill` s
ON s.person_id = p.id
</select>
</mapper>
生成强>
public class App {
public static void main(String[] args) throws IOException {
try (InputStream in = App.class.getResourceAsStream("/mybatis-config.xml")) {
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
try (SqlSession session = factory.openSession()) {
List<PersonSkill> pskills = session
.selectList("jp.gr.java_conf.hangedman.mybatis_join_sample.mappers.PersonSkillMapper.selectList");
for (PersonSkill ps : pskills) {
System.out.println(ps.toString());
}
}
}
}
}
<强> PS 强>
如果您想创建一些变量属性,可能需要使用Generics
。如果你想使用它,请发表评论。