赞
踩
内容
Auto Mapping 单表实现(别名方式)
实现单表配置
单个对象关联查询(N+1,外连接)
集合对象关联查询
注解开发
MyBatis 运行原理
<resultMap>
标签写在mapper.xml中,由程序员控制SQL查询结果与<resultMap>
标签时,<select>
标签不写 resultType 属性,而是使<resultMap>
标签.<!--当实体类中的属性与数据库中的字段写法不同时,需要使用resultMap将字段与属性进行映射 column="t_id"对应数据库中的字段 property="id"对应实体类的属性 --> <resultMap type="com.xmm.demo.pojo.Teacher" id="mymap"> <!-- 主键使用 id 标签配置映射关系 --> <id column="t_id" property="id" /> <!-- 其他列使用 result 标签配置映射关系 --> <result column="name" property="name" /> </resultMap> <select id="selById" resultMap="mymap" parameterType="int"> select * from teacher where t_id= #{param} </select> 错误示例如下: <!-- 由于实体类是id,数据库字段是t_id,使用下方查询方式时输出结果id全都等于0,应该使用resultMap将id与t_id映射起来 --> <select id="selById" resultType="com.xmm.demo.pojo.Teacher" parameterType="int"> select * from teacher where t_id= #{param} </select>
public class Student {
private int id;
private String name;
private int age;
private int tid;
private Teacher teacher;
4.3.2 在 TeacherMapper 中提供一个查询
<select id="selById" resultType="teacher"
parameterType="int">
select * from teacher where id=#{0}
</select>
4.3.3 在 StudentMapper 中
4.3.3.1 <association>
装配一个对象时使用
4.3.3.2 property: 对象在类中的属性名
4.3.3.3 select:通过哪个查询查询出这个对象的信息
4.3.3.4 column: 把当前表的哪个列的值做为参数传递给另一个查询
4.3.3.5 大前提使用 N+1 方式.时如果列名和属性名相同可以不配置,使用 Auto mapping 特性.但是 mybatis 默认只会给列专配一次
<resultMap type="com.xmm.demo.pojo.Student" id="stuMap">
<id column="s_id" property="id" />
<result column="t_id" property="tid" />
<result column="age" property="age" />
<!-- 如果关联一个对象 将上面的t_id的值作为参数传给com.xmm.demo.mapper.TeacherMapperDemo.selById -->
<association property="teacher"
select="com.xmm.demo.mapper.TeacherMapperDemo.selById" column="t_id"></association>
</resultMap>
<select id="selAll" resultMap="stuMap">
select * from student
</select>
<!-- 相当于:先select * from student 查出的tid的值传给TeacherMapperDemo.selById(有多少个tid的值执行多少次) select * from teacher where t_id= ? -->
4.3.3.6 把上面代码简化成
如<result column="age" property="age" />中column的值和property的值书写一样,可以不写
<resultMap type="student" id="stuMap">
<result column="tid" property="tid"/>
<!-- 如果关联一个对象 -->
<association property="teacher"
select="com.bjsxt.mapper.TeacherMapper.selById"
column="tid"></association>
</resultMap>
<select id="selAll" resultMap="stuMap">
select * from student
</select>
<association/>
只要专配一个对象就用这个标签<association/>小的<resultMap>
看待<association/>
专配完后返回一个什么类型<resultMap type="Student" id="stuMap1"> <id column="sid" property="id"/> <result column="sname" property="name"/> <result column="age" property="age"/> <result column="tid" property="tid"/> <association property="teacher" javaType="Teacher" > <id column="tid" property="id"/> <result column="tname" property="name"/> </association> </resultMap> <select id="selAll1" resultMap="stuMap1"> select s.id sid,s.name sname,age age,t.id tid,t.name tname FROM student s left outer join teacher t on s.tid=t.id </select>
select * from student;
<resultMap>
查询关联集合对象(N+1)List<Student>
public class Teacher {
private int id;
private String name;
private List<Student> list;
<select id="selByTid" parameterType="int"
resultType="student">
select * from student where tid=#{0}
</select>
<collection/>
当属性是集合类型时使用的标签.<resultMap type="teacher" id="mymap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="list"
select="com.bjsxt.mapper.StudentMapper.selByTid"
column="id"></collection>
</resultMap>
<select id="selAll" resultMap="mymap">
select * from teacher
</select>
<!-- 相当于查询每个老师,然后查询与该老师关联的所有学生
先select * from teacher 后执行根据t_id的个数执行n次select * from student where t_id=? -->
<resultMap>
实现加载集合数据(联合查询方式)<resultMap type="teacher" id="mymap1">
<id column="tid" property="id"/>
<result column="tname" property="name"/>
<collection property="list" ofType="student" >
<id column="sid" property="id"/>
<result column="sname" property="name"/>
<result column="age" property="age"/>
<result column="tid" property="tid"/>
</collection>
</resultMap>
<select id="selAll1" resultMap="mymap1">
select t.id tid,t.name tname,s.id sid,s.name
sname,age,tid from teacher t LEFT JOIN student s on
t.id=s.tid;
</select>
5.1 只能使用多表联合查询方式.
5.2 要求:查询出的列别和属性名相同.
5.3 实现方式
5.3.1 .在 SQL 是关键字符,两侧添加反单引号
<select id="selAll" resultType="student">
select t.id `teacher.id`,t.name
`teacher.name`,s.id id,s.name name,age,tid
from student s LEFT JOIN teacher t on t.id=s.tid
</select>
<package/>
<mapper class=””/>
@Insert("insert into teacher
values(default,#{name})")
int insTeacher(Teacher teacher);
@Update("update teacher set name=#{name} where
id=#{id}")
int updTeacher(Teacher teacher);
@Delete("delete from teacher where id=#{0}")
int delById(int id);
<resultMap>
功能@Select("select * from student where tid=#{0}")
List<Student> selByTid(int tid);
9.3 在 TeacherMapper 接口添加
9.3.1 @Results() 相当于<resultMap>
9.3.2 @Result() 相当于<id/>或<result/>
9.3.2.1 @Result(id=true) 相当与
9.3.3 @Many() 相当于<collection/>
9.3.4 @One() 相当于<association/>
@Results(value={
@Result(id=true,property="id",column="id"),
@Result(property="name",column="name"),
@Result(property="list",column="id",many=@Many(sele
ct="com.bjsxt.mapper.StudentMapper.selByTid"))
})
@Select("select * from teacher")
List<Teacher> selTeacher();
3.文字解释
在 MyBatis 运行开始时需要先通过 Resources 加载全局配置文件.下面需要实例化SqlSessionFactoryBuilder 构建器.帮助 SqlSessionFactory 接口实现类 DefaultSqlSessionFactory. 在实例化 DefaultSqlSessionFactory 之前需要先创建 XmlConfigBuilder解析全局配置文件流,并把解析结果放在 Configuration 中.之后把Configuratin 传递给 DefaultSqlSessionFactory.到此 SqlSessionFactory 工
厂创建成功. 由 SqlSessionFactory 工厂创建 SqlSession. 每次创建 SqlSession 时,都需要由TransactionFactory 创建 Transaction对象,同时还需要创建 SqlSession 的执行器 Excutor,最后实例化DefaultSqlSession,传递给 SqlSession 接口. 根据项目需求使用 SqlSession 接口中的 API 完成具体的事务操作. 如果事务执行失败,需要进行 rollback 回滚事务. 如果事务执行成功提交给数据库.关闭 SqlSession到此就是 MyBatis 的运行原理.(面试官说的.)
mybatis运行原理(这个比较清晰):
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。