mybatis的基本使用

来源:互联网 发布:江南大学网络接入系统 编辑:程序博客网 时间:2024/06/05 07:14

1.mybatis支持声明式数据缓存,当一条SQL语句被标记为可缓存之后,先从数据库中查找,所有的数据都存到高速缓存中,默认情况下是基于Java的hashMap实现

2.在同一个xml中ID唯一,namespace这个名称空间要配置对应的Mapper,

  <settings>
       <setting name="mapUnderscoreToCamelCase" value = "true"></setting>

   </settings>


3.对于resultMap用法,

<resultMap type="com.ld.model.operations.vo.FaultShowDetailVo"    类名
        id="showDetail"   唯一标识

autoMapping="true"  自动匹配没写出来的字段(名字一样且最少有一个这个类中的属性)

extends="" >  继承父resultMap的Id
        <id column="faultId" property="faultId"  javaType="string"/>   id唯一标识对象
        <collection property="faultImgs" javaType="java.util.ArrayList"   集合映射
            ofType="com.ld.model.operations.vo.FaultImgVo">
            <result column="fileName" property="fileName" javaType="String" /> 其他字段
            <result column="imgType" property="imgType" javaType="String" />
        </collection>
      
        <association property=""  关联对象

fetchType="lazy"   懒加载 

columnPrefix=""  列名前缀 

foreignColumn="" 外键 

resultMap="" 对应map

column="" autoMapping="true"  javaType=""></association>
        <constructor>  配置使用构造方法注入结果
        <idArg column=""  name="" /> 唯一标识和与resultMap中的Id,一样
        <arg column=""  name="" />与resultMap中的result,一样
        </constructor>
    </resultMap>


4.insert的使用

 <insert id="" flushCache="true"    设置为true清空一级二级缓存

timeout=""设置在抛出异常之前,驱动程序等待数据返回请求的秒数

statementType="STATEMENT" 对应的STATEMENT,PREPARED,CALLABLE,mybatis分别对应statement,preparement,CallableStatement  ,默认为PREPARED

useGeneratedKeys="true" 默认为false,如果设置为true,mybatis会使用JDBC的getGenerateKeys方法取出数据库内部生产到的主键

keyProperty=""

keyColumn="" >
  <selectKey keyColumn="" keyProperty="" order="AFTER">
   SELECT LAST_INSERT_ID()  在MySQL中的主键生产策略

   SELECT SEQ_ID.nextval from dual   在Oracle中的主键生产策略 Order=“BEFORE”
   </selectKey>
   </insert>