Mybatis 逆向工程 (MySQL和SQL server)

转自:

点击打开链接

1、添加 pom 依赖:


        
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generatorartifactId>
            <version>1.3.4version>
        dependency>

        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-maven-pluginartifactId>
            <version>1.3.4version>
        dependency>
        

2、添加配置:



<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            
            <property name="suppressAllComments" value="true"/>
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://rm-bp14b4ud3l5yoy3f6po.mysql.rds.aliyuncs.com:3306/zhongshu_prod_db" userId="root"
                        password="Liyi880301">
        jdbcConnection>
        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="true"/>
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="com.zhongshu.vegetables.bean"
                            targetProject=".\src">
            
            <property name="enableSubPackages" value="false"/>
            
            <property name="trimStrings" value="true"/>
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="com.zhongshu.vegetables.mapper"
                         targetProject=".\src">
            
            <property name="enableSubPackages" value="false"/>
        sqlMapGenerator>
        
        
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.zhongshu.vegetables.dao"
                             targetProject=".\src">
            
            <property name="enableSubPackages" value="false"/>
        javaClientGenerator>

        
        <table schema="" tableName="area">table>
        <table schema="" tableName="category">table>
        <table schema="" tableName="common">table>
        <table schema="" tableName="dictionary">table>
        <table schema="" tableName="item">table>
        <table schema="" tableName="menu">table>
        <table schema="" tableName="menu_role">table>
        <table schema="" tableName="user">table>
        <table schema="" tableName="order">table>
        <table schema="" tableName="order_detail">table>
        <table schema="" tableName="receive_address">table>
        <table schema="" tableName="role">table>
        <table schema="" tableName="shop_cart">table>
        <table schema="" tableName="unit">table>
        <table schema="" tableName="version">table>

    context>
generatorConfiguration>

3、执行代码启动

package com.zhongshu.vegetables.utils;

import java.io.File;
import java.io.IOException;
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.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorSqlmap {

    public void generator() throws Exception{

        List warnings = new ArrayList();
        boolean overwrite = true;

        //指定 逆向工程配置文件
        File configFile = new File("./src/main/resources/generatorConfig.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);

    } 
    public static void main(String[] args) throws Exception {
        try {
            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

4、脚本方式启动逆向工程

新建文件名 run_mysql.bat

内容:    java -jar lib/mybatis-generator-core-1.3.1-ext.jar -configfile generatorConfig_mysql.xml

Mybatis 逆向工程 (MySQL和SQL server)_第1张图片

双击运行即可 !

******************************************************华丽丽滴分界线*************************************

MySQL

只需配置generatorConfig.xml配置文件即可

    
    
<generatorConfiguration>  
    <properties resource="jdbc.properties">properties>    
    <classPathEntry location="D:\lion\LionTest\WebContent\WEB-INF\lib\sqljdbc4.jar" />    
    <context  id="context1">    
            
            
        <commentGenerator>    
            <property name="suppressDate" value="true"/>    

            <property name="suppressAllComments" value="true"/>    
        commentGenerator>    
            
        <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"    
                        connectionURL="jdbc:sqlserver://localhost:1433;DatabaseName=tvshow" userId="sa" password="sa" />    

            
        <javaTypeResolver>    
            <property name="forceBigDecimals" value="false"/>    
        javaTypeResolver>    

            
        <javaModelGenerator targetPackage="com.lion.pojo" targetProject="SqlTest" />    

            
        <sqlMapGenerator targetPackage="com.lion.mapper" targetProject="SqlTest" />    
            
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.lion.dao" targetProject="SqlTest">    
           <property name="enableSubPackages" value="true"/>    
        javaClientGenerator>    
            
            
        <table schema="" tableName="production" domainObjectName="Production" enableCountByExample="false" enableSelectByExample="false"    
               enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >    
        table>  
    context>    
generatorConfiguration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

MS SQL server

1.配置 generatorConfig.xml



<generatorConfiguration>
    <properties resource="jdbc.properties">properties>
    <classPathEntry location="${dbconfig.sqlServer.driverLocation}" />
    <context  id="context1">
        
        
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        commentGenerator>
        
        <jdbcConnection driverClass="${dbconfig.sqlServer.driverClasss}"
                        connectionURL="${dbconfig.sqlServer.ssmDemo.read.jdbcUrl}"
                        userId="${dbconfig.sqlServer.username}"
                        password="${dbconfig.sqlServer.password}" />

        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="com.lion.pojo"
                            targetProject="SqlTest" />

        
        <sqlMapGenerator targetPackage="com.lion.mapper" targetProject="SqlTest" />
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.lion.dao" targetProject="SqlTest"> 
        <property name="enableSubPackages" value="true"/>
        javaClientGenerator>

        
        
        <table schema="" 
               tableName="title" 
               domainObjectName="Title" 
               enableCountByExample="false" 
               enableSelectByExample="false"
               enableDeleteByExample="false" 
               enableUpdateByExample="false" 
               selectByExampleQueryId="false" >
        table>
    context>
generatorConfiguration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

2.配置jdbc.properties

dbconfig.sqlServer.driverClasss=com.microsoft.sqlserver.jdbc.SQLServerDriver
dbconfig.sqlServer.ssmDemo.read.jdbcUrl=jdbc:sqlserver://localhost:1433;DatabaseName=tvshow
dbconfig.sqlServer.username=sa
dbconfig.sqlServer.password=111
#定义初始连接数
#定义最大连接数
dbconfig.maxActive=20
#定义最大空闲
dbconfig.maxIdle=20
#定义最小空闲
dbconfig.minIdle=1
#定义最长等待时间
dbconfig.maxWait=60000
#sqljsbc的位置(和sqlserver版本有关)
dbconfig.sqlServer.driverLocation=D:\sqljdbc4.jar

你可能感兴趣的:(学而时习)