mysql 逆向生成工具_「小程序JAVA实战」Springboot版mybatis逆向生成工具(32)

本文介绍了如何利用MyBatis Generator工具,根据已有的数据库结构,逆向生成Spring Boot项目的PoJO类和Mapper接口。通过配置generatorConfig.xml文件,设置数据库连接信息,运行GeneratorDisplay类即可自动生成相关代码,方便快捷地整合到现有项目中,避免影响业务逻辑。
摘要由CSDN通过智能技术生成

我们的整个项目的构建和分层已经完毕了,现在需要通过一个 工具 将我们整个数据库,反向升成pojo和mybatis类。源码:https://github.com/limingios/wxProgram.git 中的wx-springboot

代码生成器

逆向生成 mysql 对应的pojo和mybatis通过单独的一个工程不影响现有的业务逻辑。

mybatis-generatorConfig工程

21b766488cde859e66ec149d5a8f3430.png

b10c55a74cdb4663011a1e863e11605c.png

1f241a0ef2ba8695b0aed0fe90aa95d0.png

通过源码里面的sql生成对应的表结构和表数据。

1b042035c35739b2750eac76d84c9219.png

mybatis-generatorConfig的pom文件

4.0.0

com.idig8

0.0.1-SNAPSHOT

mybatis-generatorConfig

UTF-8

org.springframework.boot

spring-boot-starter-log4j

1.3.8.RELEASE

com.alibaba

druid

1.1.0

com.alibaba

druid-spring-boot-starter

1.1.0

mysql

mysql-connector-java

5.1.41

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.1

tk.mybatis

mapper-spring-boot-starter

1.2.4

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.3

org.mybatis.generator

mybatis-generator-core

1.3.2

compile

true

添加generatorConfig.xml文件

/p>

PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

connectionURL="jdbc:mysql://47.94.162.28:3306/test"

userId="XXXXX"

password="XXXXX">

type="XMLMAPPER"/>

GeneratorDisplay.java类

package com.idig8.mybatis.utils;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

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;

public class GeneratorDisplay {

public void generator() throws Exception{

List warnings = new ArrayList();

boolean overwrite = true;

//指定 逆向工程配置文件

File configFile = new File("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.java文件

/*

* The MIT License (MIT)

*

* Copyright (c) 2014-2016 abel533@gmail.com

*

* Permission is hereby granted, free of charge, to any person obtaining a copy

* of this software and associated documentation files (the "Software"), to deal

* in the Software without restriction, including without limitation the rights

* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

* copies of the Software, and to permit persons to whom the Software is

* furnished to do so, subject to the following conditions:

*

* The above copyright notice and this permission notice shall be included in

* all copies or substantial portions of the Software.

*

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

* THE SOFTWARE.

*/

package com.idig8.utils;

import tk.mybatis.mapper.common.Mapper;

import tk.mybatis.mapper.common.MySqlMapper;

public interface MyMapper extends Mapper, MySqlMapper {

//TODO

//FIXME 特别注意,该接口不能被扫描到,否则会出错

}

运行GeneratorDisplay的main方法

c160bd2811751f0fabf637b9faf792d2.png

复制com.idig8.mapper目录到wx-springboot-mapper项目下

d95ed20b27d5eab052149ee70cc31114.png

cd11b09988f090995b076f8211cc034c.png

复制com.idig8.pojo 目录到wx-springboot-pojo项目下

f3c3388704d6239df55f8692c7597315.png

f5831f0bd6a3bf89635f3d7a4138ee52.png

复制com.idig8.utils 目录到wx-springboot-common项目下

afac037fc85225190351ecf03ec95279.png

d516758ff6bf11452680aac70821075e.png

复制resources mapper到wx-springboot-api的resources里面

9af9d030fe1b979ae378d3bebbc04862.png

1c1e2c9878579ad885e32749e79cf920.png

PS:mybatis-generatorConfig 可以看成一个单独的项目,主要的目的就是为了升成对应的mapper.xml和对应的pojo实体类,mapper对应的实体。

>>原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!

>>原文链接地址:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值