The defendant, Taleb al-Abdulmohsen, was a psychiatrist originally from Saudi Arabia, described by officials as having a history of anti-Islamic rhetoric and far-right sympathies.
The attack shocked the country and stirred up tensions over the charged issue of immigration, months before a general election in early 2025.
Prosecutors charged the man with murdering six people and the attempted murder of hundreds more in an attack they say was planned over several weeks.
Five women aged 45 to 75 and a nine-year-old boy were killed.
The defendant appears to have acted out of dissatisfaction and frustration over the outcome of a civil law dispute and his lack of success in various criminal complaints, prosecutors have said.
The court on Friday imposed a life sentence with a finding of "exceptional gravity", which means a person will generally not be eligible for parole after the usual 15 years and faces a significantly longer period in prison.
Al-Abdulmohsen had worked as a psychiatrist at a prisoner rehabilitation clinic in Bernburg, about 40km from Magdeburg, but had been absent due to holiday and illness since the end of October 2024.
Initially, the attack drew comparisons on social media to an Islamist-influenced immigrant's deadly attack on a Berlin Christmas market in 2016, but the focus quickly shifted to the defendant's invective against Islam.
Al-Abdulmohsen had appeared in a number of media interviews in 2019, reporting on his activist work helping Saudis who had turned their backs on Islam to flee to Europe, though a Saudi exile group reported clashing with him, saying he was isolated and had problems working with others.
Local media reported that the defendant expressed scant remorse, rambled and was asked by the judge to be brief during the trial.
The defendant also left video messages on his X social media account on the day of the attack.
In rambling commentary, he variously blamed Germany's supposed liberalism for the death of ancient Greek philosopher Socrates, and accused police of stealing a USB stick from him and destroying a criminal complaint he had filed.
Federal police said Germany had received a warning from Saudi Arabia as far back as 2023 about the defendant, which authorities investigated but found vague.
A large number of affected individuals joined the trial proceedings as joint plaintiffs, represented by about 40 lawyers.