SpringBoot使用Mybatis 逆向工程(自动生成pojo和mapper)
Mybatis逆向生成工具可以通过数据库映射生成对应的 mapper 接口、mapper.xml 文件和 pojo 类
逆向生成工具的制作如下:(💕文末附下载链接)
创建一个 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>com.jingchao.mybatis</groupId> <artifactId>mybatis_generator_for_springboot</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!--mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency> <!-- mybatis 逆向生成工具 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> <scope>compile</scope> <optional>true</optional> </dependency> </dependencies> </project>
配置 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> <context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="com.jingchao.mybatis.utils.MyMapper"/> </plugin> <!-- 数据库的连接信息 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC" userId="root" password="123456"> </jdbcConnection> <!-- javaBean的生成策略 --> <javaModelGenerator targetPackage="com.jingchao.mybatis.pojo" targetProject="src/main/java"> </javaModelGenerator> <!-- SQL映射文件的生成策略 --> <sqlMapGenerator targetPackage="com.jingchao.mybatis.mapper" targetProject="src/main/resources"> </sqlMapGenerator> <!-- Mapper接口的生成策略 --> <javaClientGenerator targetPackage="com.jingchao.mybatis.mapper" targetProject="src/main/java" type="XMLMAPPER"> </javaClientGenerator> <!--逆向分析表--> <!-- tableName设置为*号,可以对应所有表 --> <table tableName="t_emp"/> </context> </generatorConfiguration>
注意:一定要提前创建好数据库 (本文的数据库表为 part-time-platform)
创建一个 GeneratorDisplay 类
package com.jingchao.mybatis.utils; 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 GeneratorDisplay { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 File configFile = new File("src/main/resources/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 { GeneratorDisplay generatorSqlmap = new GeneratorDisplay(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }
创建 MyMapper 类
package com.jingchao.mybatis.utils; import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.MySqlMapper; //继承自己的MyMapper public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {}
效果展示