mybatis generator配置和呼叫方法
阿新 • • 發佈:2019-01-07
1.下面是配置檔案,需要放到/workspace/xxx_project 資料夾下
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- 基礎包 只用一個 --> <!-- mysql --> <classPathEntry location="D:\work\mybatisGenerator\mybatisGenerator\lib\mysql-connector-java-5.1.16-bin.jar" /> <!-- oracle <classPathEntry location="x.jar" />--> <!-- sqlserver <classPathEntry location="x.jar" />--> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 分頁外掛 只用一個 --> <!-- mysql --> <plugin type="MysqlPaginationPlugin"></plugin> <!-- oracle <plugin type="OraclePaginationPlugin"></plugin>--> <!-- sqlserver <plugin type="SqlServicePaginationPlugin"></plugin>--> <!-- 通用 --> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" /> <!--<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"> <property name="searchString" value="Example$"/> <property name="replaceString" value="Criteria"/> </plugin> --> <commentGenerator> <!-- 不生成註釋 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 資料庫連線 只用一個 --> <!-- mysql --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://x.x.x.x:3306/itss_temp" userId="root" password="root"> </jdbcConnection> <!-- oracle <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@x.x.x.x:1521:orcl" userId="opm" password="opm"> </jdbcConnection>--> <!-- sqlserver <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://x.x.x.x:1433;DatabaseName=vsp" userId="sa" password="sa"> <jdbcConnection> --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.x.x.publish.table.entity" targetProject="src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.x.x.publish.table.mapper" targetProject="src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.x.x.publish.table.mapper" targetProject="src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="publish_checkitem" domainObjectName="PublishCheckItem"></table> <!-- <table tableName="publish" domainObjectName="Publish"></table> --> <!-- <table tableName="room" domainObjectName="Room"></table> --> <!-- <table tableName="user" domainObjectName="User"></table> --> </context> </generatorConfiguration>
2.下面是呼叫方法
3.配置檔案中使用到的一些外掛可以從網上搜到,最主要的是分頁外掛,下面提供了一個分頁外掛:(注意!外掛要放在src的default package當中!)package test; import java.io.File; 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.internal.DefaultShellCallback; public class GeneratorSqlmap { public static void main(String[] args) throws Exception { try { GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); generatorSqlmap.generator(); System.out.println("complete!"); } catch (Exception e) { e.printStackTrace(); } } public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("mybatis-generator_itss.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } }
import java.util.List; import org.mybatis.generator.api.CommentGenerator; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.ShellRunner; import org.mybatis.generator.api.dom.java.Field; import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; import org.mybatis.generator.api.dom.java.JavaVisibility; import org.mybatis.generator.api.dom.java.Method; import org.mybatis.generator.api.dom.java.Parameter; import org.mybatis.generator.api.dom.java.TopLevelClass; import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.XmlElement; /** * <pre> * mysql分頁外掛 */ public class MysqlPaginationPlugin extends PluginAdapter { @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { // add field, getter, setter for limit clause addLimit(topLevelClass, introspectedTable, "limitStart"); addLimit(topLevelClass, introspectedTable, "limitEnd"); return super.modelExampleClassGenerated(topLevelClass, introspectedTable); } @Override public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated( XmlElement element, IntrospectedTable introspectedTable) { XmlElement isParameterPresenteElemen = (XmlElement) element.getElements().get(element.getElements().size() - 1); XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$ isNotNullElement.addAttribute(new Attribute("test", "limitStart > -1")); isNotNullElement.addElement(new TextElement("limit ${limitStart} , ${limitEnd}")); isParameterPresenteElemen.addElement(isNotNullElement); return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable); } private void addLimit(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) { CommentGenerator commentGenerator = context.getCommentGenerator(); Field field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); field.setType(FullyQualifiedJavaType.getIntInstance()); field.setName(name); field.setInitializationString("-1"); commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field); char c = name.charAt(0); String camel = Character.toUpperCase(c) + name.substring(1); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("set" + camel); method.addParameter(new Parameter(FullyQualifiedJavaType .getIntInstance(), name)); method.addBodyLine("this." + name + "=" + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getIntInstance()); method.setName("get" + camel); method.addBodyLine("return " + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); } /** * This plugin is always valid - no properties are required */ public boolean validate(List<String> warnings) { return true; } public static void generate() { String config = MysqlPaginationPlugin.class.getClassLoader().getResource( "mybatisConfig.xml").getFile(); String[] arg = { "-configfile", config, "-overwrite" }; ShellRunner.main(arg); } public static void main(String[] args) { generate(); } }