java逆向工程删改查_mybatis反向生成sql,基本的增删改查

packagecom.timestech.wsgk.test.tools;import staticorg.mybatis.generator.internal.util.ClassloaderUtility.getCustomClassloader;import staticorg.mybatis.generator.internal.util.messages.Messages.getString;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStreamWriter;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.HashSet;importjava.util.List;importjava.util.Set;importorg.mybatis.generator.api.GeneratedJavaFile;importorg.mybatis.generator.api.GeneratedXmlFile;importorg.mybatis.generator.api.ProgressCallback;importorg.mybatis.generator.api.ShellCallback;importorg.mybatis.generator.config.Configuration;importorg.mybatis.generator.config.Context;importorg.mybatis.generator.config.MergeConstants;importorg.mybatis.generator.exception.InvalidConfigurationException;importorg.mybatis.generator.exception.ShellException;importorg.mybatis.generator.internal.DefaultShellCallback;importorg.mybatis.generator.internal.NullProgressCallback;importorg.mybatis.generator.internal.ObjectFactory;importorg.mybatis.generator.internal.XmlFileMergerJaxp;public classMyBatisGeneratorProxy {privateConfiguration configuration;privateShellCallback shellCallback;private ListgeneratedJavaFiles;private ListgeneratedXmlFiles;private Listwarnings;private Setprojects;/*** Constructs a MyBatisGenerator object.

*

*@paramconfiguration

* The configuration for this invocation

*@paramshellCallback

* an instance of a ShellCallback interface. You may specify

* null in which case the DefaultShellCallback will

* be used.

*@paramwarnings

* Any warnings generated during execution will be added to this

* list. Warnings do not affect the running of the tool, but they

* may affect the results. A typical warning is an unsupported

* data type. In that case, the column will be ignored and

* generation will continue. You may specify null if

* you do not want warnings returned.

*@throwsInvalidConfigurationException

* if the specified configuration is invalid*/

publicMyBatisGeneratorProxy(Configuration configuration, ShellCallback shellCallback,

List warnings) throwsInvalidConfigurationException {super();if (configuration == null) {throw new IllegalArgumentException(getString("RuntimeError.2")); //$NON-NLS-1$

} else{this.configuration =configuration;

}if (shellCallback == null) {this.shellCallback = new DefaultShellCallback(false);

}else{this.shellCallback =shellCallback;

}if (warnings == null) {this.warnings = new ArrayList();

}else{this.warnings =warnings;

}

generatedJavaFiles= new ArrayList();

generatedXmlFiles= new ArrayList();

projects= new HashSet();this.configuration.validate();

}/*** This is the main method for generating code. This method is long running,

* but progress can be provided and the method can be canceled through the

* ProgressCallback interface. This version of the method runs all

* configured contexts.

*

*@paramcallback

* an instance of the ProgressCallback interface, or

* null if you do not require progress information

*@throwsSQLException

*@throwsIOException

*@throwsInterruptedException

* if the method is canceled through the ProgressCallback*/

public void generate(ProgressCallback callback) throwsSQLException,

IOException, InterruptedException {

generate(callback,null, null);

}/*** This is the main method for generating code. This method is long running,

* but progress can be provided and the method can be canceled through the

* ProgressCallback interface.

*

*@paramcallback

* an instance of the ProgressCallback interface, or

* null if you do not require progress information

*@paramcontextIds

* a set of Strings containing context ids to run. Only the

* contexts with an id specified in this list will be run. If the

* list is null or empty, than all contexts are run.

*@throwsInvalidConfigurationException

*@throwsSQLException

*@throwsIOException

*@throwsInterruptedException

* if the method is canceled through the ProgressCallback*/

public void generate(ProgressCallback callback, SetcontextIds)throwsSQLException, IOException, InterruptedException {

generate(callback, contextIds,null);

}/*** This is the main method for generating code. This method is long running,

* but progress can be provided and the method can be cancelled through the

* ProgressCallback interface.

*

*@paramcallback

* an instance of the ProgressCallback interface, or

* null if you do not require progress information

*@paramcontextIds

* a set of Strings containing context ids to run. Only the

* contexts with an id specified in this list will be run. If the

* list is null or empty, than all contexts are run.

*@paramfullyQualifiedTableNames

* a set of table names to generate. The elements of the set must

* be Strings that exactly match what's specified in the

* configuration. For example, if table name = "foo" and schema =

* "bar", then the fully qualified table name is "foo.bar". If

* the Set is null or empty, then all tables in the configuration

* will be used for code generation.

*@throwsInvalidConfigurationException

*@throwsSQLException

*@throwsIOException

*@throwsInterruptedException

* if the method is canceled through the ProgressCallback*/

public void generate(ProgressCallback callback, SetcontextIds,

Set fullyQualifiedTableNames) throwsSQLException,

IOException, InterruptedException {if (callback == null) {

callback= newNullProgressCallback();

}

generatedJavaFiles.clear();

generatedXmlFiles.clear();//calculate the contexts to run

ListcontextsToRun;if (contextIds == null || contextIds.size() == 0) {

contextsToRun=configuration.getContexts();

}else{

contextsToRun= new ArrayList();for(Context context : configuration.getContexts()) {if(contextIds.contains(context.getId())) {

contextsToRun.add(context);

}

}

}//setup custom classloader if required

if (configuration.getClassPathEntries().size() > 0) {

ClassLoader classLoader=getCustomClassloader(configuration.getClassPathEntries());

ObjectFactory.addExternalClassLoader(classLoader);

}//now run the introspections...

int totalSteps = 0;for(Context context : contextsToRun) {

totalSteps+=context.getIntrospectionSteps();

}

callback.introspectionStarted(totalSteps);for(Context context : contextsToRun) {

context.introspectTables(callback, warnings,

fullyQualifiedTableNames);

}//now run the generates

totalSteps = 0;for(Context context : contextsToRun) {

totalSteps+=context.getGenerationSteps();

}

callback.generationStarted(totalSteps);for(Context context : contextsToRun) {

context.generateFiles(callback, generatedJavaFiles,

generatedXmlFiles, warnings);

}//now save the files

callback.saveStarted(generatedXmlFiles.size()+generatedJavaFiles.size());for(GeneratedXmlFile gxf : generatedXmlFiles) {

projects.add(gxf.getTargetProject());

File targetFile;

String source;try{

File directory=shellCallback.getDirectory(gxf

.getTargetProject(), gxf.getTargetPackage());

targetFile= newFile(directory, gxf.getFileName());if(targetFile.exists()) {if(gxf.isMergeable()) {

source=XmlFileMergerJaxp.getMergedSource(gxf,

targetFile);

}else if(shellCallback.isOverwriteEnabled()) {

source=gxf.getFormattedContent();

warnings.add(getString("Warning.11", //$NON-NLS-1$

targetFile.getAbsolutePath()));

}else{

source=gxf.getFormattedContent();

targetFile=getUniqueFileName(directory, gxf

.getFileName());

warnings.add(getString("Warning.2", targetFile.getAbsolutePath())); //$NON-NLS-1$

}

}else{

source=gxf.getFormattedContent();

}

}catch(ShellException e) {

warnings.add(e.getMessage());continue;

}

callback.checkCancel();

callback.startTask(getString("Progress.15", targetFile.getName())); //$NON-NLS-1$

writeFile(targetFile, source, "UTF-8"); //$NON-NLS-1$

}for(GeneratedJavaFile gjf : generatedJavaFiles) {

projects.add(gjf.getTargetProject());

File targetFile;

String source;

String fileName;try{

fileName=gjf.getFileName();

File directory=shellCallback.getDirectory(gjf

.getTargetProject(), gjf.getTargetPackage());

targetFile= newFile(directory, fileName);if(targetFile.exists()) {if(shellCallback.isMergeSupported()) {

source=shellCallback.mergeJavaFile(gjf

.getFormattedContent(), targetFile

.getAbsolutePath(),

MergeConstants.OLD_ELEMENT_TAGS,

gjf.getFileEncoding());

}else if(shellCallback.isOverwriteEnabled()) {

source=gjf.getFormattedContent();

warnings.add(getString("Warning.11", //$NON-NLS-1$

targetFile.getAbsolutePath()));

}else{

source=gjf.getFormattedContent();

targetFile=getUniqueFileName(directory, fileName);

warnings.add(getString("Warning.2", targetFile.getAbsolutePath())); //$NON-NLS-1$

}

}else{

source=gjf.getFormattedContent();

}if(!fileName.equals(gjf.getFileName())){

source= source.replace("interface " + gjf.getFileName().substring(0,gjf.getFileName().indexOf(".")),"interface " + fileName.substring(0,fileName.indexOf(".")));

}

callback.checkCancel();

callback.startTask(getString("Progress.15", targetFile.getName())); //$NON-NLS-1$

writeFile(targetFile, source, gjf.getFileEncoding());

}catch(ShellException e) {

warnings.add(e.getMessage());

}

}for(String project : projects) {

shellCallback.refreshProject(project);

}

callback.done();

}/*** Writes, or overwrites, the contents of the specified file

*

*@paramfile

*@paramcontent*/

private void writeFile(File file, String content, String fileEncoding) throwsIOException {

FileOutputStream fos= new FileOutputStream(file, false);

OutputStreamWriter osw;if (fileEncoding == null) {

osw= newOutputStreamWriter(fos);

}else{

osw= newOutputStreamWriter(fos, fileEncoding);

}

BufferedWriter bw= newBufferedWriter(osw);

bw.write(content);

bw.close();

}privateFile getUniqueFileName(File directory, String fileName) {

File answer= null;//try up to 1000 times to generate a unique file name

StringBuilder sb = newStringBuilder();for (int i = 1; i < 1000; i++) {

sb.setLength(0);

sb.append(fileName);

sb.append('.');

sb.append(i);

File testFile= newFile(directory, sb.toString());if (!testFile.exists()) {

answer=testFile;break;

}

}if (answer == null) {throw newRuntimeException(getString("RuntimeError.3", directory.getAbsolutePath())); //$NON-NLS-1$

}returnanswer;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值