在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>