Unfortunately rejection is a part of the traditional publishing process. You write your book, polish it up, and send it out to agents and editors. And then you eagerly await their ecstatic reply.
Except the glowing offer of representation or the perfect publishing contract doesn't come. Instead you get a rejection letter. Ouch.
It can't be avoided. I don't think anyone's been published right out of the shoots. It takes time and effort to bring your work up to the level it needs to be for representation and publication. The thing to remember is that publishing is subjective, and what one agent or editor passes on another will love.
My first novel, Searching for Spice, was rejected several times. One letter from an agent thanked me for sharing it but went on to say, "I only represent clients with good ideas and good execution of those ideas -- neither of which was apparent in your work." Double ouch. A few months later, I got a contract with Tyndale House Publishers for that book and Out of Her Hands.
My agent, Rachelle Gardner, posted this week about rejection letters she received on behalf of her clients. Take a look. But the bottom line is that those rejected books were eventually sold to a publisher who loved the book.
Here's a link to an article about 30 famous authors whose work was repeatedly rejected.
Be encouraged. Don't give up. Keep learning the craft, and write on!