The invention discloses a method and a system for automatically generating a code based on a data model drive. The method comprises the steps as follows: firstly, establishing user configuration information according to user settings, wherein the user configuration information comprises a package name generated by the code, a class name, a primary table required to generate the code and an association of the primary table; secondly, generating an xml (extensive markup language) file capable of being parsed by a preset server side by the system according to the user configuration information; and lastly, parsing the xml file by the server side, and generating the standard code according to a code template. Compared with the prior art, the method has the advantages of high efficiency, standard and uniform code, high readability, strong maintainability and the like; and meanwhile, the operation is visualized, convenient and quick, errors are not easily caused, and the technical requirements are lowered.