Mybatis-Generator使用
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,
由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。
1、首先在maven项目pom.xml添加依赖插件
<!--generator插件--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6</version> </dependency>
然后在<build> --> <plugins>
标签下添加如下代码
声明数据库
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <!--在此作者使用的是MariaDB数据库--> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>2.3.0</version> </dependency> </dependencies> </plugin>
2、在resource下添加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> <properties resource="jdbc.properties"></properties> <context id="xxx" targetRuntime="MyBatis3Simple"> <commentGenerator> <property name="suppressDate" value="true" /> </commentGenerator> <jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}"> </jdbcConnection> <javaModelGenerator targetPackage="test.model" targetProject="D:/二学年/SSM/anntext/src/main/java"> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="test.xml" targetProject="D:/二学年/SSM/anntext/src/main/resources"/> <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="D:/二学年/SSM/anntext/src/main/java"/> <!-- %为全部的表 --> <table tableName="Customer"> <property name="useActualColumnNames" value="true"/> <generatedKey column="id" sqlStatement="Mysql"/> </table> </context> </generatorConfiguration>
然后在菜单栏中
选择Apply就OK了 运行。
不忘初心