mybatis generator 使用方法
环境: ubuntu eclipse maven
一. 简介
mybatis-geneator是一款mybatis自动代码生成工具,可以通过配置,快速生成mapper和xml文件以及pojo
二.配置
pom.xml配置
<dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> </dependencies> <build> <finalName>shop</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugin> </plugins> </build>
在main的resource目录下创建generatorConfig.xml文件
generatorConfig.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"> <!-- classPath:数据库的JDBC驱动--> <generatorConfiguration> <classPathEntry location="/home/jiang/.m2/repository/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16.jar"/> <context id="default" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="false"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://106.13.46.152:3306/shop" userId="jiang" password="1"/> <javaModelGenerator targetPackage="com.feilong.shop.entity" targetProject="./src/main/java"> <!-- TODO enableSubPackages:是否让schema作为包的后缀--> <property name="enableSubPackages" value="false"/> <!-- 从数据库返回的值被清理前后的空格--> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--map xml生成器 --> <sqlMapGenerator targetPackage="com.feilong.shop.dao.mappers" targetProject="./src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- dao生成器--> <javaClientGenerator targetPackage="com.feilong.shop.dao" targetProject="./src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 数据表与Bean的映射 --> <table tableName="shop_user" domainObjectName="User" > <!-- 如果设置为true,生成的model类会直接使用column本身的名字,而不会再使用驼峰命名方法,比如BORN_DATE,生成的属性名字就是BORN_DATE,而不会是bornDate --> <property name="useActualColumnNames" value="true"/> </table> <table tableName="shop_address" domainObjectName="Address" > <property name="useActualColumnNames" value="true"/> </table> <table tableName="shop_cart" domainObjectName="Cart" > <property name="useActualColumnNames" value="true"/> </table> <table tableName="shop_category" domainObjectName="Category" > <property name="useActualColumnNames" value="true"/> </table> <table tableName="shop_comment" domainObjectName="Comment" > <property name="useActualColumnNames" value="true"/> </table> <table tableName="shop_goods" domainObjectName="Goods" > <property name="useActualColumnNames" value="true"/> </table> <table tableName="shop_order" domainObjectName="Order" > <property name="useActualColumnNames" value="true"/> </table> <table tableName="shop_orderdetail" domainObjectName="OrderDetail" > <property name="useActualColumnNames" value="true"/> </table> <table tableName="shop_orderstatus" domainObjectName="OrderStatus" > <property name="useActualColumnNames" value="true"/> </table> <table tableName="visit" domainObjectName="Visit" > <property name="useActualColumnNames" value="true"/> </table> </context> </generatorConfiguration>
三. 生成文件
linux控制台在项目pom.xml配置文件的同级目录下输入
mvn mybatis-generator:generate