Mybatis reverse engineering,
Link: https://www.cnblogs.com/shanheyongmu/p/5863961.html1. What is reverse engineering?
One of the main features of mybatis is that programmers need to write their own SQL statements, so if there are too many tables, it will inevitably be very troublesome, so mybatis officially provides a reverse engineering, the Code required for mybatis execution can be automatically generated for a single table (including mapper. xml, mapper. java, po ..). Generally, during development, the common reverse engineering method is to generate code through the database table.
2. Use Reverse Engineering
Using mybatis reverse engineering, you need to import the jar package of reverse engineering, I am using a mybatis-generator-core-1.3.2, has been uploaded to the download channel (click I download ), the following describes how to use mybatis reverse engineering.
2.1 create a project (important)
We need to create a new java project, which is used to generate code using reverse engineering. Some may ask why we need to create a new project? Can you generate it directly in the original project? This is indeed the case. It can be generated in the original project, but it is risky because mybatis is generated based on the configuration file (as described below). If the generated path contains the same file, it will overwrite the original file, which is risky. Therefore, during development, a new java project is usually created to generate the file, and then the generated file is copied to your own project, which is not troublesome and safe. As follows:
From this point of view, 1 is the java code to be executed. Execute it to generate the code we need; 2 is the new package during execution, this package can be specified in the configuration file 4, preferably the same as the package name of our own project. You can copy the package directly later without modifying the package name; 3 is the jar package; 4 is the configuration file. The following is a detailed analysis.
2.1 code generation configuration file
Mybatis requires a configuration file for reverse engineering code generation. Then, mybatis generates the corresponding code based on the configuration in this configuration file. After downloading the jar package, there is a help document in it. After opening the jar package, there is a configuration file template in it. I will not go into details here. Write the configuration file below:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE generatorConfiguration PUBLIC "-// mybatis.org//DTD MyBatis Generator Configuration 1.0 //" http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd "> <generatorConfiguration> <context id =" testTables "targetRuntime =" MyBatis3 "> <commentGenerator> <! -- Whether to remove automatically generated comments true: Yes: false: No --> <property name = "suppressAllComments" value = "true"/> </commentGenerator> <! -- Database connection information: Driver Class, connection address, user name, password --> <jdbcConnection driverClass = "com. mysql. jdbc. driver "connectionURL =" jdbc: mysql: // localhost: 3306/mybatis "userId =" root "password =" root "> </jdbcConnection> <! -- <JdbcConnection driverClass = "oracle. jdbc. oracleDriver "connectionURL =" jdbc: oracle: thin: @ 127.0.0.1: 1521: yycg "userId =" yycg "password =" yycg "> </jdbcConnection> --> <! -- The default value is false. The jdbc decimal and NUMERIC types are parsed to Integer. If the value is true, the jdbc decimal and NUMERIC types are parsed to java. math. bigDecimal --> <javaTypeResolver> <property name = "forceBigDecimals" value = "false"/> </javaTypeResolver> <! -- TargetProject: the location where the PO class is generated. It is important !! --> <JavaModelGenerator targetPackage = "mybatis. po" targetProject = ". \ src"> <! -- EnableSubPackages: whether to use schema as the package suffix --> <property name = "enableSubPackages" value = "false"/> <! -- Space before and after the value returned from the database --> <property name = "trimStrings" value = "true"/> </javaModelGenerator> <! -- TargetProject: the location where the mapper ing file is generated. Important !! --> <SqlMapGenerator targetPackage = "mybatis. mapper" targetProject = ". \ src"> <property name = "enableSubPackages" value = "false"/> </sqlMapGenerator> <! -- TargetPackage: Location generated by the ER interface. Important !! --> <JavaClientGenerator type = "XMLMAPPER" targetPackage = "mybatis. mapper "targetProject = ". \ src "> <property name =" enableSubPackages "value =" false "/> </javaClientGenerator> <! -- Specifies the database table. If you want to generate the tables, you can write the tables corresponding to the database. You cannot write an error! --> <Table tableName = "items"> </table> <table tableName = "orders"> </table> <table tableName = "orderdetail"> </table> <table tableName = "user"> </table> </context> </generatorConfiguration>
From the configuration file above, we can see that the configuration file mainly needs to do the following:
2.3 run the generated program
After the configuration file is ready, execute the following generated program to generate the generated java program. The downloaded reverse engineering document contains examples as follows:
Public class GeneratorSqlmap {public void generator () throws Exception {List <String> warnings = new ArrayList <String> (); boolean overwrite = true; // point to the reverse engineering configuration File configFile = new File ("generatorConfig. xml "); ConfigurationParser cp = new ConfigurationParser (warnings); Configuration config = cp. parseConfiguration (configFile); DefaultShellCallback callback = new DefaultShellCallback (overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator (config, callback, warnings); myBatisGenerator. generate (null);} public static void main (String [] args) throws Exception {try {GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap (); generatorSqlmap. generator ();} catch (Exception e) {e. printStackTrace ();}}}
Run it. After running, refresh the project and you will see the latest code.
Here we can see that there is one more thing for each po class, namely xxxExample. java. This class is used for user-defined SQL and will be mentioned later. The code is generated here. Next we will copy the generated code to our own project for ease of use. Here I will copy ItemsMapper. java/ItemsMapper. xml/Items. java/ItemsExample. java and others are the same.