Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example of reverse Engineering on the MyBatis Command Line

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the MyBatis command line to achieve reverse engineering examples, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

Introduction to reverse Engineering of 1.MyBatis

In the development process, there is often a need to use MyBatis situations, usually, MyBatis construction projects can not avoid writing entity classes, Dao interfaces, Mapper mapping files, these work are similar, with a certain degree of repetition, and sometimes error-prone, so someone invented [MyBatis reverse Engineering]-> MyBatis Generator. MyBatis Generator (MBG) is a code generator, which can generate the dao.java, mapper.xml, pojo entity class files we need. For some projects with more tables, it can reduce a lot of work and improve our efficiency. There are many similar tutorials online, you can install plug-ins in eclipse to generate, and generate in IDEA, and so on, because I use the command line to achieve, so here I only introduce the command line way, but also relatively simple.

SQL statements that can be generated include:

Insert

Update by primary key

Update by example (using a dynamic where clause)

Delete by primary key

Delete by example (using a dynamic where clause)

Select by primary key

Select by example (using a dynamic where clause)

Count by example

The changes in these statements depend on the structure of the table (for example, if the table does not have a primary key, MBG does not generate updates through the primary key function). For more information, please refer to the documentation: http://mybatis.org/generator.

two。 Download the MyBatis Generator file

I downloaded it on github, GitHub address: https://github.com/mybatis/generator/releases, it is recommended to use Thunderbolt download should be faster. Download the file and decompress it. The directory of the decompressed file is as follows:

Check the contents of the lib folder after the decompression. Here is the jar package we need to use.

3. Configure the generatorConfig.xml file

Create a new generatorConfig.xml file in the folder you just unzipped. Of course, the name of this file is your own, so what is the use of this file? The configuration file tells MBG:

How to connect to a database

What objects are generated and how to generate them

Which tables are applied to object generation

In addition, we have to do some preparatory work:

1) find the jar package location of your database driver. In order to facilitate the demonstration, I will put the database driver under the directory you just unzipped. Mine is MySQL5.7.

2) create an empty src folder to store the generated files

The generatorConfig.xml file is as follows:

GeneratorConfig.xml code

PUBLIC "/ / mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">"

The information that needs to be configured. Here I have circled it with a red box. The information that needs to be configured includes

Database information

The package name and targetProject of the generated entity class is where our generated file will be stored. Here, I put it in the src directory.

Generate mapper interface information

Generated mapper xml file information, where my mapper interface and xml file are in the same directory

For the configuration information of the database table to be generated, which tables need to be generated can be added later. For the specific meaning of the configuration attributes in the table, please see the description in the document. See here.

4. Generate code

The commands that need to be executed on the command line are as follows:

Java-jar lib/mybatis-generator-core-1.3.1.jar-configfile generatorConfig.xml-overwrite

We can cd to the current directory, and then execute the above command. I'll put it in the bat file here to execute, create a new start.bat file, and copy the above command.

Double-click the execution bat file, you can see the generated file in the src directory, and then copy the generated file to our project project and you can use it!

Ps: in the generated file, some fields or methods may not be created correctly, which should be caused by the inconsistency between the MySQL environment and the plug-in version. You can add the following attributes to the jdbcConnection to try:

Thank you for reading this article carefully. I hope the article "examples of reverse engineering on the MyBatis command line" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report