当您使用具有与 xml 中的 sql 完全相同的方法签名的抽象方法创建映射器接口时。
例如。这是包含实际查询的 dao.xml 的命名空间。
<mapper namespace=" com.mybatis.dao.EntityMapperInterface">
<select id="selectEmployeeWithId" parameterType="Long"
resultType="com.mybatis.domain.Employee">
select id,name from employee where 1=1
<if test="_parameter != null">
AND id=#{id}
</if>
order by id
</select>
会映射到接口 com.mybatis.dao.EntityMapperInterface
public interface EntityMapperInterface {
public List<Employee> selectEmployeeWithId(Long id);
mybatis-config文件
<mappers>
<mapper resource="com/mybatis/mappers/EntityMapper.xml" />
</mappers>
你如何从 Action 类/Servlet 中调用它?当你初始化 SqlSession 时,
EntityMapperInterface emi = session.getMapper(EntityMapperInterface.class);
List eList = emi.selectEmployeeWithId(1);