mybatis 使用总结

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语句的组成部分,那么就应该使用${参数名称}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值