(IDEA版)用Maven优化MyBatis逆向工程

(IDEA版)用Maven优化MyBatis逆向工程

1、项目目录

项目是在Module下创建
(IDEA版)用Maven优化MyBatis逆向工程_第1张图片

2、在pom.xml配置插件


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>cn.cxg.ssmgroupId>
    <artifactId>SSM-mybatisartifactId>
    <version>1.0-SNAPSHOTversion>
    <build>
        <plugins>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.6version>
            plugin>
        plugins>
    build>
    <dependencies>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
<!—mybatis核心-->
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.3version>
        dependency>
<!—数据库-->
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.28version>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.6version>
        dependency>
    dependencies>
project>

3、generatorConfig.xml生成代码的配置文件



<generatorConfiguration>
   <context id="testTables" targetRuntime="MyBatis3">
      <commentGenerator>
         
         <property name="suppressAllComments" value="true" />
      commentGenerator>
      
      <jdbcConnection driverClass="com.mysql.jdbc.Driver"
         connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
         password="cxg200888">
      jdbcConnection>
      <!—Oracle数据库方法 <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
         connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" 
         userId="yycg"
         password="yycg">
      jdbcConnection> -->

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

      
      <javaModelGenerator targetPackage="cn.itcast.ssm.po"
         targetProject=".\src">
         
         <property name="enableSubPackages" value="false" />
         
         <property name="trimStrings" value="true" />
      javaModelGenerator>
        
      <sqlMapGenerator targetPackage="cn.itcast.ssm.mapper" 
         targetProject=".\src">
         
         <property name="enableSubPackages" value="false" />
      sqlMapGenerator>
      
      <javaClientGenerator type="XMLMAPPER"
         targetPackage="cn.itcast.ssm.mapper" 
         targetProject=".\src">
         
         <property name="enableSubPackages" value="false" />
      javaClientGenerator>
      
      <table tableName="items">table>
      <table tableName="orders">table>
      <table tableName="orderdetail">table>
      <table tableName="user">table>
      

      
   context>
generatorConfiguration>

4、mybatis官方提供的固定代码

固定代码

package com.ssm;
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;
/**
 * @author Administrator
 */
public class GeneratorSqlmap {
   public void generator() throws Exception{
      List warnings = new ArrayList();
      boolean overwrite = true;
      //指定 逆向工程配置文件:修改即可用
      File configFile = new File("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();
      }

   }

}

5、日志文件

Log4j.properties必须放在src/main/resources
(IDEA版)用Maven优化MyBatis逆向工程_第2张图片

6、运行

点击下图
(IDEA版)用Maven优化MyBatis逆向工程_第3张图片
(IDEA版)用Maven优化MyBatis逆向工程_第4张图片

7、注意

generatorConfig.xml不能放在resource必须放在SSM-mybatis目录下

你可能感兴趣的:(Mybatis)