<resultMap>:配置字段和对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
<id>:配置主键映射关系标签。
<result>:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
<association>:配置被包含对象的映射关系标签。
property 属性:被包含对象的变量名
javaType 属性:被包含对象的数据类型
SQL准备
CREATETABLE person(
id INTPRIMARYKEYAUTO_INCREMENT,
NAME VARCHAR(20),
age INT);INSERTINTO person VALUES(NULL,'张三',23);INSERTINTO person VALUES(NULL,'李四',24);INSERTINTO person VALUES(NULL,'王五',25);CREATETABLE card(
id INTPRIMARYKEYAUTO_INCREMENT,
number VARCHAR(30),
pid INT,CONSTRAINT cp_fk FOREIGNKEY(pid)REFERENCES person(id));INSERTINTO card VALUES(NULL,'12345',1);INSERTINTO card VALUES(NULL,'23456',2);INSERTINTO card VALUES(NULL,'34567',3);
配置文件准备
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.itheima.table01.OneToOneMapper"><!--配置字段和实体对象属性的映射关系--><resultMapid="oneToOne"type="card"><idcolumn="cid"property="id"/><resultcolumn="number"property="number"/><!--
association:配置被包含对象的映射关系
property:被包含对象的变量名
javaType:被包含对象的数据类型
--><associationproperty="p"javaType="person"><idcolumn="pid"property="id"/><resultcolumn="name"property="name"/><resultcolumn="age"property="age"/></association></resultMap><selectid="selectAll"resultMap="oneToOne">
SELECT c.id cid,number,pid,NAME,age FROM card c,person p WHERE c.pid=p.id
</select></mapper>
测试类
@TestpublicvoidselectAll()throwsException{//1.加载核心配置文件InputStream is =Resources.getResourceAsStream("MyBatisConfig.xml");//2.获取SqlSession工厂对象SqlSessionFactory sqlSessionFactory =newSqlSessionFactoryBuilder().build(is);//3.通过工厂对象获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession(true);//4.获取OneToOneMapper接口的实现类对象OneToOneMapper mapper = sqlSession.getMapper(OneToOneMapper.class);//5.调用实现类的方法,接收结果List<Card> list = mapper.selectAll();//6.处理结果for(Card c : list){System.out.println(c);}//7.释放资源
sqlSession.close();
is.close();}
多表模型一对多操作
<resultMap>:配置字段和对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
<id>:配置主键映射关系标签。
<result>:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
<collection>:配置被包含集合对象的映射关系标签。
property 属性:被包含集合对象的变量名
ofType 属性:集合中保存的对象数据类型
一对多模型: 一对多模型:班级和学生,一个班级可以有多个学生。
CREATETABLE classes(
id INTPRIMARYKEYAUTO_INCREMENT,
NAME VARCHAR(20));INSERTINTO classes VALUES(NULL,'一班');INSERTINTO classes VALUES(NULL,'二班');CREATETABLE student(
id INTPRIMARYKEYAUTO_INCREMENT,
NAME VARCHAR(30),
age INT,
cid INT,CONSTRAINT cs_fk FOREIGNKEY(cid)REFERENCES classes(id));INSERTINTO student VALUES(NULL,'张三',23,1);INSERTINTO student VALUES(NULL,'李四',24,1);INSERTINTO student VALUES(NULL,'王五',25,2);INSERTINTO student VALUES(NULL,'赵六',26,2);
<mappernamespace="com.itheima.table02.OneToManyMapper"><resultMapid="oneToMany"type="classes"><idcolumn="cid"property="id"/><resultcolumn="cname"property="name"/><!--
collection:配置被包含的集合对象映射关系
property:被包含对象的变量名
ofType:被包含对象的实际数据类型
--><collectionproperty="students"ofType="student"><idcolumn="sid"property="id"/><resultcolumn="sname"property="name"/><resultcolumn="sage"property="age"/></collection></resultMap><selectid="selectAll"resultMap="oneToMany">
SELECT c.id cid,c.name cname,s.id sid,s.name sname,s.age sage FROM classes c,student s WHERE c.id=s.cid
</select></mapper>