自己写一个maven版mybatis****(main方法类做入口)

今天想自己搞一个mybatis****,搜了一下大都是用eclispe命令来执行的,后来找了下MyBatis Generator官网,写了一个用java的main方法作为入口执行的maven****。

1.首先,把官网链接放上来(各种写法可以在上边找到,比较详细,不过是纯英文;小伙伴们可以用谷歌浏览器自行翻译为中文查看,考配置的时候记得翻译回来哦,要不然会报错的)

              http://www.mybatis.org/generator/

2.用到工具:eclipse jdk1.8 maven

3.搭建一个简单的maven项目 new -> Maven Project:

自己写一个maven版mybatis****(main方法类做入口)

自己写一个maven版mybatis****(main方法类做入口)

在新的弹出窗口中填写一些信息:

自己写一个maven版mybatis****(main方法类做入口)

然后点击finish,完成一个maven项目的创建;

4.项目包结构进行搭建:

4.1建立自己存放mapper、model(pojo)类包(注:4.1可省略,相应的路径只需要后边的generatorConfig.xml中配置即可,为了小伙伴们便于理解,我才建了这些包和文件夹。mapper的java、xml,可以放在一起也可以分开,主要看你自己在generatorConfig.xml中的配置,为了演示我给分开了)

4.2新建****xml配置文件(我的叫generatorConfig.xml,位置和名字看个人的习惯,最后要用java代码去读他,保持一致就可以了

4.3建一个****项目入口main包及类(也即最后****执行入口):

自己写一个maven版mybatis****(main方法类做入口)

5.向****generatorConfig.xml填入配置信息(先图后配置,官网也有例子,或者根据我图片后放的配置信息个性化修改)

   自己写一个maven版mybatis****(main方法类做入口)

自己写一个maven版mybatis****(main方法类做入口)

自己写一个maven版mybatis****(main方法类做入口)

自己写一个maven版mybatis****(main方法类做入口)

上边是截图加注释,下边放上我自己的配置信息,供大家参考(看似很长,其实主要是注释,排版神马的不知道会不会乱...)

<?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>
  <!-- 驱动类位置,使用了maven依赖,不需要了 (注释掉的是官网db2数据库的一个例子,知道就可以了)-->
  <!-- <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" /> -->
  <!-- 数据库连接配置,可以放到 .properties文件中,用java main类读取,我失败了,写上做一个参考-->
  <!-- <properties resource="jdbc.properties"/>  -->
  <context id="mysqlTables" targetRuntime="MyBatis3" >
      <!-- 是否去除****里自动生成的注释 true:是 : false:否 -->
    <commentGenerator >
        <property name="suppressDate" value="true"/>
        <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!-- 数据库连接配置【根据各自实际情况填入,我的是mysql数据库】 -->
    <jdbcConnection 
        driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/mysql?useSSL=false"
        userId="root"
        password="">
    </jdbcConnection>
    <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
         NUMERIC 类型解析为java.math.BigDecimal -->
    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver> 
  
    <!-- java Model 类生成配置 targetProject:生成文件路径  targetPackage:路径下包 路径和包可以自行修改-->
    <javaModelGenerator targetPackage="model" targetProject="./src/main/java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!-- mapper.xml 生成配置 targetProject:生成文件路径  targetPackage:路径下包 路径和包可以自行修改-->
    <sqlMapGenerator targetPackage="mapper"  targetProject="./src/main/resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    <!-- mapper.java 生成配置  targetProject:生成文件路径  targetPackage:路径下包  路径和包可以自行修改-->
    <javaClientGenerator type="XMLMAPPER" targetPackage="mapper"  targetProject="./src/main/java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    
   <!-- 官方给的 table 例子 -->
   <!--  <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
      <property name="useActualColumnNames" value="true"/>
      <generatedKey column="ID" sqlStatement="DB2" identity="true" />
      <columnOverride column="DATE_FIELD" property="startDate" />
      <ignoreColumn column="FRED" />
      <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
    </table> -->
    
    
    <!--此****会默认生成example类和mapper中的ByExample查询方法
        table 将下列属性设置成false就不会自动生成example类和ByExample查询方法了,
        如果需要example类和mapper中的ByExample查询方法可以去掉这些配置
        
        enableCountByExample="false"
        enableUpdateByExample="false"
        enableDeleteByExample="false"
        enableSelectByExample="false"
        selectByExampleQueryId="false" 
        
        tableName属性后跟需****的库表名称     
        
     -->
    <table tableName="myuser" enableCountByExample="false"
        enableUpdateByExample="false"
        enableDeleteByExample="false"
        enableSelectByExample="false"
        selectByExampleQueryId="false">
    </table>
    <table tableName="role"  enableCountByExample="false"
        enableUpdateByExample="false"
        enableDeleteByExample="false"
        enableSelectByExample="false"
        selectByExampleQueryId="false"/>
    <table tableName="user_role" 
        enableCountByExample="false"
        enableUpdateByExample="false"
        enableDeleteByExample="false"
        enableSelectByExample="false"
        selectByExampleQueryId="false"/>
  </context>
</generatorConfiguration>

6.修改pom.xml文件,引入依赖

自己写一个maven版mybatis****(main方法类做入口)

主要就是要再引入三个依赖:​mybatis-generator-core、mysql-connector-java、ibatis-core

下面贴一下我的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.0</modelVersion>
    <groupId>com.mybatis</groupId>
    <artifactId>generator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>generator</name>
    <description>mybatis库表实体mapper映射工具</description>
    <properties>    
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
        <!-- mybatis****核心 -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <!-- mysql连接 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core -->
        <!-- mapper类中或有ibatis注解,故引入此依赖 -->
        <dependency>
            <groupId>org.apache.ibatis</groupId>
            <artifactId>ibatis-core</artifactId>
            <version>3.0</version>
        </dependency>
    </dependencies>
</project>

修改完pom.xml之后,等下maven加载相应的依赖。

7.编写入口main类(先图后代码):

自己写一个maven版mybatis****(main方法类做入口)

我自己的代码(整个类,大家主要看main方法就可以了):

package genMain;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
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.config.xml.MyBatisGeneratorConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class createMainJre8 {
    public static void main(String[] args) throws IOException, XMLParserException, 
                InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        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);
        //mybatis-generator-core依赖 1.3.6 后才有MyBatisGenerator类,需要jre1.8才能运行
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}
8.项目写完了,最后执行一下入口类main方法吧(有说没生成的小伙伴,执行完之后别忘记刷新一下啊喂)

  下边放下我的成果截图:

自己写一个maven版mybatis****(main方法类做入口)

成功了的小伙伴晚上给自己加一个鸡腿吧(>.<)