使用 mybatis-Generator 自动生成DAO、Model、Mapping相关文件
1、Maven项目
2、配置generatorConfig.xml文件
3、在pom.xml中配置插件
2、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"> <generatorConfiguration> <context id="default" targetRuntime="MyBatis3Simple"> <!--创建class时,对注释进行控制--> <commentGenerator> <property name="suppressDate" value="true" /> <!--去除注释--> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--jdbc的数据库连接--> <jdbcConnection driverClass="org.mariadb.jdbc.Driver" connectionURL="jdbc:mariadb://localhost:3306/data_test" userId="oukele" password="oukele"> </jdbcConnection> <!-- Model模型生成器 targetPackage -> 指定生成的model生成所在的包名 targetProject -> 指定在该项目下所在的路径 --> <javaModelGenerator targetPackage="com.oukele.model" targetProject="src/main/java"> <!-- 是否对类CHAR类型的列的数据进行trim操作 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件--> <sqlMapGenerator targetPackage="DynamicSql.xml" targetProject="src/main/resources"/> <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.oukele.dao1" targetProject="src/main/java"/> <!-- tableName 表名 % -> 全部表 --> <table tableName="%"> <generatedKey column="id" sqlStatement="Mysql"/> </table> </context> </generatorConfiguration>
3、在pom.xml文件中配置插件
<?xml version="1.0" encoding="UTF-8"?> <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.oukele</groupId> <artifactId>mybatisdemo</artifactId> <version>1.0</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>2.3.0</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
使用插件,自动生成文件
结果:
详细信息:请点击此处。