1. 程式人生 > >【mybatis】mybaits generator 逆向工程的使用

【mybatis】mybaits generator 逆向工程的使用

alibaba gin resolve PE div sed pen sss bsp

mybatis逆向工程官方網站:http://www.mybatis.org/generator/quickstart.html

  • 準備xml文件。如下generator.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> <properties resource="db.properties"/> <context id="mbg" targetRuntime="MyBatis3"> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.user}
" password="${jdbc.password}"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.lw.entity" targetProject="src/main/java"> <property name="
enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.lw.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lw.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="tk_dg_coupon" domainObjectName="TkDgCoupon" > </table> </context> </generatorConfiguration>
  • 上面generator.xml中引入的資源文件db.properties如下:

######mysql#####
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/common
jdbc.user=root
jdbc.passsord=123456
  • pom文件配置如下,重點註意build標簽的配置:

<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.0</modelVersion>
  <groupId>com.lw</groupId>
  <artifactId>common</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <!-- 版本信息 -->
  <properties>
      <spring.version>4.3.16.RELEASE</spring.version>
      <druid.version>1.1.9</druid.version>
      <junit.version>4.12</junit.version>
      <mysql.version>5.1.6</mysql.version>
      <mybatis.version>3.4.1</mybatis.version>
      <mybatis.spring.version>1.3.1</mybatis.spring.version>
  </properties>
  <dependencies>
      <!-- spring相關依賴 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.0</version>
        <scope>provided</scope>
    </dependency>
    
    <!-- ali數據源 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>${druid.version}</version>
    </dependency>
      
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis.spring.version}</version>
    </dependency>
        
  </dependencies>
  
  <build>
      <plugins>
        <plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.5</version>
          <dependencies>
              <!-- mysql的依賴,此處必須放一邊,不然執行maven命令時,會提示找不到com.mysql.jdbc.Driver錯誤 -->
              <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
          </dependencies>
          <configuration>
              <!-- xml文件的位置 -->
              <configurationFile>${basedir}/src/main/resources/mybatis/generator.xml</configurationFile>
          </configuration>
        </plugin>
      </plugins>
  </build> 
</project>
  • 右鍵項目執行run as---->Maven build ---->寫入命令mybatis-generator:generate 即可

技術分享圖片

Maven構建成功

技術分享圖片


可能出現的錯誤提示: Access denied for user ‘root‘@‘localhost‘ 請參看https://www.cnblogs.com/oldwei/p/9108713.html

【mybatis】mybaits generator 逆向工程的使用