Mybatis generator生成工具简单介绍
Mybatis generator
其主要的功能就是方便,快捷的创建好 Dao,entry,xml 加快了开发速度,使用方面根据其提供的规则配置好就 OK
这里还有一个重要的开发场景,开发过程中,对数据库的操作肯定很多,比如新增字段什么的,你只要将原先自动生成的一套代码删除,重新再生成一份,这就完美解决了,但是这样做的前提是,你必须对生成后的代码不改动,只是使用。不需要想手动开发写代码那样到处改代码,还担心改漏地方。
其实这个的实现方式也是五花八门的,写一种比较常见的
主要流程
第一步:添加依赖
主要是 jdbc 和 generator
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6</version> </dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
第二步:配置 generatorConfig.xml
这个文件主要是对 从哪来,到哪去的描述,还有一些特殊要求的配置
<?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> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 生成 mysql 带有分页的 sql 的插件 这个可以自己写,--> <plugin type="generator.MysqlPaginationPlugin" /> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <!-- 自定义的注释规则,继承 DefaultCommentGenerator 重写 一些方法 --> <commentGenerator type="generator.NewbatisGenerator"> <!-- 是否去除自动生成日期的注释 true:是 : false: 否 --> <property name="suppressDate" value="true"/> <!-- 是否去除所有自动生成的注释 true:是 : false: 否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql:// 数据库地址" userId="username" password="password"> </jdbcConnection> <!--生成 entity 类存放位置--> <javaModelGenerator targetPackage="包名(com.generator.test.entity)" targetProject="项目地址到 \java (D:\workspace\src\main\java)"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="包名(com.generator.test.mapper)" targetProject="项目地址到 \java (D:\workspace\src\main\java)"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成 Dao 类存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="包名(com.generator.test.dao)" targetProject="项目地址到 \java (D:\workspace\src\main\java)"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="表名" domainObjectName="生成实体的类名"> </table> </context> </generatorConfiguration>
第三步:主要看你个人的需求,对注释,分页啥的有啥要求不,可以重写几个方法对其进行改造
主要举几个例子
对注释的修改 NewbatisGenerator
public class NewbatisGenerator extends DefaultCommentGenerator { private Properties properties; private Properties systemPro; private boolean suppressDate; private boolean suppressAllComments; private String currentDateStr;</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> NewbatisGenerator() { </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(); properties </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Properties(); systemPro </span>=<span style="color: rgba(0, 0, 0, 1)"> System.getProperties(); suppressDate </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; suppressAllComments </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; currentDateStr </span>= (<span style="color: rgba(0, 0, 255, 1)">new</span> SimpleDateFormat("yyyy-MM-dd")).format(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date()); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 对类的注解 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> topLevelClass * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> introspectedTable </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { topLevelClass.addJavaDocLine(</span>"/**"<span style="color: rgba(0, 0, 0, 1)">); topLevelClass.addJavaDocLine(</span>" * 这是MyBatis Generator自动生成的Model Class."<span style="color: rgba(0, 0, 0, 1)">); StringBuilder sb </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuilder(); sb.append(</span>" * 对应的数据表是 : "<span style="color: rgba(0, 0, 0, 1)">); sb.append(introspectedTable.getFullyQualifiedTable()); topLevelClass.addJavaDocLine(sb.toString()); String tableRemarks </span>=<span style="color: rgba(0, 0, 0, 1)"> introspectedTable.getRemarks(); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">StringUtils.isEmpty(tableRemarks)) { sb.setLength(</span>0<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>" * 数据表注释 : "<span style="color: rgba(0, 0, 0, 1)">); sb.append(tableRemarks); topLevelClass.addJavaDocLine(sb.toString()); } sb.setLength(</span>0<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>" * @author "<span style="color: rgba(0, 0, 0, 1)">); sb.append(systemPro.getProperty(</span>"user.name"<span style="color: rgba(0, 0, 0, 1)">)); topLevelClass.addJavaDocLine(sb.toString()); String curDateString </span>= (<span style="color: rgba(0, 0, 255, 1)">new</span> SimpleDateFormat("yyyy-MM-dd HH🇲🇲ss")).format(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date()); sb.setLength(</span>0<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>" * @date "<span style="color: rgba(0, 0, 0, 1)">); sb.append(curDateString); topLevelClass.addJavaDocLine(sb.toString()); topLevelClass.addJavaDocLine(</span>" */"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 生成的实体增加字段的中文注释 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (suppressAllComments) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">; } StringBuilder sb </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuilder(); field.addJavaDocLine(</span>"/**"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>" * "<span style="color: rgba(0, 0, 0, 1)">); sb.append(introspectedColumn.getRemarks()); field.addJavaDocLine(sb.toString().replace(</span>"\n", " "<span style="color: rgba(0, 0, 0, 1)">)); field.addJavaDocLine(</span>" */"<span style="color: rgba(0, 0, 0, 1)">); }
}
对分页的添加 MysqlPaginationPlugin 自动生成带分页插件
public class MysqlPaginationPlugin extends PluginAdapter { public MysqlPaginationPlugin(){}</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.addLimit(topLevelClass, introspectedTable, "limitStart"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">this</span>.addLimit(topLevelClass, introspectedTable, "limitSize"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.modelExampleClassGenerated(topLevelClass, introspectedTable); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { XmlElement isNotNullElement </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> XmlElement("if"<span style="color: rgba(0, 0, 0, 1)">); isNotNullElement .addAttribute(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Attribute("test", "limitStart != null and limitSize >= 0"<span style="color: rgba(0, 0, 0, 1)">)); isNotNullElement.addElement(</span><span style="color: rgba(0, 0, 255, 1)">new</span> TextElement("limit #{limitStart} , #{limitSize}"<span style="color: rgba(0, 0, 0, 1)">)); element.addElement(isNotNullElement); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.sqlMapSelectByExampleWithoutBLOBsElementGenerated(element, introspectedTable); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { XmlElement isNotNullElement </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> XmlElement("if"<span style="color: rgba(0, 0, 0, 1)">); isNotNullElement .addAttribute(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Attribute("test", "limitStart != null and limitSize >= 0"<span style="color: rgba(0, 0, 0, 1)">)); isNotNullElement.addElement(</span><span style="color: rgba(0, 0, 255, 1)">new</span> TextElement("limit #{limitStart} , #{limitSize}"<span style="color: rgba(0, 0, 0, 1)">)); element.addElement(isNotNullElement); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.sqlMapSelectByExampleWithBLOBsElementGenerated(element, introspectedTable); } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> addLimit(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) { CommentGenerator commentGenerator </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.context.getCommentGenerator(); Field field </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Field(); field.setVisibility(JavaVisibility.PROTECTED); field.setType(PrimitiveTypeWrapper.getIntegerInstance()); field.setName(name); commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field); </span><span style="color: rgba(0, 0, 255, 1)">char</span> c = name.charAt(0<span style="color: rgba(0, 0, 0, 1)">); String camel </span>= Character.toUpperCase(c) + name.substring(1<span style="color: rgba(0, 0, 0, 1)">); Method method </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName(</span>"set" +<span style="color: rgba(0, 0, 0, 1)"> camel); method.addParameter(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Parameter(PrimitiveTypeWrapper.getIntegerInstance(), name)); StringBuilder sb </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuilder(); sb.append(</span>"this."<span style="color: rgba(0, 0, 0, 1)">); sb.append(name); sb.append(</span>" = "<span style="color: rgba(0, 0, 0, 1)">); sb.append(name); sb.append(</span>";"<span style="color: rgba(0, 0, 0, 1)">); method.addBodyLine(sb.toString()); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); Method getterMethod </span>=<span style="color: rgba(0, 0, 0, 1)"> AbstractJavaGenerator.getGetter(field); commentGenerator.addGeneralMethodComment(getterMethod, introspectedTable); topLevelClass.addMethod(getterMethod); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> validate(List<String><span style="color: rgba(0, 0, 0, 1)"> warnings) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 生成mapper.xml,文件内容会被清空再写入 * </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) { sqlMap.setMergeable(</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.sqlMapGenerated(sqlMap, introspectedTable); }
}
最后一步:运行生成
这些官网都是有的
public class Generator {</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception{ List</span><String> warnings = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<String><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> overwrite = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; File configFile </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> File("generatorConfig.xml"<span style="color: rgba(0, 0, 0, 1)">); ConfigurationParser cp </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ConfigurationParser(warnings); Configuration config </span>=<span style="color: rgba(0, 0, 0, 1)"> cp.parseConfiguration(configFile); DefaultShellCallback callback </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(</span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">); }
}
这个比较简单明了,个性化也不错,值得推荐
转载请注明出处:https://www.cnblogs.com/zhouguanglin/p/11239583.html