Quickly create Java domain entity classes corresponding to MySQL tables

Source: Internet
Author: User

Today you create a table with more than 10 fields, and you must write a Java domain entity class that corresponds to it when you create it. Isn't that a repetitive job? Why not first find out all the fields of this table, and then put it in the Linux environment and use the SED tool to precede each line with "private String" followed by ";" after each line. This can save a lot of repetitive work. Below are the SQL code and SED commands.

The SQL code that queries the column names of all columns in a MySQL table is as follows:

SELECT column_name from INFORMATION_SCHEMA. Columnswhere table_name = ' table_name ';

Execution results


OK, after you get all the column names, create a new file on the Linux environment, and copy the column name in. Save exit.

Use SED on Linux to process a file that is a column name for each row. The command is as follows:

[Email protected] tmp]# sed-i "s/^/private string/g" web.txt [[email protected] tmp]# sed-i "s/$/; /g "Web.txt

The first command means to add "private String" at the beginning of each line, and "^" to denote the meaning of the beginning in the regular expression. The second command adds ";" at the end of each line, and "$" dollar the dollar sign to mean the end of the line in a regular expression.

Again cat this file, the output is as follows:


Okay, OK, put this code in your IDE, because the variables of type string are the majority, so the uniform plus "string". The following work is the type of the field that is not the correct type.

This process can knock out a lot of code less. And in the case of using Ibatis, the property name of the class and the field name of the database table if it is the same, then you do not have to write any resultmap.

Finally, add "@Data" annotations (Lombok annotations) to the class name so that the getter, setter method is generated for all fields. The whole class is very concise.

Quickly create Java domain entity classes corresponding to MySQL tables

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.