mybatis使用总结

总结一个mybatis-spring-boot-starter依赖包的使用;

(1)spring.datasource.{url, username, password, driver-class-name}

(2)使用mybatis.config-location属性指定Mybatis的配置文件 classpath:mybatis-config.xml
    或者配置mybatis.mapper-locations=/UserMapper.xml,/DinnerMapper.xml

(3)在mybatis-config中定义域模型和mapper路径地址
实例代码如下:

<!DOCTYPE configuration
       PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="sample.mybatis.domain"/>
    </typeAliases>
    <mappers>
        <mapper resource="sample/mybatis/mapper/CityMapper.xml"/>
        <mapper resource="sample/mybatis/mapper/HotelMapper.xml"/>
    </mappers>
</configuration>

(4)书写mapper文件中主要事项
    resultType是返回resultSet中的某个实例转换为特定的bean类型;
(5)resultMap的使用
    <resultMap id="mapperId" type="xxx.SomeType">
        <id column="id" property="id"/>
        <result column="xxcol" property="propertyName"/>
    </resultMap>
    <select id="selectXXX" resultMap="mapperId">
        select * from user
    </select>
(6)启用缓存
     <cache eviction="LRU" flushInterval="20000" size="2014" readOnly="true"/>
        eviction属性配置缓存策略; FIFO/BLOCKING/SOFT
    


mybatis解析将mapper接口与xml文件映射实现原理;
    new PlainMethodInvoker(new MapperMethod(mapper接口名,sql方法对象));
        MapperMethod.execute(sqlsession, args);
            execute():
                根据commandType,select-->exu


    MapperMethod.sqlCommand()
        resolveMappedStatement(mapperInterface, methodName, declaringClass, configuration)
    MapperMethod根据接口名+方法名解析出MappedStatement对象,
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值