做汽车行业必须注册际零件网站,提供网站建设工具的品牌有哪些,淘客网站开发流程,外贸人常用的app多表模型#xff1a; 多表模型分类 一对一#xff1a;在任意一方建立外键#xff0c;关联对方的主键。一对多#xff1a;在多的一方建立外键#xff0c;关联一的一方的主键。多对多#xff1a;借助中间表#xff0c;中间表至少两个字段#xff0c;分别关联两张表的主键…多表模型 多表模型分类 一对一在任意一方建立外键关联对方的主键。一对多在多的一方建立外键关联一的一方的主键。多对多借助中间表中间表至少两个字段分别关联两张表的主键。 多表模型一对一操作
sql语句准备
CREATE TABLE person(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20),age INT
);
INSERT INTO person VALUES (NULL,张三,23);
INSERT INTO person VALUES (NULL,李四,24);
INSERT INTO person VALUES (NULL,王五,25);CREATE TABLE card(id INT PRIMARY KEY AUTO_INCREMENT,number VARCHAR(30),pid INT,CONSTRAINT cp_fk FOREIGN KEY (pid) REFERENCES person(id)
);
INSERT INTO card VALUES (NULL,12345,1);
INSERT INTO card VALUES (NULL,23456,2);
INSERT INTO card VALUES (NULL,34567,3);配置文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtdmapper namespacecom.duobiao.table.One
!--
resultMap配置字段和实体对象属性的映射关系标签。
id 属性唯一标识
type 属性实体对象类型id配置主键映射关系标签。
result配置非主键映射关系标签。column 属性表中字段名称property 属性 实体对象变量名称
association配置被包含对象的映射关系标签。property 属性被包含对象的变量名javaType 属性被包含对象的数据类型
--!--配置字段和实体对象属性的映射关系--resultMap idoneToOne typecardid columncid propertyid /result columnnumber propertynumber /association propertyp javaTypepersonid columnpid propertyid /result columnname propertyname /result columnage propertyage //association/resultMapselect idselectAll resultMaponeToOneSELECT c.id cid,number,pid,NAME,age FROM card c,person p WHERE c.pidp.id/select
/mapper核心配置文件 !--起别名--typeAliasespackage namecom.duobiao.bean//typeAliases !-- mappers引入映射配置文件 --mappers!-- mapper 引入指定的映射配置文件 resource属性指定映射配置文件的名称 --mapper resourceOneToOneMapper.xml//mappersbean包
public class Person {private Integer id;private String name;private Integer age;
}public class Card {// 主键idprivate Integer id;// 身份证号private String number;// 所属人对象private Person p;
}接口
public interface OneToOneMapper {// 查询全部ListCard selectAll();
}测试类 Testpublic void selectAll() throws Exception{//1.加载核心配置文件InputStream is Resources.getResourceAsStream(MyBatisConfig.xml);//2.获取SqlSession工厂对象SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(is);//3.通过工厂对象获取SqlSession对象SqlSession sqlSession sqlSessionFactory.openSession(true);//4.获取OneToOneMapper接口的实现类对象OneToOneMapper mapper sqlSession.getMapper(OneToOneMapper.class);//5.调用实现类的方法接收结果ListCard list mapper.selectAll();//6.处理结果for (Card c : list) {System.out.println(c);}//7.释放资源sqlSession.close();is.close();}一对多
sql数据
CREATE TABLE classes(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20)
);
INSERT INTO classes VALUES (NULL,一班);
INSERT INTO classes VALUES (NULL,二班);CREATE TABLE student(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(30),age INT,cid INT,CONSTRAINT cs_fk FOREIGN KEY (cid) REFERENCES classes(id)
);
INSERT INTO student VALUES (NULL,张三,23,1);
INSERT INTO student VALUES (NULL,李四,24,1);
INSERT INTO student VALUES (NULL,王五,25,2);
INSERT INTO student VALUES (NULL,赵六,26,2);bean
public class Student {private Integer id;private String name;private Integer age;
}public class Classes {// 主键idprivate Integer id;// 班级名private String name;// 班级中所有的学生private ListStudent students;
}配置文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtdmapper namespacecom.table.OneToManyMapperresultMap idoneToMany typeclassesid columncid propertyid/result columncname propertyname/!--collection配置被包含的集合对象映射关系property被包含对象的变量名ofType被包含对象的实际数据类型--collection propertystudents ofTypestudentid columnsid propertyid/result columnsname propertyname/result columnsage propertyage//collection/resultMapselect idselectAll resultMaponeToManySELECT c.id cid,c.name cname,s.id sid,s.name sname,s.age sage FROM classes c,student s WHERE c.ids.cid/select
/mapper测试 Testpublic void selectAll() throws Exception{//1.加载核心配置文件InputStream is Resources.getResourceAsStream(MyBatisConfig.xml);//2.获取SqlSession工厂对象SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(is);//3.通过工厂对象获取SqlSession对象SqlSession sqlSession sqlSessionFactory.openSession(true);//4.获取OneToManyMapper接口的实现类对象OneToManyMapper mapper sqlSession.getMapper(OneToManyMapper.class);//5.调用实现类的方法接收结果ListClasses classes mapper.selectAll();//6.处理结果for (Classes cls : classes) {System.out.println(cls.getId() , cls.getName());ListStudent students cls.getStudents();for (Student student : students) {System.out.println(\t student);}}//7.释放资源sqlSession.close();is.close();}多对多
sql语句
CREATE TABLE course(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20)
);
INSERT INTO course VALUES (NULL,语文);
INSERT INTO course VALUES (NULL,数学);CREATE TABLE stu_cr(id INT PRIMARY KEY AUTO_INCREMENT,sid INT,cid INT,CONSTRAINT sc_fk1 FOREIGN KEY (sid) REFERENCES student(id),CONSTRAINT sc_fk2 FOREIGN KEY (cid) REFERENCES course(id)
);
INSERT INTO stu_cr VALUES (NULL,1,1);
INSERT INTO stu_cr VALUES (NULL,1,2);
INSERT INTO stu_cr VALUES (NULL,2,1);
INSERT INTO stu_cr VALUES (NULL,2,2);配置文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtdmapper namespacecom.tableManyToManyMapperresultMap idmanyToMany typestudentid columnsid propertyid/result columnsname propertyname/result columnsage propertyage/collection propertycourses ofTypecourseid columncid propertyid/result columncname propertyname//collection/resultMapselect idselectAll resultMapmanyToManySELECT sc.sid,s.name sname,s.age sage,sc.cid,c.name cname FROM student s,course c,stu_cr sc WHERE sc.sids.id AND sc.cidc.id/select
/mapper测试 Testpublic void selectAll() throws Exception{//1.加载核心配置文件InputStream is Resources.getResourceAsStream(MyBatisConfig.xml);//2.获取SqlSession工厂对象SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(is);//3.通过工厂对象获取SqlSession对象SqlSession sqlSession sqlSessionFactory.openSession(true);//4.获取ManyToManyMapper接口的实现类对象ManyToManyMapper mapper sqlSession.getMapper(ManyToManyMapper.class);//5.调用实现类的方法接收结果ListStudent students mapper.selectAll();//6.处理结果for (Student student : students) {System.out.println(student.getId() , student.getName() , student.getAge());ListCourse courses student.getCourses();for (Course cours : courses) {System.out.println(\t cours);}}//7.释放资源sqlSession.close();is.close();}标签解释
resultMap配置字段和对象属性的映射关系标签。id 属性唯一标识type 属性实体对象类型id配置主键映射关系标签。result配置非主键映射关系标签。column 属性表中字段名称property 属性 实体对象变量名称association配置被包含对象的映射关系标签。property 属性被包含对象的变量名javaType 属性被包含对象的数据类型collection配置被包含集合对象的映射关系标签。property 属性被包含集合对象的变量名ofType 属性集合中保存的对象数据类型