使用idea建立逆向工程
Posted luobentomoon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用idea建立逆向工程相关的知识,希望对你有一定的参考价值。
逆向工程(Mybatis Generator)
概念
mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表 自动生成mybatis执行所需要的代码.(mapper.java、mapper.xml、po类)
常采用数据库生成java代码
相关配置文件
这里使用IDEA自带的maven插件配置搭建运行环境,当然,官网提供了更多使用generator的方法,具体可以参考 其他方法.
一、在使用maven之前,有必要贴一下maven插件所必须的setting.xml (默认是在C:Users${username}.m2路径下)
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>E:JavaMaven epository</localRepository> <interactiveMode>true</interactiveMode> <usePluginRegistry>false</usePluginRegistry> <offline>false</offline> <pluginGroups/> <servers/> <mirrors> <mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors> <proxies/> <profiles/> <activeProfiles/> </settings>
二、接下来,重点是maven 使用的 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>club.lemos</groupId> <artifactId>mybatis-generator</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <properties> <spring.version>4.3.4.RELEASE</spring.version> <mybatis.version>3.4.1</mybatis.version> <slf4j.version>1.7.21</slf4j.version> </properties> <dependencies> <!-- mybatis 包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis与spring整合包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!-- javaee jar包--> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- mysql 驱动包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <!--dbcp2--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency> <!-- 日志文件管理包--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> </dependencies> <build> <resources> <!-- 复制非java文件到class路径下--> <resource> <directory>${basedir}/src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>${basedir}/src/main/resources</directory> </resource> </resources> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <!-- 配置generatorConfig.xml的路径和名称 --> <configuration> <configurationFile> ${basedir}srcmain esourcesgeneratorConfig.xml </configurationFile> </configuration> <!-- 配置generator运行插件,使maven执行generator --> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
三、下面是generator的重要配置文件
该文件在项目中所在的位置:注意以上的properties配置应该和db.properties的匹配
文件的配置信息:
<?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> <!-- !!!! Driver Class Path !!!! --> <classPathEntry location="E:JavaMaven epository-taotaomysqlmysql-connector-java5.1.40mysql-connector-java-5.1.40.jar"/> <context id="context" targetRuntime="MyBatis3"> <!-- 是否去除注释--> <commentGenerator> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator> <!-- !!!! Database Configurations !!!! --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/taotao?characterEncoding=UTF8&useSSL=false" userId="root" password="lemo"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- !!!! Model Configurations !!!! --> <javaModelGenerator targetPackage="com.taotao.po" targetProject="src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- !!!! Mapper XML Configurations !!!! --> <sqlMapGenerator targetPackage="com/taotao/mapping" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- !!!! Mapper Interface Configurations !!!! --> <javaClientGenerator targetPackage="com.taotao.mapper" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- !!!! Table Configurations !!!! --> <!-- 指定数据库表 --> <table schema="" tableName="tb_content"/> <table schema="" tableName="tb_content_category"/> <table schema="" tableName="tb_item"/> <table schema="" tableName="tb_item_cat"/> </context> </generatorConfiguration>
四、配置运行maven
首先
然后,这里是+,然后选择maven,接着如下
最后,尝试运行
如果成功,输出窗口是这样的:
同时,相应的文件夹会生成项目需要的文件.
END
以上是关于使用idea建立逆向工程的主要内容,如果未能解决你的问题,请参考以下文章
IDEA - 利用 Mybatis 使用 maven 命令生成逆向工程