idea中使用mybatis逆向工程
发布日期:2021-06-29 17:03:04 浏览次数:2 分类:技术文章

本文共 2928 字,大约阅读时间需要 9 分钟。

  如果使用过mybatis的人就会发现,当我们使用mybatis时,我们每次都需要自己手动创建实体类,映射文件(当然你也可以用注释),还有接口来进行使用,这样手动创建非常的繁琐,mybatis考虑到这方面所以就为我们提供了逆向工程,使得系统可以自动创建配套的文件,让我们可以直接使用。废话不多说,下面开始;

一,添加pom依赖:

  由于逆向工程需要使用到mybatis-generator-core,所以我们需要添加对应的依赖,如下:

org.mybatis.generator
mybatis-generator-core
1.3.5

二、在maven项目下的src/main/resources 目录下建立名为 Maven的项目配置文件存放路径如下图:

创建generator.properties用于存放数据库配置,generatorConfig用于配置逆向工程

 

下面贴出两个文件的具体配置

复制代码

复制代码

下面是properties文件:

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/testjdbc.userId=rootjdbc.pwd=123456

三.添加主程序运行项目:

  网上也有添加逆向工程的工具从而运行逆向工程,这里我们先介绍使用主程序运行来生成逆向工程:这个是官方提供的运行程序

复制代码

package com.lwc.generator;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 GeneratorTest {    public void testGenerator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {        List
warnings=new ArrayList
(); boolean overWriter=true;    //指向配置文件   File configFile=new File(GeneratorTest.class.getResource("/setting/generatorConfig").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 { GeneratorTest generatorTest=new GeneratorTest(); generatorTest.testGenerator(); }}

复制代码

四,添加工具包进行逆向工程:

首先得在pom中添加如下工具:

复制代码

org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
true
true

复制代码

点击 菜单run中Edit Configurations

 

 点击加号,并选择maven

在name和Commond line分别填上如上图所示,apply和ok

点击如下运行:

 

 

以上两种都可以进行逆向工程,运行结果如下

生成的接口:

生成的实体类:

生成的映射文件:

 

 由于我只会简单的使用逆向工程,所以只是用到了部分配置,下面给出完整配置及其注释:

复制代码

复制代码

 

转载地址:https://superman.blog.csdn.net/article/details/109399020 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:boost::multi_array模块实现创建负步幅视图的小测试
下一篇:boost::multi_array模块调整 multi_arrays 大小的测试

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月20日 02时45分32秒