博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis中的一对多及多对一举例
阅读量:6334 次
发布时间:2019-06-22

本文共 4145 字,大约阅读时间需要 13 分钟。

这次要连数据库建表及插入的数据都列表,

推导和理解起来,就通畅多了。

adea227c0357b072d69added026f4d6eeb5b512f

建表及插入数据:

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 List
students; 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 + "]"; } }
Student.java

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()); 		List
students = 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()); }}
f88a9d87da3b47c5ebe73c1aa52f14884a9a6eee

转载地址:http://rnsoa.baihongyu.com/

你可能感兴趣的文章
《Photoshop蒙版与合成(第2版)》目录—导读
查看>>
《团队软件过程(修订版)》—第1章1.3节TSPi的设计
查看>>
“最佳人气奖”出炉!4月27号,谁能拿到阿里聚安全算法挑战赛的桂冠?
查看>>
《网页美工设计Photoshop+Flash+Dreamweaver从入门到精通》——2.6 图层与图层样式...
查看>>
《iOS组件与框架——iOS SDK高级特性剖析》——第2章,第2.7节获取线路
查看>>
Spring中 @Autowired标签与 @Resource标签 的区别
查看>>
人工智能凭什么毁灭人类
查看>>
[LeetCode]--349. Intersection of Two Arrays
查看>>
tomcat启动报错
查看>>
mongorocks引擎原理解析
查看>>
用Swift实现一款天气预报APP(一)
查看>>
oracle11g R2 RAC卸载grid
查看>>
ES6 结构和扩展运算符
查看>>
王利阳:电商大促 决战6.18
查看>>
kafka消息传输的事务定义
查看>>
实现LNMMP
查看>>
mysql的pid文件出现问题
查看>>
计算rem单位
查看>>
第七章 大网高级 ASA
查看>>
rsync+inotify触发式远程同步
查看>>