Snhk

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  1. 使用逆向工程生成mapperpojo

2. 新建一个项目,随便叫什么

3.导入mybatis-generator-core mybatismybatis-springlog4jjar

 4.在与src目录同级别下生成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="MybatisGenerator" targetRuntime="MyBatis3" >
          <commentGenerator>
          <!-- 是否去除自动生成的注解,true是,false否 -->
              <property name="supressAllComments" value="true"/>
          </commentGenerator>
          <!-- 数据库连接信息: -->
          <jdbcConnection driverClass="com.mysql.jdbc.Driver" 
          connectionURL="jdbc:mysql://localhost:3306/egou" userId="root" password="123456" ></jdbcConnection>
          <!-- 默认值false,把JDBC DECIMAL 和NUMBER类型解析为Integer,为true时 解析为 java.math.BigDecimal -->
          <javaTypeResolver>
              <property name="forceBigDecimals" value="false"/>
          </javaTypeResolver>
          <!-- 生成的po实体类的存放位置 -->
          <javaModelGenerator targetPackage="com.egou.pojo" targetProject=".\src">
              <!-- enableSubPackages:是否让schema作为包的后缀 -->
              <property name="enableSubPackages" value="false"/>
              <!-- 从数据库返回的值为清理前后的空格 -->
              <property name="trimStrings" value="true"/>
          </javaModelGenerator>
          <!-- 生成的mapper映射文件(XML)的存放位置 -->
          <sqlMapGenerator targetPackage="com.egou.mapper" targetProject=".\src">
              <property name="enableSubPackages" value="false"/>
          </sqlMapGenerator>
          <!-- 生成的mapper接口(JAVA文件)的存放位置 -->
          <javaClientGenerator targetPackage="com.egou.mapper" type="XMLMAPPER" targetProject=".\src">
              <property name="enableSubPackages" value="false"/>
          </javaClientGenerator>
          <!-- 指定数据库表 根据表名,有多个表就写多条数据 -->
          <table tableName="tb_content"></table>
          <table tableName="tb_content_category"></table>
          <table tableName="tb_item"></table>
          <table tableName="tb_item_cat"></table>
          <table tableName="tb_item-desc"></table>
          <table tableName="tb_item_param"></table>
          <table tableName="tb_item_param_item"></table>
          <table tableName="tb_order"></table>
          <table tableName="tb_order_item"></table>
          <table tableName="tb_order_shipping"></table>
          <table tableName="tb_user"></table>
      </context>
  </generatorConfiguration>

 

 

 

 

5.新建一个类,随意叫什么

 1 package generatorSqlmapCustom;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.ArrayList;
 6 import java.util.List;
 7 
 8 import org.mybatis.generator.api.MyBatisGenerator;
 9 import org.mybatis.generator.config.Configuration;
10 import org.mybatis.generator.config.xml.ConfigurationParser;
11 import org.mybatis.generator.exception.XMLParserException;
12 import org.mybatis.generator.internal.DefaultShellCallback;
13 
14 
15 
16 public class GeneratorSqlmap {
17     public void generate() throws Exception {
18         List<String> warnings = new ArrayList<String>();
19         boolean overwrite =true;
20         File configFile = new File("GeneratorConfig.xml");
21         System.out.println("--------"+configFile.getAbsolutePath());
22         ConfigurationParser cp = new ConfigurationParser(warnings);
23         Configuration config = cp.parseConfiguration(configFile);
24         DefaultShellCallback callback = new DefaultShellCallback(overwrite);
25         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback,warnings);
26         myBatisGenerator.generate(null);
27     }
28     public static void main(String[] args) {
29         GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
30         try {
31             generatorSqlmap.generate();
32         } catch (Exception e) {
33             e.printStackTrace();
34         }
35     }
36 }

6.结果

  6.1控制台

   6.2 多出两个从mysql本地库中生成的包

 

posted on 2019-04-20 21:14  Snhk  阅读(1416)  评论(0编辑  收藏  举报