Usage of mybatis generator code generator in mybatis framework of Java

About mybatis generator mybatis generator (MBG) is a code generator of mybatis, mybatis and ibatis It can generate code for various versions of mybatis and ibatis 2.2 Code after version 0. It can introspect database tables (or multiple tables) and then generate basic objects that can be used to access (multiple) tables. In this way, there is no need to create objects and configuration files when interacting with database tables. MBG solves some simple cruds that have the greatest impact on database operations (insert, query, update, delete) operation. You still need to write SQL and objects for federated queries and stored procedures.

Mybatis generator generates:

Java POJOs matching the table structure may include:

There will be appropriate inheritance relationships between these classes. Note that the generator can be configured to generate hierarchies of different types of POJOs. For example, if you wish, you might choose to generate a separate entity object for each table.

Mybatis / ibatis compatible SQL mapping XML file. MBG generates SQL for simple CRUD operations for each table in the configuration. The generated SQL statements include:

According to the structure of the table, the generated statements will have different changes (for example, if there is no primary key in the table, MBG will not generate the update by primary key method).

The Java client class will use the above objects appropriately, which is optional when generating java client classes. MBG will be mybatis 3 X generates the following client class: one that can work with mybatis 3 The mapper interface class MBG used with X will be ibatis 2 X generates the following client classes: Dao classes conforming to the spring framework. Dao using ibatis SQL mapping API only. This Dao can be generated in the following two ways: providing sqlmapclient through constructor or setter injection. Dao conforming to ibatis Dao framework (an optional part of ibatis. This framework is outdated. We recommend that you use spring framework instead). Mybatis generator can run well in the iterative development environment and act as an ant task or Maven plug-in in the continuous construction environment. When running MBG, remember the following important things:

MBG will automatically merge XML that already exists and has the same name as the newly generated file. MBG does not overwrite your changes to the generated XML. You can run it repeatedly without worrying about losing your custom changes. MBG will replace all XML elements generated in previous runs. MBG does not merge java files. It can overwrite existing files or save newly generated files with a different and unique name. You can merge these changes manually. MBG can automatically merge java files when you use eclipse plug - ins

Basic usage: the operation of MBG mainly depends on an XML configuration file. First, we can re create a project named mybatisgenerator and create three packages named config and David Test, and David MBG and config packages mainly store the configuration files needed in the real mybatis. You can use mybatis in the projects in the previous chapters_ demo_ config. Copy the XML and put it in this directory, etc. for testing the program, David As the name suggests, test is to store the following common methods and test programs. You can also take the mybatisutils tool class used in the previous chapters and create the corresponding mainfunction for testing. And the last David MBG is the XML we want to configure today, and MBG generates a configuration file.

As shown in the figure, let's create a new one named MBG_ configuration. XML configuration file, details are as follows:

You can notice that these nodes are the main ones

< classpathentry > = > the location where the jdbc driver package is stored can be either a relative path or an absolute path. In this example, a relative path is used

< context > = > corresponds to the configuration of all tables under a database. There can be multiple contexts, one configured with MySQL and the other configured with Oracle.

The < context > node mainly includes:

< commentgenerator > = > comment generation node. In this example, the lower 2 child nodes are represented by tables

Suppressallcomments = > remove all automatically generated comment files

Suppressdate = > whether to remove the timestamp of all automatically generated files. The default value is false

< JDBC connection > = > database connection configuration information

< javatyperesolver > = > convert decimal and numeric types in JDBC to Java math. Bigdeciaml formal representation

< javamodelgenerator > = > configure your POJO entity class, targetpackage = "David. Model", corresponding to your enrollment, you can name it according to your actual business, targetproject = "SRC". In the eclipse environment, it refers to the path of the project and source folder, generally refers to the SRC directory, and your packages will be created in this directory. If it is not the eclipse environment, The value here should be an actual file system path. If the specified path does not exist, an error will be reported because MBG will not create the corresponding folder itself

< sqlmapgenerator > = > configure to generate the corresponding entity mapper XML, for mapper3 X we need to type = "xmlmapper"

< javaclientgenerator > = > configure and generate corresponding interface classes, corresponding to mapper A series of crud method SQL statements in XML

< Table > = > configure the corresponding database, which indicates the domain class name (i.e. entity class name) you want to generate. In this example, I turn off all unnecessary example generation information

All the above information can be viewed on the official website or downloaded from my file, with corresponding configuration instructions and related application examples. Download document

After configuring the above information, what is the last step? We are going to run this script file. There are four ways in the official instructions. First, it is generated through the command line, second and third, it is generated through tools such as ant or maven, and the last one is generated through Java code. We will use the method generated through Java code here. Add a method to generate script in demorun class as follows:

After running the refresh project, you will find that the following magic helps you generate the main configuration, as shown in the red box below:

Finally, let's use the automatically generated results. We can add corresponding crud test methods in demorun by referring to the methods in the previous six chapters, as follows:

Run the test program and the results will come out

Do you feel that using this helps you improve a lot of efficiency? You don't have to worry about cumbersome configuration. At least you don't have to do repeated unnecessary steps. Let's leave these to the tool ^ 0 ^. Of course, in actual use, we may need to modify the corresponding class information and interface information names after generation. Of course, these workload is not too much. I hope these contents will be helpful to the students who need configuration today.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>