通过日志信息 详细的阅读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>