The boy stabbed Vincent Uzomah, 50, in a classroom at Dixons Kings Academy, Bradford, on 11 June.
There was "clear evidence" he attacked Mr Uzomah because he was black, the city's crown court heard.
He was handed an 11-year extended sentence, which includes six years' detention and five years on licence.
The boy, who admitted causing grievous bodily harm with intent but denied attempted murder, was told he could serve half of the six years in custody.
He racially abused Mr Uzomah before stabbing him in the stomach with a kitchen knife he had brought to school, the court heard.
Jonathan Sharp, prosecuting, said the boy had discussed the attack with a friend beforehand, and later made "sick" boasts about his actions on Facebook.
About 20 minutes after the attack, the boy, who was not arrested until six hours later, posted a status update containing the line "I stick a blade straight in his tummy".
The post received 69 "likes", something the judge described as "an appalling reflection of a small microcosm of our society".
Mr Sharp told the court the boy had taken a dislike to Mr Uzomah in the seven weeks he had worked at the school.
"The defendant disliked him, claiming he couldn't teach, and freely referred to him by the epithet beginning with the letter n, including saying it in anger just before he attacked him," he said.
He said the boy attacked Mr Uzomah after a row over his mobile phone.
The teenager was described by witnesses as "getting angry, red in the face", Mr Sharp said.
"He approached Mr Uzomah and reached into his pocket, but at that point he took out the knife and stabbed Mr Uzomah in the stomach.
"Mr Uzomah thought he was going to die."
Sentencing the boy, who cannot be named because of his age, Judge Durham Hall QC described him as "out of control" and a bully who "could not tolerate being told off".
Speaking outside court, Mr Uzomah said: "As a Christian, I have forgiven this boy who has inflicted this trauma and pain on to me and my family.
"It was, however, important for the law to run its course and for a strong message to be sent out, especially to kids of similar tendencies, that violence is never acceptable.
"Our prayer for him is that he will make use of the opportunities and support that will be provided to him, to become a changed person who will make a positive contribution to society."
Det Supt Simon Atkinson, from West Yorkshire Police, said: "This was a shocking and very serious attack on a man doing a job he loved, and he was lucky that the consequences were not more serious.
"His attacker showed no concern by fleeing the scene...he worryingly showed a further lack of remorse towards his victim by boasting about it on social media."
Outside court, Mr Sharp said it was "truly remarkable" that Mr Uzomah had forgiven his attacker.
He said: "The victim has suffered serious physical and psychological injuries, has had to put his career plans on hold and is uncertain when, or if, he will feel able to return to teaching - a profession he loved.
"It was also profoundly distressing to Mr Uzomah - and shocking - that the youth posted a Facebook update boasting of what he had done. This subsequently received 69 'likes' from his circle of acquaintances.
"In the circumstances it is remarkable that Mr Uzomah has expressed his desire to forgive the attacker, whilst wishing to see justice done as a deterrent to others."
In a statement posted on the Dixons Kings Academy website, the school said: "This was a very shocking crime, and today a young man has had to face the consequences of his actions.
"We are pleased that Mr Uzomah is making a good recovery, and our school community will continue to do what we can to support Vincent and his family."
A 14-year-old boy who stabbed a supply teacher before bragging about the racially-motivated attack on Facebook has been handed an 11-year sentence.