Maven中Mybatis逆向工程的使用(自动生成代码)
Posted stitchGoGo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven中Mybatis逆向工程的使用(自动生成代码)相关的知识,希望对你有一定的参考价值。
1、添加maven插件,让maven环境支持mybatis-generator组件
在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>cn.mybatis.generator</groupId> <artifactId>mybatis-generator</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <!-- Mybaits逆转工程依赖包 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!-- Mybaits 依赖jar包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.1</version> </dependency> <!-- log4j 依赖jar包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</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> <!--JDK版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <showWarnings>true</showWarnings> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
2、新建generatorConfig.xml配置文件(在pom.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="generateTables" targetRuntime="MyBatis3"> <!-- 此处是将 Example 改名为 Criteria <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"> <property name="searchString" value="Example" /> <property name="replaceString" value="Criteria" /> </plugin> --> <!-- 序列化 --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> <property name="suppressDate" value="true" /> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://39.108.217.125:3306/RoboBlogs" userId="root" password="HZBhzb1237763522-1"> </jdbcConnection> <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg" password="yycg"> </jdbcConnection> --> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="com.Mybaits.pojo" targetProject=".\\src\\main\\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.Mybaits.mapper" targetProject=".\\src\\main\\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.Mybaits.mapper" targetProject=".\\src\\main\\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 保留Example例子 --> <table schema="" tableName="u01" domainObjectName="u01" /> <table schema="" tableName="Upload_info" domainObjectName="Upload_info"/> <table schema="" tableName="Upload_UserPushContent" domainObjectName="Upload_UserPushContent" /> <!-- 指定数据库表 去除Example例子 <table schema="general" tableName="tb_item" domainObjectName="Item" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="useActualColumnNames" value="false" /> </table> --> </context> </generatorConfiguration>
3.1、运用maven指令生成逆向工程代码
3.2、使用单元测试生成逆向工程代码:
package MybaitsGenerator; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; import org.junit.Test; public class MybaitsGeneratorTest { @Test public void MybaitsGenerator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true;//加载上面的配置文件 File configFile = new File("./src/main/resources/generatorconfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } }
4.刷新页面,效果如:
以上是关于Maven中Mybatis逆向工程的使用(自动生成代码)的主要内容,如果未能解决你的问题,请参考以下文章
基于maven+ssm的增删改查之使用mybatis逆向工程生成相关文件
13.3.1 使用Maven运行 MyBatis Generator(MyBatis Generator逆向代码生成工具) -《SSM深入解析与项目实战》
13.3.1 使用Maven运行 MyBatis Generator(MyBatis Generator逆向代码生成工具) -《SSM深入解析与项目实战》