在MyBatis中,Mapper中的namespace用于绑定Dao接口的,即面向接口编程,它的功能和Dao接口的实现类Impl相当,但是他不用写接口实现类,通过namesapce(命名空间)的绑定直接通过id找到相应方法,执行相应的SQL语句。
Demo.java
Student stu = mapper.findById(1);
StudentDao.java
public interface StudentDao {
public Student findById(Integer sid);
}
StudentMapper.xml
<mapper namespace="com.sjm.manytomany.StudentDao">
<resultMap id="stuMap" type="student">
<id column="sid" property="sid"></id>
<result column="sname" property="sname"></result>
<collection property="courseList" javaType="list" ofType="course"
resultMap="com.sjm.manytomany.CourseDao.courseMap">
</collection>
</resultMap>
<select id="findById" parameterType="int" resultMap="stuMap">
select s.* ,c.* from t_stu s
inner join t_select ts
on s.sid = ts.sid
inner join t_course c
on c.cid = ts.cid
where s.sid = #{sid};
</select>
</mapper>
本文深入解析了MyBatis框架中Mapper与Dao接口的绑定机制,介绍了如何通过namespace属性将Mapper文件与Dao接口关联,实现SQL语句的调用。通过具体的代码示例,展示了面向接口编程在MyBatis中的应用,以及如何定义和使用resultMap来映射复杂的数据关系。

1702

被折叠的 条评论
为什么被折叠?



