2. 创建工程
2.1 项目架构
2.1.1 创建空项目
1. 新建一个空项目,命名为 crowdfunding
设置 Project Settings
2. 新建三个 Maven Module
不选择模板(即不勾选 Create from archetype) 并设置GroupId
- crowdfunding01-admin-parent
- crowdfunding05-common-util
- crowdfunding06-common-reverse
3. 新建三个 Maven Module并进行Parent选择
- crowdfunding02-admin-webui
- crowdfunding03-admin-component
- crowdfunding04-admin-entity
4. 选择打包方式 (pom.xml文件中)
2.1.2 建立工程之间的依赖关系(pom.xml文件中)
2.2 创建数据库和数据库表
2.2.1 新建数据库
CREATE DATABASE `crowdfouding_project` CHARACTER SET utf8;
2.2.2 新建表
USE project_crowd;
DROP TABLE IF EXISTS t_admin;
CREATE TABLE t_admin
(
id INT NOT NULL AUTO_INCREMENT, # 主键
login VARCHAR(255) NOT NULL, # 登录账号
user_pswd CHAR(32) NOT NULL, # 登录密码
user_name VARCHAR(255) NOT NULL, # 昵称
email VARCHAR(255) NOT NULL, # 邮箱
create_time CHAR(19), # 创建时间
PRIMARY KEY (id) # 设置主键
);
2.3 基于Maven的MyBatis逆向工程
2.3.1 配置
1. 逆向工程为 crowdfunding06-common-reverse模块 2. 在crowdfunding06-common-reverse 下的pom.xml 配置逆向工程
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<!-- 控制Maven在构建过程中相关配置 -->
<build>
<!-- 构建过程中用到的插件 -->
<plugins>
<!-- 具体插件,逆向工程的操作是以构建过程中插件形式出现的 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<!-- 插件的依赖 -->
<dependencies>
<!-- 逆向工程的核心依赖 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2</version>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
</dependencies>
</plugin>
<!--jdk版本-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
3. 在Resources目录下新建文件 generatorConfig.xml
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--
targetRuntime: 执行生成的逆向工程的版本
MyBatis3Simple: 生成基本的CRUD(清新简洁版)
MyBatis3: 生成带条件的CRUD(奢华尊享版)
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 数据库的连接信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/crowdfunding_project?characterEncoding=utf8"
userId="root"
password="123456">
</jdbcConnection>
<!-- javaBean的生成策略-->
<javaModelGenerator targetPackage="com.crowdfunding.pojo" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- SQL映射文件的生成策略 -->
<sqlMapGenerator targetPackage="com.crowdfunding.mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- Mapper接口的生成策略 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.crowdfunding.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 逆向分析的表 -->
<!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
<!-- domainObjectName属性指定生成出来的实体类的类名 -->
<table tableName="t_admin" domainObjectName="Admin"/>
</context>
</generatorConfiguration>
2.3.2 构建
-
进行构建(双击即可)
-
为Admin类添加无参+有参构造器和toString方法(alt+insert)