逆向工程(mybatis-generator)
2021-07-27 本文已影响0人
Yluozi
springboot+mybatis+mybatis-generator 实现数据查询链接:https://www.jianshu.com/p/6bcaf86e84f0
1配置
--pom
<?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">
<parent>
<artifactId>DemoDubbo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>
src/main/resources/generator/genteratorCongfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
--启动项
import tk.mybatis.spring.annotation.MapperScan;
@MapperScan("net.cnki.knmarket.dao")
--genteratorCongfig.xml
<!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="generator/config.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
<property name="caseSensitive" value="true"/>
<property name="lombok" value="Getter,Setter,ToString"/>
</plugin>
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.url}"
userId="${jdbc.user}"
password="${jdbc.password}">
</jdbcConnection>
<!--实体-->
<javaModelGenerator targetPackage="net.cnki.knmarket.domain.entity.${moduleName}"
targetProject="src/main/java"/>
<!--mapper.xml-->
<sqlMapGenerator targetPackage="mapper.${moduleName}"
targetProject="./src/main/resources"/>
<!--mapper接口-->
<javaClientGenerator targetPackage="net.cnki.knmarket.dao.${moduleName}"
targetProject="src/main/java"
type="XMLMAPPER"/>
<!--为哪张表生成代码-->
<table tableName="${tableName}">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>
2使用
在此文件下配置文件夹名称以及对应表名(此名不可有空格,具体问题见注解1)
image.png
点击maven下的包执行工程程序,即可生产想要的工程文件
image.png
注解1
报错找不到文件
Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate (default-cli) on project knmarket-manage: .\src\main\resources\mapper\mall \KMallMapper.xml (系统找不到指定的路径。) -> [Help 1]
在git 下执行启动命令并打印日志(-X)
image.png
错误原因为文件名存在空格
image.png