Write a few simple shell scripts and cooperate with IDEA to easily convert the oracle table-building statements into the mapper configuration of java bean and mybatis

background

Some time ago, I encountered a demand. The requirement is very common, that is, some CRUD of oracle tables.

The workload is a bit heavy. I gave a few sql scripts. There are more than 60 tables in it, and each table generally has 20 or 30 fields or 40 or 50 fields.

Mybatis is used. In this case, a corresponding java bean class and a corresponding mapper file need to be created for each table, and related queries and insert statements need to be provided.

I thought about it, if I knock it hard, I will be crazy.

Mybatis has a reverse tool, I have heard of it, but I haven't used it. But as soon as I saw that these table-building statements were all the same, I wrote a few simple scripts to convert them into corresponding beans or mappers.

What I learned recently is a bit deep, not suitable for blogging, just share the script I wrote, and you can use it if you are interested, because when these sql scripts are exported with pl/sql, they basically look like this.

script

For example, this is a table building statement. There are many actual table fields. I only wrote a few fields as examples. The field names are named like this, and uppercase and lowercase letters are connected by underscores.

create table example_table  (
   user_name            VARCHAR2(200)                   not null,
   str1                 VARCHAR2(200)                   not null,
   str2                 VARCHAR2(200)                   not null,
   str3                 VARCHAR2(200),
   ONE_FLAG             VARCHAR2(1)                    default 'N' not null,
   time5                TIMESTAMP,
   str6                 VARCHAR2(1)                    default 'N' not null,
   reserved1            VARCHAR2(200),
   reserved2            VARCHAR2(200)
);

comment on table example_table is
'样例表';

comment on column example_table.user_name is
'名';

comment on column example_table.str1 is
'这是一个字符串1';

comment on column example_table.str2 is
'这是一个字符串2';

comment on column example_table.str3 is
'这是一个字符串3';

comment on column example_table.ONE_FLAG is
'这是一个标记字符串';

comment on column example_table.time5 is
'时间';

comment on column example_table.str6 is
'又来一个字符串';

comment on column example_table.reserved1 is
'预留字段1';

comment on column example_table.reserved2 is
'预留字段2';

Create a file named txt in the directory where the script to be written below is located, and copy the table-building statement above into it.

Generate java bean

1. Write an awk script (generate_bean.awk): convert table fields into variable definition syntax, and add comments

#! /usr/bin/awk -f

BEGIN {
        i = 1;
}
{
        comment="";
        if ( NR != 1 && i > 0 && $1 != "(" ) {
                if ( $1  == ");" ) {
                        i = -1;
                } else {
                        if ( NF == 1 ) {
                                print "// "$1;

                        }
                        else {
                                "grep -A 1 comment "FILENAME" | grep -i  -A 1  "$1" | grep -i -v "$1 | getline comment;
                                print "// "comment;
                                ". to_lower_word.sh " $1 | getline lower_word;
                                print "private String "lower_word";";
                        }
                }

                i++;
        }

}

You can execute it to see the effect: ./generate_bean.awk txt

Among them, to_lower_word.sh is a variable named str1_str2 that is converted into a small camel case, such as:

#! /bin/bash

echo $1 | tr [:upper:] [:lower:] | sed 's/_[a-z]\{1\}/\U&/g' | sed 's/_//g'

2. Write another script (gen_ben.sh) to call generate_bean.awk and remove the impurities in the comment

#! /bin/bash

sed -i 's/^$//g' txt;

if [ ! -e tmp_txt ]; then
        touch tmp_txt;
fi

generate_bean.awk txt > tmp_txt;
sed "s/';//g" tmp_txt | sed "s/'//g"

rm tmp_txt

Take a look at the implementation effect:

3. Open IDEA, create a new class, and copy it in.

Alt+insert, quickly generate get, set and toString methods:

There is a problem here. In order to write the script in a hurry, the writing is relatively low, and some places need to be adjusted. For example, here are all converted to String type by default, and other types need to be changed manually.

Generate mytatis ResultMap

1. Write an awk script (generate_result.awk): convert the field to a small hump, and then splicing processing

#! /usr/bin/awk -f


BEGIN {
        i = 1;
        print "<resultMap type=\"\" id=\"\">";
}
{
        file = FILENAME;
        if ( NR != 1 && i > 0 && $1 != "(" ) {
                if ( $1  == ");" ) {
                        i = -1;
                } else {
                        if ( NF > 1 ) {

                                ". to_lower_word.sh " $1 | getline lower_word;
                                print "\t<result column=\""$1"\"\tproperty=\""lower_word"\"/>";
                        }
                }

                i++;
        }

}
END {
        print "</resultMap>"
}

2. Write another script (gen_result.sh) to call this script:

#! /bin/bash
generate_result.awk txt

3. Perform to see the effect

Copy to the xml file of mapper

Generate select sql

1. Write an awk script (generate_fields.awk) to convert the database table fields into two columns of data in small camel case format, the first line is the table name:

#! /usr/bin/awk -f

BEGIN {
        i = 1;
}
{
        if ( NR == 1 ) {
                print $3;
        }
        if ( NR != 1 && i > 0 && $1 != "(" ) {
                if ( $1  == ");" ) {
                        i = -1;
                } else {
                        ". to_lower_word.sh " $1 | getline lower_word;
                        print $1,lower_word;
                }

                i++;
        }

}

Take a look at the effect:

2. Write another script (gen_select.sh) to splice this into a select sql

#! /bin/bash

generate_fields.awk txt > fields_tmp.txt

table_name=`head -1 fields_tmp.txt`
lines=`wc -l fields_tmp.txt | awk '{print $1}'`
awk -v total=$lines 'BEGIN {print "select";}{ if (NR > 1 && NR < total && NF > 1) {print "\t"$1",";}} END {print "\t"$1;}' fields_tmp.txt;

echo "from $table_name"
echo "<where>"

awk '{if ( NR > 1 ) { print "\t""<if test=\""$2" != null and "$2" != ""\x27\x27""\">" "\n\t\t AND " $1" = #{" $2 "}" "\n\t</if>" }}' fields_tmp.txt;

echo "</where>"

rm fields_tmp.txt

3. Under execution, copy to mapper.xml and see the effect:

There are some fields in it, you should delete them as needed, and adjust and adjust

Generate insert

1. Script (gen_insert.sh)

#! /bin/bash

generate_fields.awk txt > fields_tmp.txt

table_name=`head -1 fields_tmp.txt`
lines=`wc -l fields_tmp.txt | awk '{print $1}'`
awk -v total=$lines -v table_name=$table_name 'BEGIN {print "insert into "table_name"(";}{ if (NR > 1 && NR < total && NF > 1) {print "\t"$1",";}} END {print "\t"$1")";}' fields_tmp.txt;

echo "values("

awk -v total=$lines '{if ( NR > 1 && NR < total ) { print "\t#{" $2 ", jdbcType=VARCHAR},"  }} END {print "\t#{"$1 ", jdbcType=VARCHAR}"}' fields_tmp.txt;

echo ")"

rm fields_tmp.txt

2. Execute, copy to mapper.xml, the effect is as follows:

postscript

I still remember that day, I didn't sleep at noon and spent 2 hours before tinkering with this script.

The next morning, at my crazy hand speed, it took 2 hours and less than half a day to submit tens of thousands of lines of code.

Guess you like

Origin blog.csdn.net/x763795151/article/details/108349532