一.部分JQuery的使用回顾
$(function(){
//i表示循环脚标,n表示迭代变量,n=数组[i],n是dom对象
//dom对象转换成jquery对象?$(dom对象)
//把jquery对象转换成dom对象?jquery对象[0],jquery对象.get(0)
$.each($(":radio"),function(i,n){
if($(n).val()==pageSize){
$(n).attr("checked","checked");
}
});
//对输入框设置值
$(":text[name='sname']").val(sname);
$(":text[name='tname']").val(tname);
});
二.Mybatis实现多表查询
Mybatis实现多表查询方式:①业务装配,对两个表编写单表查询语句,在业务(Service)把查询的两个结果进行关联。②使用AutoMapping特性,在实现两表联合查询时通过别名完成映射。③使用Mybatis的<resultMap>标签进行实现。
多表查询时,类中包含另一个类的对象的分类:单个对象,集合对象。
<resultMap>标签写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系,默认Mybatis使用Auto Mapping特性。
使用<resultMap>标签时,<select>标签不写resultType而是使用resultMap属性引用<resultMap>标签.
使用resultMap实现单表映射关系
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.TeacherMapper">
<resultMap type="teacher" id="mymap">
<!-- 主键使用id标签配置映射关系 -->
<id column="id" property="id1" />
<!-- 其他列使用result标签配置映射关系 -->
<result column="name" property="name1"/>
</resultMap>
<select id="selAll" resultMap="mymap">
select * from teacher
</select>
</mapper>
使用resultMap实现关联单个对象(N+1)方式,当属性第一个单词只有一个字母时,那么第二个单词首字母也小写。N+1查询方式,先查询出某个表的全部信息,根据这个表的信息查询另一个表的信息。与业务装配的区别,在service里面写的代码,由mybatis完成装配。
实现步骤:在Student实现类中包含了一个Teacher对象,在TeacherMapper中提供一个查询,在StudentMapper中关联对象,<association>装配一个对象时使用,property时对象在类中的属性名,select是通过哪个查询查出这个对象的信息,column是把当前表的哪个列的值做为参数传递给另一个查询,大前提使用N+1方式时,如果列名和属性名相同可以不配置,使用Auto Mapping特性,但是mybatis默认只会给列装配一次。
public class Student {
private int id;
private String name;
private int age;
private int tid;
private Teacher teacher;
}
<select id="selById" resultType="teacher" parameterType="int">
select * from teacher where id=#{0}
</select>
<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>
多表resultMap加载单个对象联合查询方式
<mapper namespace="com.bjsxt.mapper.StudentMapper">
<resultMap type="Student" id="stuMap">
<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="selAll" resultMap="stuMap">
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>
</mapper>
N+1方式和联合查询方式对比:N+1,需求不确定时;联合查询,需求中确定查询时两个表一定都查询。
N+1名称的由来:学生中有三条数据,需要查询所有学生信息级授课老师信息,执行一次查询学生全部信息select * from 学生,执行三次select * from 老师 where id = 学生的外键。使用多条SQL命令查询书两表数据时,如果希望把需要的数据都查询出来,需要执行N+1条SQL才能把所有数据库查询出来。缺点是效率低,优点是如果有时候不需要查询学生时同时查询老师,有的时候只需要查询学生。如果执行N+1查询带来的效率低的问题,默认带的前提,每次都是两个都查询,使用两表联合查询。
使用<resultMap>查询关联集合对象(N+1):①在Teacher中添加List<Student>②在StudentMapper.xml中添加通过tid查询③在TeacherMapper.xml中添加查询全部,<collection/>当属性是集合类型时使用的标签。
public class Teacher {
private int id;
private String name;
private List<Student> list;
}
<mapper namespace="com.bjsxt.mapper.StudentMapper">
<select id="selByTid" parameterType="int" resultType="student">
select * from student where tid=#{0}
</select>
</mapper>
<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>
多表resultmap加载集合对象联合查询方式
<mapper namespace="com.bjsxt.mapper.TeacherMapper">
<resultMap type="teacher" id="mymap">
<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="mymap">
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>
</mapper>
使用Auto Mapping结合别名实现多表查询:只能使用多表联合查询方式,只适合查询对象的,要求查询出的列别和属性名相同,在SQL是关键字符,两侧添加反单引号。
<mapper namespace="com.bjsxt.mapper.StudentMapper">
<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>
</mapper>
三.MyBatis注解
注解是为了简化配置文件。MyBatis的注解简化mapper.xml文件,如果涉及动态SQL依然使用mapper.xml。mapper.xml和注解可以共存。使用注解时mybatis.xml中<mappers>使用<package/><mapper class=""/>
使用MyBatis注解写增删查改
使用MyBatis注解写多表查询
四.运行原理
运行过程中涉及到的类:①Resources,是MyBatis中IO流的工具类,负责加载配置文件。②SqlSessionFactoryBuilder(),构建器,负责创建SqlSessionFactory接口的实现类。③XMLConfigBuilder,MyBatis全局配置文件内容构建器类,负责读取流内容并转换为JAVA代码。④Configuration,封装了全局配置文件所有配置信息,全局配置文件内容存放在Configuration中。⑤DefaultSqlSessionFactory,是SqlSessionFactory接口的实现类。⑥Transaction,事务类,每一个SqlSession会带有一个Transaction对象。⑦TransactionFactory,事务工厂,负责生产Transaction。⑧Executor,MyBatis执行器,负责来执行SQL命令,相当于JDBC中statement对象(或PreparedStatement或CallableStatement),默认的执行器SimpleExcutor,批量操作BatchExcutor,通过openSession(参数控制)。⑨DefaultSqlSession是SqlSession接口的实现类。⑩ExceptionFactory,MyBatis中的异常工厂。
文字解释:在MyBatis运行开始时需要先通过Resources加载全局配置文件。下面需要实例化SqlSessionFactoryBuilder构建器。帮助SqlSessionFactory接口实现类DefaultSqlSessionFactory。在实例化DefaultSqlSessionFactory之前需要先创建XmlConfigBuilder解析全局配置文件流,并把解析结果存放在Configuration中,之后把Configuration传递给DefaultSqlSessionFactory。到此SqlSessionFactory工厂创建成功。由SqlSessionFactory工厂创建SqlSession。每次创建SqlSession时,都需要由TransactionFactory创建Transaction对象,同时还需要创建SqlSession的执行器Executor,最后实例化DefaultSqlSession,传递给SqlSession接口。根据项目需求使用SqlSession接口中的API完成具体的事务操作。如果事务执行失败,需要进行rollback回滚事务。如果事务执行成功提交给数据库,关闭SqlSession,到此就是MyBatis的运行原理(面试官说的)