自动生成实体类、Mapper、Mapper.xml文件

搭建Spring Boot + Mysql + MyBatis 项目

核心

配置pom.xml

创建表

配置文件

生成文件

结果

项目结构

搭建Spring Boot + Mysql + MyBatis 项目

idea 可直接创建相应的项目及配置

核心

配置pom.xml

mysql

mysql-connector-java

5.1.38

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

com.example

common

0.0.1-SNAPSHOT

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.1.1

com.alibaba

druid

1.0.26

org.mybatis.generator

mybatis-generator-maven-plugin

1.3.5

创建表

DROP TABLE IF EXISTS `user_info`;

CREATE TABLE `user_info` (

`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '编号',

`name` varchar(255) DEFAULT NULL COMMENT '名称',

`age` int(11) DEFAULT NULL COMMENT '年龄',

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

配置文件

生成文件

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;

/**

@User: D·Ian GHQ

@Date: 2018/10/23 0023

@Email: iangan@kingyon.cn

*/

public class MybatisGenerateUtil {

public static void main(String[] args) throws Exception {

List warnings = new ArrayList();

// 根据配置文件生成相应的实体类、mapper文件

Configuration config = new ConfigurationParser(warnings).parseConfiguration(new File("_database/mybatis-generate-user.xml"));

new MyBatisGenerator(config, new DefaultShellCallback(true), warnings).generate(null);

}

}

结果

实体类

accf1bfba61ea42dca969c78aaff1031.png

mapper

f7aa2403c32cc7a92dcffa8a8917e6b5.png

mapper.xml

4d0ed6c582c2c7b761eb5deb63b81416.png

项目结构

5b3b90159ad02e54bdfbc2152f423e9a.png

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐