mybatis学习二

来源:互联网 发布:江南大学网络教育好吗? 编辑:程序博客网 时间:2024/06/06 08:52

1、resutlType和restultMap的区别

1、resultType:需要指定的pojo属性名和sql语句中的列名一致,才可以映射成功
2、如果sql语句和pojo属性名不一致,可以通过resultMap将字段名和属性名作为一个对应关系。resultMap最终还是将查询结果映射到pojo对象中
3、resutlMap可以实现将查询结果映射成复杂的pojo,完成一些高级映射。

  • 将关联查询的列映射成pojo属性中(一对一)
  • 将关联查询的列映射成一个List(一对多)

4、resutMap可以实现延迟加载,resultType无法实现

2、延迟加载

延迟加载: 延迟加载也称懒加载:当真需要数据的时候,才执行数据的加载操作。比如数据库查询,真正需要的时候才去执行sql语句。避免一些无所谓的性能开销。


这里写图片描述
两个设置条件

<settings>        <!--打开延迟加载 -->        <setting name="lazyLoadingEnabled" value="true" />        <!--按需加载 -->        <setting name="aggressiveLazyLoading" value="false" />    </settings>

使用延迟加载方法,先去查询简单的sql(最好单表,也可以关联查询),再去按需要加载关联查询的其它信息。

3、mybatis一级缓存

mybatis提供查询缓存,用于减轻数据压力,提高数据库性能。

这里写图片描述
一级缓存是SqlSession缓存,这个缓存在底层用map结构存储

 @SuppressWarnings("unchecked")      public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {        ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());        if (closed) throw new ExecutorException("Executor was closed.");        if (queryStack == 0 && ms.isFlushCacheRequired()) {          clearLocalCache();        }        List<E> list;        try {          queryStack++;          //locatCache就是一级缓存存储区域,这是对map的封装          list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;          if (list != null) {              //如果list!=null说明有缓存,直接执行缓存中的语句            handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);          } else {              //如果list==null,说明缓存为空,执行真正的查询语句            list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);          }        } finally {          queryStack--;        }        if (queryStack == 0) {          for (DeferredLoad deferredLoad : deferredLoads) {            deferredLoad.load();          }          deferredLoads.clear(); // issue #601          if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {            clearLocalCache(); // issue #482          }        }        return list;      }

:当用户执行第一次查询时,如果缓存中没有,则从数据库中查询。查询后将结果保存在一级缓存中

:当用户第二次查询是,如果缓存不这为空,则直接用缓存中数据,不再进行sql查询操作

清空:当执行commit(插入,更新,删除)时清空sqlsession,为了保证数据一致,防止脏读。

3、mybatis二级缓存

二级缓存是mpaaer层的缓存,多个sqlsession操作同一个sql语句
这里写图片描述
二级缓存和一级缓存相对,二级缓存的范围更大,多个sqlSession可以共享一个Mapper的二级缓存区域。如果两个mapper的namespace相同,这两个mapper执行sql查询到的数据将存在相同的二级缓存中。
:当sqlsession1执行时,如果缓存中没有,则从数据库中查询。查询后将结果保存在二级缓存中

:当第二次查询时sqlsession2,如果缓存不这为空,则直接用缓存中数据,不再进行sql查询操作

清空:当执行mapper中sql为commit时,清空mapper下的二级缓存区。

4、逆向工程

mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml、po..)

常用以xml文件配置的方式生成代码

5、Mybatis打印sql语句

在mybatis和spirngmvc使用时打印log
这里写图片描述

1、在mybatis中配置如下进行扫描:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <!--mybatis sessionFactory配置 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="configLocation" value="classpath:config/log.xml"></property>        <property name="mapperLocations" value="classpath:cn/itcast/core/dao/*.xml" />        <property name="typeAliasesPackage" value="cn.itcast.core.bean" />    </bean>    <!-- 扫包 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="cn.itcast.core.dao" />    </bean></beans>

2、log中配置如下:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration       PUBLIC "-//mybatis.org//DTD Config 3.0//EN"      "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <settings>        <!-- 打印查询语句 -->        <setting name="logImpl" value="STDOUT_LOGGING" />    </settings></configuration>
1 0
原创粉丝点击