逆向生成mybatis实体类接口和映射文件

  1. 导入jar包
  2. 将Generator.xml配置文件放到项目名称下面
    1. <?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>
      	<!-- classPathEntry:数据库的JDBC驱动的jar包地址 -->
      	<!-- <classPathEntry location="mysql-connector-java-5.1.21.jar" /> -->
      	<context id="DB2Tables" targetRuntime="MyBatis3">
      		
      		<commentGenerator>
      			<!-- 是否去除自动生成的注释 true:是;false:否 -->
      			<property name="suppressAllComments" value="true" />
      		</commentGenerator>
      		
      			<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
      		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
      			connectionURL="jdbc:mysql://localhost:3306/smbms" userId="root"
      			password="123">
      		</jdbcConnection>
      
      		<!-- 是否将JDBC DECIMAL 和 NUMERIC 类型解析为 java.math.BigDecimal true:转换为BigDecimal;false:转换为Integer -->
      		<javaTypeResolver>
      			<property name="forceBigDecimals" value="false" />
      		</javaTypeResolver>
      
      		<!-- 自动生成模型层代码(实体类) targetPackage:实体类文件所在的包名 targetProject:实体类文件存放的物理路径 -->
      		<javaModelGenerator targetPackage="com.pojo"
      			targetProject="./src">
      
      			<!-- enableSubPackages:是否让schema(数据库用户名)作为包的后缀 -->
      			<property name="enableSubPackages" value="false" />
      
      			<!-- 从数据库返回的值被清理前后的空格 -->
      			<property name="trimStrings" value="true" />
      		</javaModelGenerator>
      
      		<!-- 自动生成实现业务接口的xml映射文件 enableSubPackages:是否需要将数据库用户名追加到包名最后 -->
      		<sqlMapGenerator targetPackage="com.dao"
      			targetProject="./src">
      			<property name="enableSubPackages" value="false" />
      		</sqlMapGenerator>
      
      		<!-- 自动生成业务接口代码 -->
      		<javaClientGenerator type="XMLMAPPER"
      			targetPackage="com.dao" targetProject="./src">
      			<property name="enableSubPackages" value="false" />
      		</javaClientGenerator>
      
      
      		<!-- tableName:用于自动生成代码的数据库表; domainObjectName:对应于数据库表的javaBean类名 schema:数据库用户名 
      			tableName:表名 domainObjectName:实体类名 -->
      		<table tableName="smbms_user" domainObjectName="User"
      			enableCountByExample="false" enableUpdateByExample="false"
      			enableDeleteByExample="false" enableSelectByExample="false"
      			selectByExampleQueryId="false"></table>
      			
      		<!-- <table schema="jack" tableName="grade" domainObjectName="Grade"
      			enableCountByExample="false" enableUpdateByExample="false"
      			enableDeleteByExample="false" enableSelectByExample="false"
      			selectByExampleQueryId="false"></table> -->
      	</context>
      
      </generatorConfiguration>
       
  3. 编写运行类
    1. package com.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 TestGenerator {
      	  public static void main(String[] args) throws Exception {
      		  List<String> warnings = new ArrayList<String>();
      		   boolean overwrite = true;
      		   File configFile = new File("generator.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);
      		   System.out.println("success");
      	}
      }	
      
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值