在mybatis中,映射文件中的namespace是用于绑定Dao接口的,即面向接口编程。当namespace绑定接口后,可以不用写接口实现类,mybatis会通过该绑定自动找到对应要执行的SQL语句,如下:
接口:
public interface UserDAO {
public User login(User u);
}
接口中的方法与映射文件中的SQL语句的ID一一对应
映射文件:
<mapper namespace="com.demo.dao.UserDAO">
<resultMap type="User" id="UserRes">
<result column="id" property="id"/>
<result column="userName" property="userName"/>
<result column="password" property="password"/>
</resultMap>
<select id="login" parameterType="User" resultMap="UserRes">
select * from t_user where userName=#{userName} and password=#{password}
</select>
</mapper>