zoukankan      html  css  js  c++  java
  • mybatis- generator自动生成代码

    一、mybatis- generator简介

    mybatis- generator是mybatis的一款插件,主要用于根据数据库表快速生成对应的domain(即javabean)、mapper接口以及对应的mapper.xml映射文件,非常的方便,可以减少手动编写javabean、mapper接口以及mapper映射文件工作量,非常的高效。

    二、mybatis- generator使用

    1、config.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>
    	<!--连接mysql的jar包-->
    	<classPathEntry
    			location="D:ProgramRelateProgramCodemybatis-generator-usagelibmysql-connector-java-5.1.6-bin.jar" />
    
    	<context id="MySQLTables" targetRuntime="MyBatis3">
    		<!--mybatis的一些插件-->
    		<!--<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
    			<property name="searchString" value="Example$" />
    			<property name="replaceString" value="Criteria" />
    		</plugin>-->
    <!--		<plugin type="com.lz.cts.plugin.MySQLPaginationPlugin"></plugin>-->
    <!--		<plugin type="com.lz.cts.plugin.RenameExampleMethodPlugin"></plugin>-->
    <!--		<plugin type="com.lz.cts.plugin.ModelFieldCustomizePlugin"></plugin>-->
    <!--		生成的domain实现了序列化-->
    		<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
    <!--		生成toString()方法-->
    	    <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
    
    
    		<!-- 是否去除自动生成的注释,true:是,false:否 -->
    		<commentGenerator>
    			<property name="suppressDate" value="true" />
    			<property name="suppressAllComments" value="true" />
    		</commentGenerator>
    
    		<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
    						connectionURL="jdbc:mysql://rm-wz92h1ljvevri4t2y.mysql.rds.aliyuncs.com/ncr"
    			            userId="root" password="Wen18948279186">
    		<!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
    			connectionURL="jdbc:oracle:thin:@10.75.10.17:1521:ORCL"
    			userId="ctsapp" password="ctsapp"> -->
    			<property name="remarks" value="true" />
    		</jdbcConnection>
    
    		<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,
                    为 true时把JDBC DECIMAL 和NUMERIC 类型解析为java.math.BigDecimal -->
    		<javaTypeResolver>
    			<property name="forceBigDecimals" value="false" />
    		</javaTypeResolver>
    
    
    		<!-- 指定javaBean生成的位置
                targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制;
                targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录
             -->
    		<javaModelGenerator targetPackage="com.lz.cts.domain" targetProject="src/main/java">
    			<property name="enableSubPackages" value="true" />
    			<!-- <property name="trimStrings" value="true" /> -->
    			<property name="rootClass" value="com.lz.cts.support.BaseEntity" />
    		</javaModelGenerator>
    
    		<!--  指定mapper映射文件生成的位置,mapper包会自己给你创建
               targetPackage、targetProject同javaModelGenerator中作用一样-->
    		<sqlMapGenerator targetPackage="mapper" targetProject="src/main/java/com/lz/cts">
    			<property name="enableSubPackages" value="true" />
    		</sqlMapGenerator>
    
    		<!-- 指定mapper接口生成的位置
             targetPackage、targetProject同javaModelGenerator中作用一样
             -->
    		<javaClientGenerator type="XMLMAPPER" targetPackage="com.lz.cts.dao" targetProject="src/main/java">
    			<property name="enableSubPackages" value="true" />
    		</javaClientGenerator>
    
    		<!-- 指定数据库表
               domainObjectName:生成的domain类的名字,当表名和domain类的名字有差异时一定要设置,如果不设置,直接使用表名作为domain类的名字;
               可以设置为somepck.domainName,那么会自动把domainName类再放到somepck包里面;-->
    		<table tableName="cls_order_info" />
    		<table tableName="sys_menu" />
    
    	</context>
    </generatorConfiguration>
    

    2、运行程序生成对应的各层代码

    package com.lz.cts;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.mybatis.generator.api.MyBatisGenerator;
    import org.mybatis.generator.api.VerboseProgressCallback;
    import org.mybatis.generator.config.Configuration;
    import org.mybatis.generator.config.xml.ConfigurationParser;
    import org.mybatis.generator.internal.DefaultShellCallback;
    
    public class MybatisGeneratorUtil {
    
    	public static void main(String[] args) {
    		try {
    			deletefile("E:/jaosn");
    			System.out.println("start generator ...");
    			List<String> warnings = new ArrayList<>();
    			boolean overwrite = true;
    			File configFile = new File(MybatisGeneratorUtil.class.getResource("../../../config_mysql.xml").getFile());
    			ConfigurationParser cp = new ConfigurationParser(warnings);
    			Configuration config = cp.parseConfiguration(configFile);
    			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    			myBatisGenerator.generate(new VerboseProgressCallback());
    			System.out.println("end generator!");
    			System.exit(0);			
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		} 
    	}
    
    	/**
    	 * * 删除某个文件夹下的所有文件夹和文件 * @param delpath String * @throws
    	 * FileNotFoundException * @throws IOException * @return boolean
    	 */
    	public static boolean deletefile(String delpath) throws FileNotFoundException, IOException {
    		try {
    			File file = new File(delpath);
    			if (!file.isDirectory()) {
    				file.delete();
    			} else if (file.isDirectory()) {				
    				String[] filelist = file.list();
    				for (int i = 0; i < filelist.length; i++) {
    					File delfile = new File(delpath + "\" + filelist[i]);
    					if (!delfile.isDirectory()) {					
    						delfile.delete();						
    					} else if (delfile.isDirectory()) {
    						deletefile(delpath + "\" + filelist[i]);
    					}
    				}
    				//file.delete();
    			}
    		} catch (FileNotFoundException e) {
    			System.out.println("deletefile() Exception:" + e.getMessage());
    		}
    		return true;
    	}
    
    }
    

    三、推荐mybatis好用的三款插件

    1、mybatis- generator 自动生成对应的javabean、mapper接口以及mapper映射文件。 2、mybatisplugin 可以快速在mapper接口与mpper映射文件之间定位。 3、mybatis-pagehelper 分页插件,这个没用过,但是最好不用。 这三大插件的使用和安装参见博文: https://blog.csdn.net/Scor1005/article/details/94454377

    参考博文: (1) https://blog.csdn.net/AOBO516/article/details/90245203

  • 相关阅读:
    vs2005 水晶报表横向打印Bug
    petshop4.0 详解之七(PetShop表示层设计)
    petshop4.0 详解之八(PetShop表示层设计)
    在VS2005中使用VSS2005
    用DataFormatString格式化GridView
    GridView的高级用法
    水晶报表 打印时出现错误提示:出现通信错误。将停止打印
    POJ1182 食物链[并查集]
    并查集的基础知识
    HDOJ1269 迷宫城堡[强连通分量]
  • 原文地址:https://www.cnblogs.com/jasonboren/p/13296289.html
Copyright © 2011-2022 走看看