一、半自动与全自动的区别
全自动是在查询相关对象或相关集合对象时,可以直接基于对象关系模型进行检索。
半自动是在查询相关对象或相关集合对象时,必须手动创建和完成sql再进行检索。
二、了解orm框架
ORM(Object Relational Mapping)框架采用元数据来描述对象与关系映射的细节,元数据一般采用XML格式,并且存放在专门的对象一映射文件中。
也就是说,采用映射元数据来描述对象关系的映射,使得ORM中间件能在任何一个应用的业务逻辑层和数据库层之间充当桥梁。
当前ORM框架主要有五种:Hibernate(Nhibernate),iBatis,mybatis,EclipseLink,JFinal。
三、动态sql一对一、一对多、多对多的理解
动态sql就是sql的内容是变化的,可以根据条件获取到不同的sql语句。
用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户。
一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户。
一对一配置:使用<resultMap>
做配置
<resultMap id="orderMap" type="domain.Order">
<result property="id" column="id"></result>
<result property="ordertime" column="ordertime"></result>
<result property="total" column="total"></result>
<association property="user" javaType="domain.User">
<result column="uid" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
</association>
</resultMap>
一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单。
一对多配置:使用<resultMap>
+<collection>
做配置
<resultMap id="userMap" type="domain.User">
<result column="id" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<collection property="orderList" ofType="domain.Order">
<result column="oid" property="id"></result>
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
</collection>
</resultMap>
用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用。
多对多查询的需求:查询用户同时查询出该用户的所有角色。
多对多配置:使用<resultMap>
+<collection>
做配置
<resultMap id="userRoleMap" type="domain.User">
<result column="id" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<collection property="roleList" ofType="domain.Role">
<result column="rid" property="id"></result>
<result column="rolename" property="rolename"></result>
</collection>
</resultMap>
四、一级缓存、二级缓存
一级缓存也叫本地缓存,在MyBatis中,一级缓存是在会话(SqlSession)层面实现的,这就说明一级缓存作用范围只能在同一个SqlSession中,跨SqlSession是无效的。MyBatis中一级缓存是默认开启的,不需要任何配置。
二级缓存是全局的,也就是说多个请求可以共用一个缓存,二级缓存需要手动开启,有2种方式配置二级缓存,缓存会先放在一级缓存中,当sqlSession会话提交或者关闭时才会将一级缓存刷新到二级缓存中;开启二级缓存后,用户查询时,会先去二级缓存中找,找不到再去一级缓存中找。
对于缓存数据更新机制,当某一个作用域(一级缓存Session/二级缓存Namespaces)的进行了 C/U/D 操作后,默认该作用域下所有 select 中的缓存将被clear。
五、Mybatis与Hibrnate的区别
最大的区别:一个是全自动ORM框架,一个是半自动ORM框架。
Hibernate是全自动ORM框架,是表和实体类的映射;
Mybatis是半自动ORM框架,是sql语句和实体类的映射。
Hibernate属于全自动ORM映射工具,使用Hibernate查询关联对象或者关联集合对象时,可以根据对象关系模型直接获取,所以它是全自动的。
而Mybatis在查询关联对象或关联集合对象时,需要手动编写sql来完成,所以,称之为半自动ORM映射工具。
六、分页查询插件
MyBatis可以使用第三方的插件来对功能进行扩展,分页助手PageHelper是将分页的复杂操作进行封装,使用简单的方式即可获得分页的相关数据。
开发步骤:
①导入通用PageHelper坐标
<!-- 分页助手 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
②在mybatis核心配置文件中配置PageHelper插件
<!-- 注意:分页助手的插件 配置在通用的mapper之前 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 指定方言 -->
<property name="dialect" value="mysql"/>
</plugin>
③测试分页代码实现
@Test
public void testPageHelper(){
//设置分页参数
PageHelper.startPage(1,2);
List<User> select = userMapper2.select(null);
for(User user : select){
System.out.println(user);
}
}
七、延时加载
Mybatis 仅支持 association 关联对象和 collection 关联集合对象的延迟加载, associatio 指的就是一对一, collection 指的就是一对多查询。在Mybatis配置文件中,可以配置是否启用延迟加载LazyLoadingEnabled=true|false。
八、在mybatis中#与$的区别
#
传入的参数在SQL中显示为字符串,比如:select id,name,age from student where id =#{id}
,当前端把id值1,传入到后台的时候,就相当于 select id,name,age from student where id ='1'
。
$
传入的参数在SqL中直接显示为传入的值,比如:select id,name,age from student where id =${id}
,当前端把id值1,传入到后台的时候,就相当于 select id,name,age from student where id = 1
。
#{}是经过预编译的,是安全的,能够很大程度防止sql注入(语句的拼接),${}无法防止Sql注入。
大部分时候都用#,但有一个特殊场景只能用$!那就是将数据库对象作为参数传递,其中最常见的就是 group by了。假设我们现在想要对某个表进行排序查询,但参与排序的字段并不确定,而是要通过前端参数传递过来,那么此时就必须使用 $ 了!因为 # 预处理之后,会把数据库中的字段名识别为字符串,进而会出错,如下所示:
select count(*) from user group by ${param}