逆向工程使用教程

项目设计:

新建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>cn.itcast.cloud</groupId>
  <artifactId>ex-mybatis</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>ex-mybatis</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  <properties>
    <!--  依赖版本  -->
    <mapper.version>3.3.9</mapper.version>
    <mysql.version>5.1.28</mysql.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>tk.mybatis</groupId>
      <artifactId>mapper</artifactId>
      <version>${mapper.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.5</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.5</version>
        <configuration>
          <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
          <overwrite>true</overwrite>
          <verbose>true</verbose>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
          </dependency>
          <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>${mapper.version}</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

 

GeneratorSqlmap:



import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GeneratorSqlmap {

	public void generator() throws Exception{

		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		//指定 逆向工程配置文件generatorConfig.xml
		File configFile = new File("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);

	} 
	public static void main(String[] args) throws Exception {
		try {
			GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
			generatorSqlmap.generator();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}

 

config.properties

# \u6570\u636E\u5E93\u914D\u7F6E
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sqoopdb
jdbc.user=root
jdbc.password=123456

# \u901A\u7528Mapper\u56FA\u5B9A\u914D\u7F6E
mapper.plugin=tk.mybatis.mapper.generator.MapperPlugin
mapper.Mapper=tk.mybatis.mapper.common.Mapper
mapper.forceAnnotation=true

# \u751F\u6210\u6587\u4EF6\u4FDD\u5B58\u4F4D\u7F6E
targetModelPackage=cn.itcast.pojo
targetXMLPackage=cn.itcast.mapper
targetMapperPackage=cn.itcast.mapper
targetJavaProject=src/main/java
targetResourcesProject=src/main/java

 

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="config.properties"/>

	<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
		<property name="beginningDelimiter" value="`"/>
		<property name="endingDelimiter" value="`"/>


		<!--支持序列化-->
		<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>

		<!--
        <plugin type="${mapper.plugin}">
            <property name="mappers" value="${mapper.Mapper}"/>
            <property name="forceAnnotation" value="${mapper.forceAnnotation}" />
        </plugin>
        -->

		<commentGenerator>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>

		<jdbcConnection driverClass="${jdbc.driverClass}"
						connectionURL="${jdbc.url}"
						userId="${jdbc.user}"
						password="${jdbc.password}">
		</jdbcConnection>

		<javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>

		<javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" type="XMLMAPPER">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>

		<!--全部表参与逆向工程-->
		<!--以下example为false,表示不会生成example类,否则将自动生成example类-->
		<table schema="" tableName="%">
			 <!--  enableCountByExample="false"
			   enableUpdateByExample="false"
			   enableDeleteByExample="false"
			   enableSelectByExample="false"
			   selectByExampleQueryId="false">-->
		</table>

		<!--指定某些表参与逆向工程-->
		<!--<table tableName="user"-->
		<!--enableCountByExample="false"-->
		<!--enableUpdateByExample="false"-->
		<!--enableDeleteByExample="false"-->
		<!--enableSelectByExample="false"-->
		<!--selectByExampleQueryId="false">-->
		<!--</table>-->
	</context>
</generatorConfiguration>

sql:

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for t_avgpv_num
-- ----------------------------
DROP TABLE IF EXISTS `t_avgpv_num`;
CREATE TABLE `t_avgpv_num` (
  `id` int(11) DEFAULT NULL,
  `dateStr` varchar(255) DEFAULT NULL,
  `avgPvNum` decimal(6,2) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_avgpv_num
-- ----------------------------
INSERT INTO `t_avgpv_num` VALUES ('1', '20181101', '13.40');
INSERT INTO `t_avgpv_num` VALUES ('2', '20181102', '17.60');
INSERT INTO `t_avgpv_num` VALUES ('3', '20181103', '15.20');
INSERT INTO `t_avgpv_num` VALUES ('4', '20181104', '21.10');
INSERT INTO `t_avgpv_num` VALUES ('5', '20181105', '16.90');
INSERT INTO `t_avgpv_num` VALUES ('6', '20181106', '18.10');
INSERT INTO `t_avgpv_num` VALUES ('7', '20181107', '18.60');


DROP TABLE IF EXISTS `t_flow_num`;
CREATE TABLE `t_flow_num` (
  `id` int(11) DEFAULT NULL,
  `dateStr` varchar(255) DEFAULT NULL,
  `pVNum` int(11) DEFAULT NULL,
  `uVNum` int(11) DEFAULT NULL,
  `iPNum` int(11) DEFAULT NULL,
  `newUvNum` int(11) DEFAULT NULL,
  `visitNum` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_flow_num
-- ----------------------------
INSERT INTO `t_flow_num` VALUES ('1', '20181101', '4702', '3096', '2880', '2506', '3773');
INSERT INTO `t_flow_num` VALUES ('2', '20181102', '7528', '4860', '4435', '4209', '5937');
INSERT INTO `t_flow_num` VALUES ('3', '20181103', '7286', '4741', '4409', '4026', '5817');
INSERT INTO `t_flow_num` VALUES ('4', '20181104', '6653', '5102', '4900', '2305', '4659');
INSERT INTO `t_flow_num` VALUES ('5', '20181105', '5957', '4943', '4563', '3134', '3698');
INSERT INTO `t_flow_num` VALUES ('6', '20181106', '7978', '6567', '6063', '4417', '4560');
INSERT INTO `t_flow_num` VALUES ('7', '20181107', '6666', '5555', '4444', '3333', '3232');

DROP TABLE IF EXISTS `t_referer_type`;
CREATE TABLE `t_referer_type` (
  `id` int(11) NOT NULL,
  `dateStr` varchar(255) NOT NULL,
  `type` varchar(255) NOT NULL,
  `type_num` int(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_referer_type
-- ----------------------------
INSERT INTO `t_referer_type` VALUES ('1', '20181101', '搜索引擎', '1216');
INSERT INTO `t_referer_type` VALUES ('2', '20181101', '直接来源', '1752');
INSERT INTO `t_referer_type` VALUES ('3', '20181101', '链接访问', '637');
INSERT INTO `t_referer_type` VALUES ('4', '20181101', '邮件营销', '512');
INSERT INTO `t_referer_type` VALUES ('5', '20181102', '搜索引擎', '1103');
INSERT INTO `t_referer_type` VALUES ('6', '20181102', '直接来源', '1833');
INSERT INTO `t_referer_type` VALUES ('7', '20181102', '链接访问', '759');
INSERT INTO `t_referer_type` VALUES ('8', '20181102', '邮件营销', '634');

 

运行: 

效果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值