1.如果mapper接口,mapper实现是由springFactoryBean的方式来自动实现的。那么在各个配置文件的namespace需要明确写明mapper接口的全路径。
<mapper namespace="com.cnofe.base.dao.BaseUserMapper">
2.如果没有使用spring提供的自动扫描MapperScannerConfigurer,那么需要在mybatis-config.xml中配置各个映射文件的路径
<mappers>
<mapper resource="com/cnofe/base/map/BaseUserMapper.xml"/>
<mapper resource="com/cnofe/base/map/BaseMenuMapper.xml"/>
</mappers>
3.要使用mybatis 延迟加载需要在mybatis-config.xml的settings中设置
<settings>
<!-- changes from the defaults for testing -->
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
<!-- 延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
4.要使用对象的一对一关联,需要在resultMap中使用<association>
<resultMap id="BaseResultMap"
type="com.cnofe.base.entity.BaseUser">
<id property="userId" column="user_id" jdbcType="VARCHAR" />
<result property="userTitle" column="user_title"
jdbcType="VARCHAR" />
<result property="userLevel" column="user_level"
jdbcType="VARCHAR" />
<result property="education" column="education"
jdbcType="VARCHAR" />
<result property="qq" column="qq" jdbcType="VARCHAR" />
<result property="email" column="email" jdbcType="VARCHAR" />
<result property="photo" column="photo" jdbcType="VARCHAR" />
<result property="userStatus" column="user_status"
jdbcType="CHAR" />
<result property="userDesc" column="user_desc"
jdbcType="VARCHAR" />
<result property="signature" column="signature"
jdbcType="VARCHAR" />
<association property="departmentOBJ" column="dept_id" select="com.cnofe.base.dao.BaseDeptMapper.selectByPrimaryKey" />
</resultMap>
注意: select中的语句含义:从com.cnofe.base.dao.BaseDeptMapper.xml中查找id为selectByPrimaryKey的语句。
曾在这上面按照官方文档 返回的类型定为resultType 结果,mybatis不会自动封装其中,要resultMap才可以。
6.别名只能在mybatis-config.xml中定义 是全局的 其他配置文件都可以读取到。
7.mybatis 在配置文件中 参数传递 使用#{参数名称},如果参数是用于order by等 sql语句的组成部分,那么就应该使用${参数名称}