效果展示
步骤:
1.POM依赖
在build-plugins下添加
<!-- Mybatis-generator插件,用于自动生成Mapper和POJO -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!-- mybatis-generator的配置文件,根据情况调整位置 -->
<configurationFile>${basedir}/src/main/resources/static/config/generatorConfig.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>
2.新建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>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="D:\apache-maven-3.3.3\maven-repository\mysql\mysql-connector-java\8.0.17\mysql-connector-java-8.0.17.jar"/>
<context id="default" targetRuntime="MyBatis3">
<!--创建Java类时是否取消生成注释-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库的相关配置 &为特殊字符使用&替换 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/evaluation"
userId="admin"
password="admin"/>
<!--<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>-->
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.client.model" targetProject="src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="true"/>
<!-- 是否对model添加构造函数 -->
<property name="constructorBased" value="false"/>
<!-- 是否对类CHAR类型的列的数据进行trim操作 -->
<property name="trimStrings" value="true"/>
<!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- *Mapper.xml文件的位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/static">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- Dao层接口文件的位置 -->
<javaClientGenerator targetPackage="com.client.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
<table tableName="notice" domainObjectName="Notice" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
3.生成实体类、Dao、Mapper