欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > 数据库 >内容正文

数据库

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

发布时间:2025/3/12 数据库 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 mysql 自动生成mapper_自动生成实体类、Mapper、Mapper.xml文件 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

自动生成实体类、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);

}

}

结果

实体类

mapper

mapper.xml

项目结构

创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖

总结

以上是生活随笔为你收集整理的mysql 自动生成mapper_自动生成实体类、Mapper、Mapper.xml文件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。