总结一个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对象,