LLM Hallucinations in Practical Code Generation: Phenomena, Mechanism, and Mitigation