SpringBoot项目中使用mybatis逆向工程
mybatis逆向工程,即利用现有的数据表结构,生成对应的model实体类、dao层接口,以及对应的mapper.xml映射文件。借助mybatis逆向工程,我们无需手动去创建这些文件。下面是使用Java代码的方式来实现逆向工程,生成文件(也可以使用插件来生成):
首先,导入需要的依赖包:mybatis逆向工程的依赖和数据库的依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
https://img2023.cnblogs.com/blog/3163107/202310/3163107-20231018232929233-1011715043.png
关键在于这三个文件,mybatis-generatorConfig.xml是逆向工程的配置文件。generator.properties里面是数据库信息,提供给mybatis-generatorConfig.xml进行使用。GeneratorUtil.java是用来生成文件的Java代码,运行其中的main方法即可实现逆向工程文件生成。
以下分别是这三个文件中的内容(根据自己的需求和数据库信息进行修改)
generator.properties:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/pet
jdbc.userId=root
jdbc.pwd=123456
mybatis-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="generator.properties"></properties>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.userId}" password="${jdbc.pwd}"></jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.zyk.model" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
<property name="enableSubPackages" value="false"></property>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zyk.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<table tableName="comments"domainObjectName="Comment"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="follows"domainObjectName="Follow"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="pets"domainObjectName="Pet"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="pictures"domainObjectName="Picture"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="posts"domainObjectName="Post"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="reports"domainObjectName="Report"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="requests"domainObjectName="Request"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="users"domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>GeneratorUtil.java :
package com.zyk.util;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class GeneratorUtil {
public void testGenerator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings=new ArrayList<String>();
boolean overWriter=true;
//指向配置文件
File configFile=new File(GeneratorUtil.class.getResource("/mybatis-generatorConfig.xml").getFile());
ConfigurationParser cp=new ConfigurationParser(warnings);
Configuration config=cp.parseConfiguration(configFile);
DefaultShellCallback callback=new DefaultShellCallback(overWriter);
MyBatisGenerator myBatisGenerator=new MyBatisGenerator(config,callback,warnings);
myBatisGenerator.generate(null);
}
public static void main(String[] args)throws Exception {
GeneratorUtil generatorTest=new GeneratorUtil();
generatorTest.testGenerator();
}
}
执行GeneratorUtil.java中的main方法即可使用mybatis逆向工程生成文件。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]