maven版的mybatis-generator使用步骤

1.新建maven项目mybatis-generator
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.0</modelVersion>
  <groupId>com.xdl</groupId>
  <artifactId>Maven_project</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  

  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                    <version>3.3</version>
                </plugin>
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <dependencies>
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>5.1.35</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                         <!--配置文件的路径-->
                         <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> 
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
3.在src/main/resources下面建立generatorConfig.xml文件,此处注意要在上面pom.xml中指定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="testTables" targetRuntime="MyBatis3">  
        <commentGenerator>  
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
        <!--mysql数据库连接的信息:驱动类、连接地址、用户名、密码 -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
                        connectionURL="jdbc:mysql://localhost:3306/train" userId="用户名"  
                        password="密码">  
        </jdbcConnection>
  
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,  
        为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
  
        <!-- targetProject:生成model类的位置,重要!! -->  
        <javaModelGenerator targetPackage="com.demo.model" targetProject=".\src">  
            <!-- enableSubPackages:是否让schema作为包的后缀 -->  
            <property name="enableSubPackages" value="false"/>  
            <!-- 从数据库返回的值被清理前后的空格 -->  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
  
        <!-- targetProject:mapper映射xml文件生成的位置,重要!! -->  
        <sqlMapGenerator targetPackage="com.demo.mapper"  
                         targetProject=".\src">  
            <property name="enableSubPackages" value="false"/>  
        </sqlMapGenerator>  
  
        <!-- targetPackage:mapper接口生成的位置,重要!! -->  
        <javaClientGenerator type="XMLMAPPER"  
                             targetPackage="com.demo.dao"  
                             targetProject=".\src">  
            <property name="enableSubPackages" value="false"/>  
        </javaClientGenerator>  
  
        <!-- 指定数据库表,要生成哪些表,就写哪些表,要和数据库中对应,不能写错! -->  
        <table tableName="train_info"></table>  
        <table tableName="train_manager"></table>  
        <table tableName="train_order"></table>  
        <table tableName="train_passenger"></table>  
        <table tableName="train_standby_ticket"></table> 
        <table tableName="train_station"></table> 
        <table tableName="train_station_relation"></table> 
        <table tableName="train_ticket"></table> 
        <table tableName="train_user"></table> 
    </context>  
</generatorConfiguration> 
4.执行mybatis-generator:generate命令,生成文件
1.项目右键,maven-->Update Project
2.项目右键,Run As-->3Maven Build-->在Goals中填入"mybatis-generator:generate"
3:点击Run,出现"Build Success"即可
5.刷新项目即可
MyBatis Generator Maven插件可以帮助开发人员自动生成MyBatis的代码,包括Mapper接口、XML映射文件和POJO对象等。以下是使用MyBatis Generator Maven插件的步骤: 1. 配置pom.xml文件 在pom.xml文件中添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> </dependencies> ``` 在pom.xml文件中添加以下插件: ```xml <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 2. 配置generatorConfig.xml文件 在src/main/resources目录下创建generatorConfig.xml文件,并添加以下内容: ```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="context1"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="123456"> </jdbcConnection> <javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="user" domainObjectName="User"/> </context> </generatorConfiguration> ``` 其中,配置文件中的jdbcConnection节点需要根据你的实际情况进行修改。 3. 运行插件 在命令行中执行以下命令: ``` mvn mybatis-generator:generate ``` 插件将会读取generatorConfig.xml文件中的配置信息,并自动生成Mapper接口、XML映射文件和POJO对象等。生成的文件将会保存在target目录下的generated-sources/mybatis-generator目录中。 以上就是使用MyBatis Generator Maven插件的步骤
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值