Mybatis Generator自动生成Mybatis相关代码
?
?
?
import java.util.List; import java.util.Properties;import org.apache.log4j.Logger;import org.apache.log4j.PropertyConfigurator;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.Element;import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.XmlElement; /** * <pre> * add pagination using mysql limit. * This class is only used in ibator code generator. * </pre> */ public class MapperPaginationPlugin extends PluginAdapter { private Logger log = Logger.getLogger(PaginationPlugin.class); public MapperPaginationPlugin(){ Properties properties = new Properties(); properties.setProperty("log4j.rootCategory", "debug, R"); properties.setProperty("log4j.appender.R", "org.apache.log4j.RollingFileAppender"); properties.setProperty("log4j.appender.R.File", "D:/mybatisGenerator.log");//the output file ,you can read from other config file properties.setProperty("log4j.appender.R.MaxFileSize", "1024000");//???K ,??????? properties.setProperty("log4j.appender.R.MaxBackupIndex", "2"); properties.setProperty("log4j.appender.R.layout", "org.apache.log4j.PatternLayout"); properties.setProperty("log4j.appender.R.layout.ConversionPattern", "%m%n"); PropertyConfigurator.configure( properties ); } @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { // add field, getter, setter for limit clause addLimit(topLevelClass, introspectedTable, "limitStart"); addLimit(topLevelClass, introspectedTable, "limitMaxSize"); return super.modelExampleClassGenerated(topLevelClass, introspectedTable); } private String getBlank(int deep){ String s=""; for(int i=0; i<deep; i++){ s+=" "; } return s; } private void printElement(Element element,int deep){ if(element instanceof XmlElement){ XmlElement element2 = (XmlElement)element; log.info(getBlank(deep)+"Element Name:"+element2.getName()); deep += 1; for(Element c:element2.getElements()){ printElement(c,deep); } deep -=1; }else{ log.info(getBlank(deep)+"Element Type:"+element.getClass().getSimpleName()); } } @Override public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated( XmlElement element, IntrospectedTable introspectedTable) { XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$ isNotNullElement.addAttribute(new Attribute("test", "limitStart >= 0")); //$NON-NLS-1$ //$NON-NLS-2$ isNotNullElement.addElement(new TextElement("limit ${limitStart} , ${limitMaxSize}")); element.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 = PaginationPlugin.class.getClassLoader().getResource( "mybatisConfig.xml").getFile(); String[] arg = { "-configfile", config, "-overwrite" }; ShellRunner.main(arg); } public static void main(String[] args) { generate(); } }
?
?
?
import java.util.List;import org.mybatis.generator.api.PluginAdapter;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> * add pagination using mysql limit. * This class is only used in ibator code generator. * </pre> */public class PaginationPlugin 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("isGreaterEqual"); //$NON-NLS-1$ isNotNullElement.addAttribute(new Attribute("property", "limitStart")); //$NON-NLS-1$ //$NON-NLS-2$ isNotNullElement.addAttribute(new Attribute("compareValue", "0")); //$NON-NLS-1$ //$NON-NLS-2$ 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 = PaginationPlugin.class.getClassLoader() .getResource("Yododo.xml").getFile(); String[] arg = { "-configfile", config, "-overwrite" }; ShellRunner.main(arg); } public static void main(String[] args) { generate(); }}
?
?
?
targetProject=mybatis-generatorByMaven#targetProject=Yododomodel.package=com.yododo.core.domainsqlMap.package=sqlmap.mybatisdao.package=com.yododo.core.dao.mybatisjdbc.jar=C:/Users/Administrator/.m2/repository/mysql/mysql-connector-java/5.1.18/mysql-connector-java-5.1.18.jarjdbc.driver.class=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://192.168.1.5\:3306/yddtest?useUnicode\=true&characterEncoding\=UTF-8jdbc.user=yddjdbc.password=MySqL4ydd?
?
?
?
<?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><properties resource="mybatis.properties" /><classPathEntrylocation="D:\Projects\tour\lib\mysql-connector-java-5.1.18-bin.jar" /> <!-- MyBatis3 Ibatis2Java5 --><context id="context1" targetRuntime="MyBatis3"><plugin type="MapperPaginationPlugin" /><commentGenerator><property name="suppressAllComments" value="true" /></commentGenerator><jdbcConnection driveruserId="${jdbc.user}" password="${jdbc.password}" /><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><javaModelGenerator targetPackage="${model.package}"targetProject="${targetProject}"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><sqlMapGenerator targetPackage="${sqlMap.package}"targetProject="${targetProject}"><property name="enableSubPackages" value="true" /></sqlMapGenerator> <!-- SPRING XMLMAPPER --><javaClientGenerator type="XMLMAPPER" targetPackage="${dao.package}"targetProject="${targetProject}"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- <table tableName="tour_place_range"> <generatedKey column="place_range_id" identity="true" sqlStatement="SELECT LAST_INSERT_ID()"></generatedKey> </table> --><table tableName="sale_hotel"> <columnOverride column="status" javaType="Integer" jdbcType="TINYINT"></columnOverride> <columnOverride column="securepay" javaType="Integer" jdbcType="TINYINT"></columnOverride> <columnOverride column="areaorderid" javaType="Integer" jdbcType="TINYINT"></columnOverride> <columnOverride column="pricefrom" javaType="Integer" jdbcType="SMALLINT"></columnOverride> <columnOverride column="priceto" javaType="Integer" jdbcType="SMALLINT"></columnOverride></table></context> </generatorConfiguration>?
?
MyEclipse下右击Yododo.xml点Generate Mybatis/iBATIS Artifacts 运行生成