BATON ROUGE, La. — Family, friends and close comrades came Friday to pay their last respects to Matthew Gerald, a 41-year-old rookie police officer and one of three officers slain by a gunman who was targeting the police.  But many of the spots in the 2,500-seat megachurch here were packed with people who never knew Officer Gerald — though they knew, intimately, of the perils he faced. The officers and sheriff’s deputies came from around the country in their crisp, formal finery — all aiguillettes and polished shoes, Stetsons and broad-brimmed campaign hats. They came from Los Angeles County; Madison, Wis.; and Mesa, Ariz. An officer came from French Settlement, a Louisiana village of just over a thousand residents near the Amite River. A pair of Royal Canadian Mounted Police officers came, almost exotic in their blazing red serge.  It was a measure of the regard for Officer Gerald’s sacrifice, and a dramatic testament to the wide and unsettling reverberations set off by the killing of the three officers here on Sunday. Officer Gerald was the first to be buried. His funeral at Healing Place, a suburban church he attended with his family, began with an honor guard draping his coffin with an American flag.  “I pray for every law enforcement officer,” Ken Spivey, a deputy and chaplain with the nearby Ascension Parish sheriff’s office, told the crowd — not just the officers in this corner of Louisiana, Deputy Spivey added, but all across the nation. “Because they’re under attack.”  Three other area law enforcement officers were injured in the attack. The gunman, Gavin Long, 29, a former Marine who was African-American, had stated online that he believed that bloodshed was a better tool than peaceful demonstrations as a means of protesting the numerous police shootings of African-Americans that have rocked the nation in recent months — including the killing of Alton B. Sterling, a CD peddler, outside a Baton Rouge convenience store on July 5.  Two days later, five law enforcement officers were fatally shot in downtown Dallas by Micah Johnson, a black man who was targeting white officers.  In an emotional speech before Officer Gerald’s coffin, Chief Carl Dabadie Jr. of the Baton Rouge police argued that the national conversation about the events was skewed against the police. “You know, the media has blasted us for what we do and how we do it,” he said, portraying law enforcement “as basically this bunch of bullies who go around and beat people up.”  He then encouraged everyone to look around the church. These were not bullies, he said, but the people “protecting our communities. And they throw us under the bus for it. And that’s wrong.”  During the funeral service, Officer Gerald was praised as a man with a deep love for his country and community. His career as a public servant was extensive and somewhat unorthodox. According to the Marine Corps, he spent four years as a Marine, leaving with the rank of corporal in 1998. A close friend and former housemate, Dave Mulkey, recalled how he and Officer Gerald watched the Sept. 11, 2001, attacks together, “glued to the TV, crying.” Eventually, he said, Officer Gerald got up from the sofa and declared: “I’m going back in, for these people.”  Mr. Gerald enlisted in the Army, where he was deployed to Iraq three times, repairing Black Hawk helicopters.  In a candid and moving letter distributed at the funeral, his wife, Dechia Badeaux Gerald, whom he married in 2010, wrote about a union marked by serious obstacles. At one point, she wrote, Officer Gerald had become “low and distant” as he searched for purposeful work after leaving military life. He eventually graduated from the Baton Rouge police academy in March, a month after he turned 41. Fellow officers called him “Benjamin Button,” an apparent reference to the 2008 Brad Pitt film about a man who ages in reverse.  “When Matt answered the call to become a police officer,” Ms. Gerald wrote, “my prayers were answered and our marriage was made anew — as only God can do.”  In addition to his wife, Officer Gerald leaves behind a 9-year-old stepdaughter, Dawclyn Badeaux, and a 3-year-old daughter, Fynleigh Gerald.  The funeral for Brad Garafola, an East Baton Rouge sheriff’s deputy killed in the attack, is scheduled for Saturday. The funeral for the third victim, Montrell Jackson, Officer Gerald’s colleague in the Baton Rouge Police Department, is set for Monday.  On Friday, hundreds of people, some of them braving heavy thundershowers, lined the freeway, waving hands and American flags as the funeral procession made its way.  There will certainly be more grim pageantry as the officers bury their own. But during the service, Mr. Mulkey deftly evoked the humanity of the man behind the uniform.  He said his friend had a soft spot for “Lonesome Dove,” the TV mini-series based on Larry McMurtry’s novels about two retired Texas Rangers on an epic cattle drive. He recalled how, when the two men lived together, they would often start the day with silly serenades, singing to each other, “Good morning, Brother Mulkey,” and “Good morning, Brother Gerald.”  When Officer Gerald watched war movies, he said, he would often yell at the screen, “You never leave a man behind.”  A version of this article appears in print on July 23, 2016, on page A13 of the New York edition with the headline: Rites in Baton Rouge Draw Officers From Afar. Order Reprints| Today's Paper|Subscribe
Officer Gerald, 41, a military veteran, was remembered as a man whose decision to join the Baton Rouge Police Department renewed his sense of purpose.