It's a teachable moment: even on the cross, Jesus remains the teacher
But first, the parallels between Jesus and David. There are many. There is an entire school of study that investigates, through types and typology, how Jesus and David are deeply linked even though the local Jews, hoping for a Messiah like King David, got a different kind of Messiah (annointed one) in Jesus. They were in a lot of ways "similar but different."
Jesus was telling his enemies that their "victory" wasn't going to last
A way to understand his terse allusion to Psalm 22, one of King David's Psalms, is that Jesus was telling all those who would listen, and understand, that he knew how this (his crucifixion) was going to turn out, and that result was going to be a victory for the Lord, not the apparent defeat of a humiliating death on the cross.
This kind of "public showing up of one's opponents" was a core element of the Mediterranean cultural context that is described in the works of John J. Pilch (theologian). (Titles include The Cultural World of Jesus; The Cultural World of the Apostles, Introducing the Cultural World of the New Testament, and more)
A Rabbi is a teacher, and teacher's teach
Now there was a man of the Pharisees named Nicodemus, a ruler of the
Jews. This man came to Jesus by night and said to him, “Rabbi, we know
that you are a teacher come from God, for no one can do these signs
that you do unless God is with him.” Jesus answered him, “Truly,
truly, I say to you, unless one is born again he cannot see the
kingdom of God.” Nicodemus said to him, “How can a man be born when he
is old? Can he enter a second time into his mother’s womb and be
born?” (John 3:1–4) {snip} Nicodemus said to him, “How can these things be?” Jesus answered him, “Are you the teacher of Israel and yet you do not understand these things?” (John 3:9–10)
To the last, and even to his last breath, Jesus was teaching all of those who had ears to hear.
Mark’s (1:22): "He taught them with authority, not like the scribes."
Jesus is also referred to in a number of passages as "Master" which may also be construed to be equivalent to "Teacher" along the lines "Master" and "Apprentice" ...
In the New Testament the term didàskalos is used 48 times in
the Gospels, mostly applied to Jesus; and the verb didàskein, to
teach, is 95 times...prevalently applied to Jesus. Hence Jesus is the
"master" par excellence of the Christian community.
Many observers at the cross were observant Jews, to include some of the priestly classes. They would all be familiar with Psalm 22.
Citing the opening line of a psalm can be used as a short hand to allude to the whole of the Psalm. For example, if today you (assuming some familiarity with the 23rd Psalm) here someone open with "the Lord is my Shephard, I shall not want" it easily keys in your mind and understanding a to the whole of that Psalm. Jesus is reminding the observers of the 22d psalm, which has a beginning and an end.
So how does Psalm 22 end?
Psalm 22, (25-32)
For he has not spurned or disdained the misery of this poor wretch, Did not turn away* from me, but heard me when I cried out.
I will offer praise in the great assembly; my vows I will fulfill before those who fear him.
The poor* will eat their fill; those who seek the LORD will offer praise.
"May your hearts enjoy life forever!”
All the ends of the earth will remember and turn to the LORD; All the families of nations will bow low before him.
For kingship belongs to the LORD, the ruler over the nations.
*All who sleep in the earth will bow low before God; All who have gone down into the dust will kneel in homage.
And I will live for the LORD; my descendants will serve you.
The generation to come will be told of the Lord, that they may proclaim to a people yet unborn the deliverance you have brought.
He, Jesus, knows that his Passion ends in victory. By making reference to that Psalm, which is easily recognized by how it begins, Jesus tells all who have ears to hear, how this (what he's going through) ends.
Put into a modern context and into contextual phrasing, the message from Jesus up on the cross, in immense pain and suffering, is this: he is telling his tormenters and enemies "You may think you have me now, but I know where this is going - and you aren't winning." As www.gffg.info pointed out ...
"The Spirit of Christ, which was in the prophets, testifies in this
psalm, clearly and fully, the sufferings of Christ, and the glory that
should follow." -Matthew Henry