8.Mapper传递多个参数及获取的方式有哪些?【掌握】
考核点:Mybatis参数映射。
答:
第1种:使用Mybatis默认参数别名之“paramn”
//Mapper接口中代码:
Public UserselectUser(String name,String area);
//Mapper.xml映射文件中代码:
<select id="selectUser"resultMap="BaseResultMap">
select * from user_user_t where user_name = #{param1} anduser_area=#{param2}
</select>
第2种:使用Mybatis默认参数别名之“argn”
//Mapper接口中代码:
Public UserselectUser(String name,String area);
//Mapper.xml映射文件中代码:
<select id="selectUser"resultMap="BaseResultMap">
select * from user_user_t where user_name = #{arg0} anduser_area=#{arg1}
</select>
第3种:使用@param注解设置别名
//Mapper接口中代码:
public user selectuser(@param(“username”) String username, @param(“hashedpassword”)
String hashedpassword);
//Mapper.xml映射文件中代码:
<select id=”selectuser” resulttype=”user”>
select id, username, hashedpassword from some_table
where username = #{username} and hashedpassword = #{hashedpassword}
</select>
9.Mybatis动态SQL是做什么的?都有哪些动态SQL?简述一下动态SQL的执行原理?【掌握】
考核点:动态SQL。
答:
Mybatis动态SQL:可以让我们在Xml映射文件内,以标签的形式编写动态SQL,完成逻辑判断和动态拼接SQL的功能;
Mybatis提供了10种动态SQL标签:trim|where|set|foreach|if|choose|when|otherwise|include|sql;
执行原理:使用OGNL计算表达式的值,根据表达式的值动态拼接SQL,以此来完成动态SQL的功能。
10.Mybatis映射文件中不同的Xml文件,标签ID是否可以重复?【重点】
考核点:Mybatis工作原理。
答:
不同的Xml映射文件,如果配置了命名空间(namespace),那么标签的ID可以重复;如果没有配置命名空间,那么标签的ID不能重复,毕竟命名空间不是必须的,只是最佳实践而已;
原因:命名空间+标签的ID是作为Mybatis维护MapperStatemented对象集合的key使用的,如果没有命名空间,就剩下标签的ID,导致无法区分具体的MapperStatemented对象;
11.Mybatis中如何实现一对一、一对多的关联查询?【掌握】
考核点:Mybatis关联映射。
答:
一对一的例子:
<select id="getClass" parameterType="int" resultMap="ClassesResultMap">
select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}
</select>
<resultMap type="com.lcb.user.Classes" id="ClassesResultMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column=” teacher_id” javaType="com.lcb.user.Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
一对多的例子:
<select id="getClass2" parameterType="int" resultMap="ClassesResultMap2">
select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}
</select>
<resultMap type="com.lcb.user.Classes" id="ClassesResultMap2">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<collection property="students" column=” c_id” ofType="com.lcb.user.Student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
</collection>
</resultMap>
12.Mybatis是什么?与IBatis有什么区别?【了解】
考核点:Mybatis的发展历史。
答:
维护组织:Ibatis本是apache的一个开源项目,2010年这个项目由apache software foundation 迁移到了google code,并且改名为mybatis;
接口式绑定:Mybatis实现了接口绑定,使用更加方便,而iBatis不支持;
关系映射的实现方式:IBatis实现关系映射使用的是子查询,而Mybatis不但支持子查询还增加了分步查询,效率更高;
OGNL表达式的支持:MyBatis采用功能强大的基于OGNL的表达式来消除其他元素,IBatis不支持;
SQL映射的语法区别:动态SQL、传入参数、接收参数、存储过程调用等语法不同。
13.Mybatis运行原理?【重点】
考核点:Mybatis工作原理。
答:
加载Mybatis全局配置文件(数据源、Mapper映射文件等),解析配置文件,Mybatis基于XML配置文件生成Configuration,和一个个MappedStatement(包括了参数映射配置、动态SQL语句、结果映射配置),其对应着<select | update | delete | insert>标签项;
SqlSessionFactoryBuilder通过Configuration对象生成SqlSessionFactory,用来开启SqlSession;
SqlSession对象完成和数据库的交互;
用户程序调用Mybatis接口层API(即Mapper接口中的方法);
SqlSession通过调用API的Statement ID找到对应的MappedStatement对象;
通过Executor(负责动态SQL的生成和查询缓存的维护)将MappedStatement对象进行解析,SQL参数转化、动态SQL拼接,生成JDBC Statement对象;
JDBC执行SQL;
借助MappedStatement中的结果映射关系,将返回结果转化成HashMap、JavaBean等存储结构并返回。
14.Mybatis中在哪使用了命名空间?【了解】
考核点:Mybatis映射文件。
答:
Mybatis的Mapper映射文件中的Mapper配置节的namespace属性是命名空间。
作用:
使用类路径的完全限定名称的方式区分Mapper映射文件,防止SQL标签的ID重名;
namespace命名空间是Mybatis中mapperStatementId的重要组成部分。