本文主要是介绍mybatisgenerator在idea下 连接oracle 生成有xml文件 dao 和 中文注释的实体类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
mybatisgenerator 连接oracle 生成有xml文件 dao 和 中文注释的实体类
主要有下面四个步骤
第一步.pom文件添加jar包和插件
<dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.2</version></dependency><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.10</version></plugin><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.2</version><configuration><configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile><verbose>true</verbose><overwrite>true</overwrite></configuration><executions><execution><id>Generate MyBatis Artifacts</id><goals><goal>generate</goal></goals></execution></executions><dependencies><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.2</version></dependency></dependencies></plugin></plugins></build>
第二步:建立src/main/resources/mybatis-generator-config.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC"-//mybatis.org//DTD MyBatis Generator Configuration1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration><!-- 制定数据库的驱动包的路径 千万别放中文路径下 --><classPathEntry location="D:\IdeaWorkSpace2\sysadmin_v2\src\main\webapp\WEB-INF\lib\ojdbc14-10.2.0.4.0.jar" /><!-- 配置数据源和生成的代码所存放的位置 --><context id="context1"><!-- 指定生成的java文件的编码,没有直接生成到项目时中文可能会乱码 --><property name="javaFileEncoding" value="UTF-8"/><!-- 使用自定义的插件 --><commentGenerator type="com.xwtec.orienteventinfo.MyCommentGenerator"></commentGenerator><!--使用自定义插件--><!--<commentGenerator type="com.xiepanpan.ecps.utils.MyCommentGenerator"></commentGenerator>--><!--这里需要大佬们修改成自己的--><jdbcConnection driverClass="oracle.jdbc.OracleDriver"connectionURL="jdbc:oracle:thin:@127.0.0.1:80:SERVICE_NAME" userId="用户名"password="数据库密码" ><!-- 针对oracle数据库 之前实体类中文注释是问号 加上这个就好了 --><property name="remarksReporting" value="true"></property></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- dto class --><javaModelGenerator targetPackage="com.xiepanpan.ecps.model"targetProject="src/main/java"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!-- mybatis xml file --><sqlMapGenerator targetPackage="com.xiepanpan.ecps.mapper"targetProject="src/main/resources"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- mapper class --><javaClientGenerator type="XMLMAPPER"targetPackage="com.xiepanpan.ecps.dao" targetProject="src/main/java"><property name="enableSubPackages" value="true" /></javaClientGenerator><!--不生成帮助类(Exmaples) --><!-- enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"enableSelectByExample="false" selectByExampleQueryId="false" --><!--已生成的表 <table schema="demo" tableName="USER" domainObjectName="User"></table> --><table schema="" tableName="T_ORIENT_EVENTINFO"enableCountByExample="false" enableUpdateByExample="false"enableDeleteByExample="false" enableSelectByExample="false"selectByExampleQueryId="false"></table></context>
</generatorConfiguration>
第三步:建立com.xwtec.orienteventinfo.MyCommentGenerator.java文件
package com;/*** @author Administrator* @title: MyCommentGenerator1* @projectName sysadmin_v2* @description: TODO* @date 2019/4/319:28*/import static org.mybatis.generator.internal.util.StringUtility.isTrue;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.InnerEnum;
import org.mybatis.generator.api.dom.java.JavaElement;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.config.PropertyRegistry;/*** 描述:* @since 1.9.10* @version 1.9.10* @作者:niexiaohui* @创建时间:2016年11月22日* @修改记录:*/
public class MyCommentGenerator implements CommentGenerator{private Properties properties;private Properties systemPro;private boolean suppressDate;private boolean suppressAllComments;private String currentDateStr;public MyCommentGenerator() {super();properties = new Properties();systemPro = System.getProperties();suppressDate = false;suppressAllComments = false;currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());}public void addJavaFileComment(CompilationUnit compilationUnit) {// add no file level comments by defaultreturn;}/*** Adds a suitable comment to warn users that the element was generated, and* when it was generated.*/public void addComment(XmlElement xmlElement) {return;}public void addRootComment(XmlElement rootElement) {// add no document level comments by defaultreturn;}public void addConfigurationProperties(Properties properties) {this.properties.putAll(properties);suppressDate = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));suppressAllComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));}/*** This method adds the custom javadoc tag for. You may do nothing if you do* not wish to include the Javadoc tag - however, if you do not include the* Javadoc tag then the Java merge capability of the eclipse plugin will* break.** @param javaElement* the java element*/protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {javaElement.addJavaDocLine(" *");StringBuilder sb = new StringBuilder();sb.append(" * ");sb.append(MergeConstants.NEW_ELEMENT_TAG);if (markAsDoNotDelete) {sb.append(" do_not_delete_during_merge");}String s = getDateString();if (s != null) {sb.append(' ');sb.append(s);}javaElement.addJavaDocLine(sb.toString());}/*** This method returns a formated date string to include in the Javadoc tag* and XML comments. You may return null if you do not want the date in* these documentation elements.** @return a string representing the current timestamp, or null*/protected String getDateString() {String result = null;if (!suppressDate) {result = currentDateStr;}return result;}public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();innerClass.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());sb.append(" ");sb.append(getDateString());innerClass.addJavaDocLine(sb.toString().replace("\n", " "));innerClass.addJavaDocLine(" */");}public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();innerEnum.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());innerEnum.addJavaDocLine(sb.toString().replace("\n", " "));innerEnum.addJavaDocLine(" */");}public void addFieldComment(Field field, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();field.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedColumn.getRemarks());field.addJavaDocLine(sb.toString().replace("\n", " "));field.addJavaDocLine(" */");}public void addFieldComment(Field field, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();field.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());field.addJavaDocLine(sb.toString().replace("\n", " "));field.addJavaDocLine(" */");}public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}method.addJavaDocLine("/**");addJavadocTag(method, false);method.addJavaDocLine(" */");}public void addGetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}method.addJavaDocLine("/**");StringBuilder sb = new StringBuilder();sb.append(" * ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString().replace("\n", " "));sb.setLength(0);sb.append(" * @return ");sb.append(introspectedColumn.getActualColumnName());sb.append(" ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString().replace("\n", " "));method.addJavaDocLine(" */");}public void addSetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}method.addJavaDocLine("/**");StringBuilder sb = new StringBuilder();sb.append(" * ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString().replace("\n", " "));Parameter parm = method.getParameters().get(0);sb.setLength(0);sb.append(" * @param ");sb.append(parm.getName());sb.append(" ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString().replace("\n", " "));method.addJavaDocLine(" */");}public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();innerClass.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());innerClass.addJavaDocLine(sb.toString().replace("\n", " "));sb.setLength(0);sb.append(" * @author ");sb.append(systemPro.getProperty("user.name"));sb.append(" ");sb.append(currentDateStr);innerClass.addJavaDocLine(" */");}
}
第四步:再建立一个StartUp.java类
package com.xwtec;/*** @author Administrator* @title: StartUp* @projectName sysadmin_v2* @description: TODO* @date 2019/4/319:30*/import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;/*** 描述:* @since 1.9.10* @version 1.9.10* @作者:niexiaohui* @创建时间:2016年11月22日* @修改记录:*/
public class StartUp {public static void main(String[] args) throws URISyntaxException {try {List<String> warnings = new ArrayList<String>();boolean overwrite = true;ClassLoader classloader = Thread.currentThread().getContextClassLoader();InputStream is = classloader.getResourceAsStream("mybatis-generator-config.xml");ConfigurationParser cp = new ConfigurationParser(warnings);Configuration config = cp.parseConfiguration(is);DefaultShellCallback callback = new DefaultShellCallback(overwrite);MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);myBatisGenerator.generate(null);} catch (SQLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} catch (InvalidConfigurationException e) {e.printStackTrace();} catch (XMLParserException e) {e.printStackTrace();}}
}
效果:
到位!让我们善用工具 节省时间 提高效率 拒绝搬砖
另外:有的表没有主键 就会导致mybatis generator自动生成代码时 只生成了insert 而没有其他的
參考博客:https://www.cnblogs.com/wudixiaoguaishou/p/9988608.html
参考博客:https://www.cnblogs.com/mengzw/p/6655001.html
这篇关于mybatisgenerator在idea下 连接oracle 生成有xml文件 dao 和 中文注释的实体类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!