Zhao Zewei, 28, was arrested in April following the killings that he said were in response to a long-held grudge against the school.
The attack was "premeditated murder," the Yulin Intermediate Court said in a statement, adding that the circumstances warranted the death penalty.
"The goal of the crime was clear, the murderer's methods were despicable, and the consequences were extremely grave," it said on a statement on its website.
After his arrest, Zhao said he had been bullied when he attended the school, "hated" his classmates and decided to use a "dagger" to kill people, the court said in its verdict.
In preparation for the crime, he purchased five knives online. He carried three of them to the school, where he waited outside the gate for classes to finish before "rushing headlong into the stream of students and ... stabbing wildly," the court said.
Nine students died, another four were seriously wounded and seven others received light injuries from the assault, it said.
Zhao was subdued by teachers, security guards and students and then handed over to the police.
Photos from the trial show an emaciated Zhao being held by two police officers in the dock as he received his sentence.
Knife attacks are not uncommon in the country.
Two boys were stabbed to death outside a Shanghai elementary school in June. In February, a knife-wielding man with a personal grudge killed a woman and injured 12 others in a busy Beijing shopping mall - a rare act of violence in the heavily policed capital.
In the southern city of Shenzhen, a man armed with a kitchen knife killed two people and wounded nine others in a supermarket last July.
(This story has not been edited by Business Standard staff and is auto-generated from a syndicated feed.)