Spring中使用Mybatis逆向工程(IDEA版)

准备工作,首先配置好

pom.xml文件

导入必要的包:

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
添加插件
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- 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>8.0.22</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>

配置generator.properties文件,里面主要放连接数据库的驱动之类的配置。

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/product_test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=root

然后在配置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="mybatis.properties" />
-->
<!-- <classPathEntry location="该jar包的位置\mysql\mysql-connector-java\8.0.15\mysql-connector-java-8.0.15.jar" />-->
<!--加载资源文件-->
<properties resource="generator.properties"></properties>
<context id="msqlTables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<commentGenerator>
<!--是否去除自动生成的注释 true是:false 否-->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection connectionURL="${jdbc.url}"
driverClass="${jdbc.driver}" password="${jdbc.password}" userId="${jdbc.username}" >

<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.lhd.entry" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapping" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.lhd.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<!-- table指定每个表的生成策略 -->
<table tableName="bus_customer" domainObjectName="BusCustomer"></table>
<table tableName="bus_goods" domainObjectName="BusGoods"></table>

<table tableName="bus_inport" domainObjectName="BusInport"></table>
<table tableName="bus_outport" domainObjectName="BusOutport"></table>

<table tableName="bus_provider" domainObjectName="BusProvider"></table>
<table tableName="bus_sales" domainObjectName="BusSales"></table>

<table tableName="bus_salesback" domainObjectName="BusSalesback"></table>
<table tableName="sys_dept" domainObjectName="SysDept"></table>

<table tableName="sys_log_login" domainObjectName="SysLogLogin"></table>
<table tableName="sys_notice" domainObjectName="SysNotice"></table>

<table tableName="sys_permission" domainObjectName="SysPermission"></table>
<table tableName="sys_role" domainObjectName="SysRole"></table>

<table tableName="sys_role_permission" domainObjectName="SysRolePermission"></table>
<table tableName="sys_role_user" domainObjectName="SysRoleUser"></table>

<table tableName="sys_user" domainObjectName="SysUser"></table>
</context>
</generatorConfiguration>

在Run找到Run Configurations配置使用maven运行的方式

mybatis-generator:generate -e

配置好后随后到最右侧点击Maven图标,查看你配置的项目Run Configurations找到下面的Mybatis-Generator直接点击启动,稍微等待一下就好了。



posted @ 2021-01-19 11:36  RickL  阅读(125)  评论(0编辑  收藏  举报