eclipse maven 插件 自动生成代码
jopen
9年前
1、首先,用eclipse创建一个maven项目。
2、其次,在pom文件下,加入如下插件配置:
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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.liyi.test</groupId> <artifactId>hello-mybatis</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>hello-mybatis Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>hello-mybatis</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/mybatis-generator/generator.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies> </plugin> </plugins> </pluginManagement> </build> </project>
3、从pom.xml中找到configurationFile标签,按照标签上的路径放入一下生成代码的核心配置文件,generator.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> <!-- 数据库驱动包位置 --> <classPathEntry location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.21\mysql-connector-java-5.1.21.jar" /> <!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />--> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库链接URL、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="1234"> <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">--> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成实体类的包名和位置,这里配置将生成的实体类放在com.liyi.test.domain这个包下 --> <javaModelGenerator targetPackage="com.liyi.test.domain" targetProject="M:\workspace\hello-mybatis\src\main\java\"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成的SQL映射文件包名和位置,这里配置将生成的SQL映射文件放在com.liyi.test.mapping这个包下 --> <sqlMapGenerator targetPackage="com.liyi.test.mapping" targetProject="M:\workspace\hello-mybatis\src\main\java\"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置,这里配置将生成的dao类放在com.liyi.test.mapping这个包下 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.liyi.test.dao" targetProject="M:\workspace\hello-mybatis\src\main\java\"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成那些表(更改tableName和domainObjectName就可以) --> <table tableName="tab_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> </context> </generatorConfiguration>
4、cmd打开dos窗口,到项目的根路径,比如我的
M:
cd workspace/hello-mybatis/
执行mvn mybatis-generator:generate(前提是你的电脑配置了maven的环境变量,可以用mvn命令)
刷新项目就有以下效果了。