本文共 4145 字,大约阅读时间需要 13 分钟。
这次要连数据库建表及插入的数据都列表,
推导和理解起来,就通畅多了。
建表及插入数据:
CREATE TABLE tb_clazz( id INT PRIMARY KEY AUTO_INCREMENT, CODE VARCHAR(18), NAME VARCHAR(18));INSERT INTO tb_clazz(CODE, NAME) VALUES('j1601', 'Java');INSERT INTO tb_clazz(CODE, NAME) VALUES('j1602', 'Python');INSERT INTO tb_clazz(CODE, NAME) VALUES('j1603', 'Javascript');INSERT INTO tb_clazz(CODE, NAME) VALUES('j1604', 'Go');CREATE TABLE tb_student( id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(18), SEX CHAR(18), AGE INT, clazz_id INT, FOREIGN KEY (clazz_id) REFERENCES tb_clazz(id));INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('jack', 'M', 22, 1);INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('rose', 'F', 18, 1);INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('tom', 'M', 25, 2);INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('mary', 'F', 20, 2);INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('tommy', 'M', 25, 4);INSERT INTO tb_student(NAME, SEX, AGE, clazz_id) VALUES('jacky', 'F', 20, 4);clazz.java
package org.fkit.domain;import java.util.List;public class Clazz { private Integer id; private String code; private String name; private ListStudent.javastudents; public Integer getId() { return id; } public void setId(Integer 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 List getStudents() { return students; } public void setStudents(List students) { this.students = students; } @Override public String toString() { return "Clazz [id=" + id + ", code=" + code + ", name=" + name + "]"; } }
package org.fkit.domain;public class Student { private Integer id; private String name; private String sex; private Integer age; private Clazz clazz; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Clazz getClazz() { return clazz; } public void setClazz(Clazz clazz) { this.clazz = clazz; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }ClassMapper.xml
StudentMapper.xml
OneToManyTest.java
package org.fkit.tst;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.fkit.domain.Clazz;import org.fkit.domain.Student;import org.fkit.factory.FKSqlSessionFactory;import org.fkit.mapper.ClazzMapper;import org.fkit.mapper.StudentMapper;public class OneToManyTest { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub SqlSession session = FKSqlSessionFactory.getSqlSession(); OneToManyTest t = new OneToManyTest(); t.testSelectClazzById(session); t.testSelectStudentById(session); session.commit(); session.close(); } public void testSelectClazzById(SqlSession session) { ClazzMapper cm = session.getMapper(ClazzMapper.class); Clazz clazz = cm.selectClazzById(4); System.out.println(clazz.getId() + " " + clazz.getCode() + " " + clazz.getName()); Liststudents = clazz.getStudents(); for (Student stu : students) { System.out.println(stu); } } public void testSelectStudentById(SqlSession session) { StudentMapper sm = session.getMapper(StudentMapper.class); Student stu = sm.selectStudentById(1); System.out.println(stu); System.out.println(stu.getClazz()); }}
转载地址:http://rnsoa.baihongyu.com/