perl连接mysql或者oracle自动生成java实体类
程序员文章站
2022-06-16 08:46:28
...
前言
当数据库一个表字段多达几十个,甚至百来个时,手动创建java实体类太没有效率,此demo只是简单的生成类属性语句,有兴趣的可以继续研究,字段类型自动匹配生成,及生成getset方法
前提
1.perl自行安装(一定要安装dbi模块)
参考链接
perl官网
perl:DBI模块安装与卸载
windows 下使用make命令,编译代码
2.示例代码(代码有冗余,可自行去除)
#!usr/bin/perl
use strict;
use DBI;
my @ref;
my $urlconnect;
my $num;
my $dbh;
my $a="json";
my $b="private String ";
my $c=";";
my $d="st.set";
my $pro="//类名需要修改;具体字段类型视数据库实际类型而定;getset方法需手动生成!";
my $top="public class ";
my $zuo="{";
my $you="}";
my $zhushi="/**注释*/";
#创建文件夹D:\perlExam
mkdir "D:\perlExam";
#手动输入连接数据库
&condata();
sub condata{
print "请输入数据库来源(mysql或者oracle)\n";
chomp(my $sourcename=<STDIN>);
print "请输入数据库ip地址\n";
chomp(my $ipname=<STDIN>);
print "请输入数据库端口号(mysql默认为3306,oracle默认为1521)\n";
chomp(my $portname=<STDIN>);
print "请输入数据库名\n";
chomp(my $database=<STDIN>);
print"输入用户名\n";
chomp(my $username=<STDIN>);
print"输入密码\n";
chomp(my $passwd=<STDIN>);
print "\nloading......\n\n";
&contest($database,$username,$passwd);
sub contest{
my ($database,$username,$passwd)=@_;#接收变量
#$database="dbi:Oracle:$database";
if (lc($sourcename) eq lc("mysql")) {
$urlconnect="dbi:mysql:database=$database;host=$ipname:$portname";
} else {
$urlconnect="dbi:Oracle:database=$database;host=$ipname:$portname";
}
#连接数据库
if(($dbh=DBI->connect($urlconnect,$username,$passwd)) ne ""){
print "数据库连接成功\n\n";
&imp();
sub imp{
print "\n生成java实例\n\n";
# print "▲请输入需要操作的选项,例如:a \n";
# chomp(my $choose=<STDIN>);
my $choose = "a";
print"请输入表名!\n";
chomp(my $insert_table=<STDIN>);
#取得的所有字段字母全部变为小写字母
$choose=~ tr/[A-Z]/[a-z]/;
&main($insert_table,$choose);
sub main{
my ($table,$choose)=@_;#接收变量
#连接数据库
#my $dbh=DBI->connect("dbi:Oracle:orcl","scott","tiger");
#查看有几个字段
my $count=0;
if (lc($sourcename) eq lc("mysql")) {
$count=$dbh->prepare("select count(*) as count from information_schema.COLUMNS where TABLE_SCHEMA='$database' and table_name='$table' ");
} else {
$count=$dbh->prepare("SELECT count(*) FROM USER_TAB_COLUMNS WHERE TABLE_NAME=UPPER('$table') ");
}
#语句查询对象
my $sth="";
if (lc($sourcename) eq lc("mysql")) {
$sth=$dbh->prepare("select t.column_name from information_schema.COLUMNS t where TABLE_SCHEMA='$database' and table_name='$table'");
} else {
$sth=$dbh->prepare("select t.column_name from user_col_comments t where t.table_name =UPPER( '$table')");
}
#执行SQL语句
$sth->execute;#查询用的execute
$count->execute;
#$num=$count->fetchrow_array;#几个字段
#再将每个字段的首字母变成大写
$table=~ s/(\w+)/\u$1/;
#查询的每一行数据给数组
#FILE,">>d:\\perlExam\\" >>表示不覆盖$table.txt的数据 FILE,">d:\\perlExam\\" >表示覆盖$table.txt的数据
open(FILE,">d:\\perlExam\\$table.txt");#将数据写进文件 文件存放路径:d:\\perlExam\\$table.txt
print FILE ("\n$pro\n\n$top$table$zuo\n\n");
while(@ref=$sth->fetchrow_array){
@ref[0]=~tr/[A-Z]/[a-z]/;
#每打印一行注释
print FILE ("$zhushi\n");
#打印java生成的实例属性;
print FILE ("aaa@qq.com$c\n");
}
print FILE ("\n$you\n\n");
print "\n文件已创建成功!\n路径为D:\n文件夹名为:perlExam\n文件名为:$table.txt\n";
return &end();
sub end{
my ($table,$choose)=@_;#接收变量
close(FILE);
print "\n是否退出文件创建?\n1.输入1按回车键继续创建实例\n2.按其他键退出\n\n";
chomp(my $cont=<STDIN>);
if($cont eq "1"){
&imp($choose);
}else{
$sth->finish;
$count->finish;
}
}
}
}
}
else{
print "\n输入有误,请重新输入!\n\n";
&condata();
}
$dbh->disconnect;
}
}
#Perl字符串比较操作符
#操作符描述
#lt小于
#gt大于
#eq等于
#le小于等于
#ge大于等于
#ne不等于
#cmp比较,返回1,0,or-1