ToB企服应用市场:ToB评测及商务社交产业平台

标题: SpringBoot项目中使用mybatis逆向工程 [打印本页]

作者: 王柳    时间: 2023-11-8 14:59
标题: SpringBoot项目中使用mybatis逆向工程
mybatis逆向工程,即利用现有的数据表结构,生成对应的model实体类、dao层接口,以及对应的mapper.xml映射文件。借助mybatis逆向工程,我们无需手动去创建这些文件。
下面是使用Java代码的方式来实现逆向工程,生成文件(也可以使用插件来生成):
首先,导入需要的依赖包:mybatis逆向工程的依赖和数据库的依赖
  1.         <dependency>
  2.             <groupId>org.mybatis.generator</groupId>
  3.             <artifactId>mybatis-generator-core</artifactId>
  4.             <version>1.3.5</version>
  5.         </dependency>
  6.         <dependency>
  7.             <groupId>mysql</groupId>
  8.             <artifactId>mysql-connector-java</artifactId>
  9.             <version>8.0.24</version>
  10.         </dependency>
复制代码
 

 关键在于这三个文件,mybatis-generatorConfig.xml是逆向工程的配置文件。generator.properties里面是数据库信息,提供给mybatis-generatorConfig.xml进行使用。GeneratorUtil.java是用来生成文件的Java代码,运行其中的main方法即可实现逆向工程文件生成。
以下分别是这三个文件中的内容(根据自己的需求和数据库信息进行修改)
generator.properties:
 
  1. jdbc.driver=com.mysql.cj.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/pet
  3. jdbc.userId=root
  4. jdbc.pwd=123456
复制代码
 
mybatis-generatorConfig.xml:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3.         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4.         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6.    
  7.     <properties resource="generator.properties"></properties>
  8.     <context id="testTables" targetRuntime="MyBatis3">
  9.         <commentGenerator>
  10.             
  11.             <property name="suppressAllComments" value="true"/>
  12.         </commentGenerator>
  13.         
  14.         <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.userId}" password="${jdbc.pwd}"></jdbcConnection>
  15.         
  16.         <javaTypeResolver>
  17.             <property name="forceBigDecimals" value="false"/>
  18.         </javaTypeResolver>
  19.         
  20.         <javaModelGenerator targetPackage="com.zyk.model" targetProject="src/main/java">
  21.             
  22.             <property name="enableSubPackages" value="false"/>
  23.             
  24.             <property name="trimStrings" value="true"/>
  25.         </javaModelGenerator>
  26.         
  27.         <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
  28.             <property name="enableSubPackages" value="false"></property>
  29.         </sqlMapGenerator>
  30.         
  31.         <javaClientGenerator type="XMLMAPPER" targetPackage="com.zyk.dao" targetProject="src/main/java">
  32.             <property name="enableSubPackages" value="false"/>
  33.         </javaClientGenerator>
  34.         
  35.         <table tableName="comments"  domainObjectName="Comment"
  36.                enableCountByExample="false" enableUpdateByExample="false"
  37.                enableDeleteByExample="false" enableSelectByExample="false"
  38.                selectByExampleQueryId="false"></table>
  39.         <table tableName="follows"  domainObjectName="Follow"
  40.                enableCountByExample="false" enableUpdateByExample="false"
  41.                enableDeleteByExample="false" enableSelectByExample="false"
  42.                selectByExampleQueryId="false"></table>
  43.         <table tableName="pets"  domainObjectName="Pet"
  44.                enableCountByExample="false" enableUpdateByExample="false"
  45.                enableDeleteByExample="false" enableSelectByExample="false"
  46.                selectByExampleQueryId="false"></table>
  47.         <table tableName="pictures"  domainObjectName="Picture"
  48.                enableCountByExample="false" enableUpdateByExample="false"
  49.                enableDeleteByExample="false" enableSelectByExample="false"
  50.                selectByExampleQueryId="false"></table>
  51.         <table tableName="posts"  domainObjectName="Post"
  52.                enableCountByExample="false" enableUpdateByExample="false"
  53.                enableDeleteByExample="false" enableSelectByExample="false"
  54.                selectByExampleQueryId="false"></table>
  55.         <table tableName="reports"  domainObjectName="Report"
  56.                enableCountByExample="false" enableUpdateByExample="false"
  57.                enableDeleteByExample="false" enableSelectByExample="false"
  58.                selectByExampleQueryId="false"></table>
  59.         <table tableName="requests"  domainObjectName="Request"
  60.                enableCountByExample="false" enableUpdateByExample="false"
  61.                enableDeleteByExample="false" enableSelectByExample="false"
  62.                selectByExampleQueryId="false"></table>
  63.         <table tableName="users"  domainObjectName="User"
  64.                enableCountByExample="false" enableUpdateByExample="false"
  65.                enableDeleteByExample="false" enableSelectByExample="false"
  66.                selectByExampleQueryId="false"></table>
  67.     </context>
  68. </generatorConfiguration>
复制代码
GeneratorUtil.java :
  1. package com.zyk.util;
  2. import org.mybatis.generator.api.MyBatisGenerator;
  3. import org.mybatis.generator.config.Configuration;
  4. import org.mybatis.generator.config.xml.ConfigurationParser;
  5. import org.mybatis.generator.exception.InvalidConfigurationException;
  6. import org.mybatis.generator.exception.XMLParserException;
  7. import org.mybatis.generator.internal.DefaultShellCallback;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.sql.SQLException;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. public class GeneratorUtil {
  14.     public void testGenerator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
  15.         List<String> warnings=new ArrayList<String>();
  16.         boolean overWriter=true;
  17.         //指向配置文件  
  18.         File configFile=new File(GeneratorUtil.class.getResource("/mybatis-generatorConfig.xml").getFile());
  19.         ConfigurationParser cp=new ConfigurationParser(warnings);
  20.         Configuration config=cp.parseConfiguration(configFile);
  21.         DefaultShellCallback callback=new DefaultShellCallback(overWriter);
  22.         MyBatisGenerator myBatisGenerator=new MyBatisGenerator(config,callback,warnings);
  23.         myBatisGenerator.generate(null);
  24.     }
  25.     public static void main(String[] args)throws Exception {
  26.         GeneratorUtil generatorTest=new GeneratorUtil();
  27.         generatorTest.testGenerator();
  28.     }
  29. }
复制代码
 
执行GeneratorUtil.java中的main方法即可使用mybatis逆向工程生成文件。
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4