MyBatis Mapper接口的配置需要完成以下几个步骤:
public interface UserMapper {
User getUserById(int id);
}
<!-- UserMapper.xml -->
<mapper namespace="com.example.mapper.UserMapper">
<select id="getUserById" resultType="com.example.model.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
<!-- mybatis-config.xml -->
<configuration>
<mappers>
<package name="com.example.mapper"/>
</mappers>
</configuration>
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(int id) {
return userMapper.getUserById(id);
}
}
这样就完成了MyBatis Mapper接口的配置。注意,Mapper接口的命名规范和XML文件的命名规范需要按照约定来做,以便MyBatis能够正确地进行映射和配置。