Skip to content

更正:Dao 接口里的方法可以重载,但是Mybatis的XML里面的ID不允许重复! #1122

@charlienss

Description

@charlienss

更正:Dao 接口里的方法可以重载,但是Mybatis的XML里面的ID不允许重复!

guide

此处错误更正:

Dao 接口里的方法可以重载,但是Mybatis的XML里面的ID不允许重复。

Mybatis版本3.3.0,亲测如下:

/**
 * Mapper接口里面方法重载
 */
public interface StuMapper {

	List<Student> getAllStu();
    
	List<Student> getAllStu(@Param("id") Integer id);
}

然后在StuMapper.xml中利用Mybatis的动态sql就可以实现。

	<select id="getAllStu" resultType="com.pojo.Student">
 		select * from student
		<where>
			<if test="id != null">
				id = #{id}
			</if>
		</where>
 	</select>

能正常运行,并能得到相应的结果,这样就实现了在Dao接口中写重载方法。

总结:

Mybatis的Dao接口可以有多个重载方法但是方法对应的映射必须只有一个也就是Mapper的XML中定义的ID名不能重复否则启动会报错

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussdiscuss a problemenhancementNew feature or request or suggestionperfectImprove knowledge points or descriptions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions