Actually, quite a few were written specifically by composers of the day. For example, multiple Charles Wesley hymns were set to tunes composed by the composer of "The Messiah," George Friedrich Handel. www.cyberhymnal.org would give you a pretty good idea about how many of these songs actually have "barroom tunes." (One thing I've noticed recently was the fact that I have often heard the line about hymns being set to barroom tunes parroted, but have never really heard any credible source from anyone who's said such a thing. Are there any, I wonder?)
The tune is "New Britain" which (the tune) appeared in the tunebook "Columbian Harmony" in 1829.
Many of the older hymnals had just lyrics (no musical score)with a notation as to the metre. The pastor or music minister would match them up with a tune that had the same metre and present this as the hymn. If you attend a church that is older you can usually find them in the library. I believe they are still being published today, although not frequently.
I don't believe they teach this in our seminaries now, but a hundred years ago Pastors were taught how to write hymns as part of their training and they would join the lyrics with a more or less popular tune. Many of these hymns have made it into our hymnals of today, but many thousands of hymns have been lost because they were sung in only one church on one particular Sunday.
We still do this in the latest form of contemporary Christian worship. Michael W. Smith will shout the starting words of the next section to his choir and I think it is a beautiful, beautiful addition to the worsip experience.