Detailed steps for using mybatis reverse engineering and mybatis Reverse Engineering

Source: Internet
Author: User

Detailed steps for using mybatis reverse engineering and mybatis Reverse Engineering

Using mybatis to generate reverse engineering. I personally think This is the simplest one. Although there are many ways to generate reverse engineering on the Internet, this method is the simplest. Here I use maven to build an environment, but the same is true in a normal environment.
Steps:
1. Create a genreatorConfig. xml file with any name. When I created the file, I put it under src/main/resources. The content of this file does not need to be remembered. I just need to find it online. All we need to do is modify some part of the configuration file and change it to our own data.

 

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE generatorConfiguration 3 PUBLIC "-// mybatis.org//DTD MyBatis Generator Configuration 1.0 // EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 <! -- Database driver, it is best not to have Chinese characters, otherwise it will not be found --> 8 <classPathEntry location = "D: \ MysqlJdbcconnector/mysql-connector-java-5.1.41-bin.jar "/> 9 10 11 <context id =" DB2Tables "targetRuntime =" MyBatis3 "> 12 13 <commentGenerator> 14 <property name =" suppressDate "value =" true "/> 15 <property name =" suppressAllComments "value =" true "/> 16 </commentGenerator> 17 <! -- Database link address account password --> 18 <jdbcConnection driverClass = "com. mysql. jdbc. driver "connectionURL =" jdbc: mysql: // localhost/pet_hospital "userId =" root "password =" 112233 "> 19 </jdbcConnection> 20 <javaTypeResolver> 21 <property name =" forceBigDecimals "value =" false "/> 22 </javaTypeResolver> 23 <! -- Generate the storage location of the Model class --> 24 <javaModelGenerator targetPackage = "com. qianfeng. bean "targetProject =" src/main/java "> 25 <property name =" enableSubPackages "value =" true "/> 26 <property name =" trimStrings "value =" true" /> 27 </javaModelGenerator> 28 <! -- Generate the location where the ing file is stored --> 29 <sqlMapGenerator targetPackage = "com. qianfeng. bean "targetProject =" src/main/java "> 30 <property name =" enableSubPackages "value =" true "/> 31 </sqlMapGenerator> 32 <! -- Generate the storage location of the DaoMapper class --> 33 <javaClientGenerator type = "XMLMAPPER" targetPackage = "com. qiafeng. dao "targetProject =" src/main/java "> 34 35 <property name =" enableSubPackages "value =" true "/> 36 37 </javaClientGenerator> 38 <! -- Generate the corresponding table and class name. One thing to remember is that reverse engineering cannot generate associations, only one table operation can be generated --> 39 <table tableName = "owners" 40 domainObjectName = "Owners" 41> </table> 42 <table tableName = "pets" 43 domainObjectName = "Pets "44> </table> 45 <table tableName =" types "46 domainObjectName =" Types "47> </table> 48 <table tableName =" employee "49 domainObjectName =" Employee "50> </table> 51 <table tableName =" specialties "52 domainObjectName =" Specialties "53> </table> 54 <table tableName =" vets "55 domainObjectName =" Vets "56> </table> 57 <table tableName =" visits "58 domainObjectName =" Visits "59> </table> 60 <table tableName =" vet_specialties "61 domainObjectName =" VetSpecialties "62> </table> 63 64 </context> 65 </generatorConfiguration>

 

 

 

2. Import the corresponding jar package, mybatis-generator-core and mysql-connector-java.

3. Create a test class and run the following code.

 

1 public static void generator () throws Exception {2 List <String> warnings = new ArrayList <String> (); 3 boolean overwrite = true; 4 // do not include Chinese characters in the project root path. Therefore, use the absolute path 5 File configFile = new File ("src/main/resources/genreatorConfig. xml "); 6 ConfigurationParser cp = new ConfigurationParser (warnings); 7 Configuration config = cp. parseConfiguration (configFile); 8 DefaultShellCallback callback = new DefaultShellCallback (overwrite); 9 MyBatisGenerator myBatisGenerator = new MyBatisGenerator (config, callback, warnings); 10 myBatisGenerator. generate (null); 11} 12 public static void main (String [] args) {13 try {14 generator (); 15} catch (Exception e) {16 // TODO Auto-generated catch block17 e. printStackTrace (); 18} 19}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.