Mybatis学习系列六 一对一、一对多延迟加载

通过日志信息 详细的阅读mybatis执行情况 (观察mybatis实际执行的sql语句 以及sql中的参数和返回结果)

日志:Log4j

1 .Log4j.jar包
2 .开启日志 在conf.xml

<settings>        
    <!-- 开启日志, 并指定使用的具体日志 -->        
    <setting name="logImpl" value="LOG4J"/>    
</settings>

3 . 编写配置日志输出文件
在src中建立file  log4j.properties

log4j.rootLogger = debug ,  stdout
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =%5p [%t] - %m%n

一对一延迟加载

例子:一个学生对应一个学生证

在StudentMapper里配置

<!-- 一对一延迟加载 -->
	<select id="OneLoad" resultMap="Load" parameterType="int" >
		select * from student2
	</select>
	<resultMap type="org.student.entity.Student2" id="Load">
		<id  property="stuNo"  column="stuNo"/>
		<result property="stuName"  column="stuName"/>
		<result property="stuAge"  column="stuAge"/>
		
		<association property="card" javaType="org.student.entity.StudentCard" select="org.student.mapper.StudentCardMapper.cardid" column="cardid">
		</association>
	</resultMap>

在StudentCard里配置

<select id="cardid"  parameterType="int" resultType="org.student.entity.StudentCard">
		select * from studentcard where cardid = #{cardid}
</select>

一对多延迟加载 

例子:一个班级对应很多学生

StudentClassMapper.xml

<!-- 一对多延迟加载 -->
	<select id="queryClass" parameterType="int" resultMap="class_student_Load">
		select * from  studentclass
	</select>
	<resultMap type="org.student.entity.StudentClass" id="class_student_Load">
		<id property="classid" column="classid"/>
		<result property="className" column="className"/>
		
		<collection property="students" ofType="org.student.entity.Student2" select="org.student.mapper.StudentMapper.classStudent"  column="classid">
		</collection>
	</resultMap>

StudentMapper.xml

<!-- 按照班级查询学生 -->
	<select id="classStudent" parameterType="int" resultType="org.student.entity.Student2">
		select * from student2 where classid = #{classid}
	</select>

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值