【Spring Boot+MyBatis】配置generatorMapper.xml自动生成实体类、Mapper和xml文件

        前提:先在Navicat中建立数据库及表,并在IntelliJ IDEA中连接数据库。

目录

1. 在Navicat中建立数据库及表

2. 在IntelliJ IDEA中连接本地数据库

3. 在pom.xml中添加mysql、mybatis依赖

4. 在src/main/resources目录下创建generatorMapper.xml文件

5. 在pom.xml中添加mybatis代码自动生成插件

6. 配置好maven插件后,重新加载maven

7. 单击mybatis-generator:generate,生成实体类、Mapper和*Mapper.xml文件


1. 在Navicat中建立数据库及表(数据库表结构如下)

/*
Navicat MySQL Data Transfer

Source Server         : community
Source Server Version : 50720
Source Host           : localhost:3306
Source Database       : community

Target Server Type    : MYSQL
Target Server Version : 50720
File Encoding         : 65001

Date: 2021-12-03 21:07:47
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for comment
-- ----------------------------
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
  `id` int(11) NOT NULL,
  `user_id` int(20) DEFAULT NULL COMMENT '评论的用户id,创建索引',
  `entity_id` int(20) DEFAULT NULL COMMENT '评论实体id,创建索引',
  `entity_type` int(50) DEFAULT NULL COMMENT '评论实体类型:1 帖子评论,2 评论回复',
  `target_id` int(50) DEFAULT NULL COMMENT '评论目标id',
  `content` text COMMENT '评论内容',
  `status` int(50) DEFAULT NULL COMMENT '评论状态:0 有效,1 无效',
  `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of comment
-- ----------------------------

-- ----------------------------
-- Table structure for discuss_post
-- ----------------------------
DROP TABLE IF EXISTS `discuss_post`;
CREATE TABLE `discuss_post` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(45) NOT NULL COMMENT '用户id,关联user',
  `title` varchar(100) DEFAULT NULL COMMENT '帖子的标题',
  `content` text COMMENT '帖子的内容',
  `type` int(11) DEFAULT '0' COMMENT '0 普通,1置顶',
  `status` int(11) DEFAULT '0' COMMENT '帖子的状态,0 正常、1精华、2拉黑',
  `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '帖子创建时间',
  `comment_count` int(11) DEFAULT NULL COMMENT '帖子评论数量',
  `score` double DEFAULT NULL COMMENT '帖子的分数,用于后续帖子的排名',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of discuss_post
-- ----------------------------
INSERT INTO `discuss_post` VALUES ('1', '1', '帖子标题', '帖子内容', '0', '0', '2021-12-03 17:45:20', '2', '5');
INSERT INTO `discuss_post` VALUES ('2', '1', '面试', '百度面试内容', '0', '0', '2021-12-03 17:45:22', '0', '0');
INSERT INTO `discuss_post` VALUES ('3', '1', '实习', '百度实习内容', '0', '0', '2021-12-03 17:45:24', '0', '0');

-- ----------------------------
-- Table structure for login_ticket
-- ----------------------------
DROP TABLE IF EXISTS `login_ticket`;
CREATE TABLE `login_ticket` (
  `id` int(11) NOT NULL,
  `user_id` int(20) DEFAULT NULL COMMENT '登录用户id',
  `ticket` varchar(100) DEFAULT NULL COMMENT '登录凭证,随机字符串',
  `status` int(50) DEFAULT NULL COMMENT '登录状态:0 有效,1 无效',
  `expired` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '过期时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of login_ticket
-- ----------------------------

-- ----------------------------
-- Table structure for message
-- ----------------------------
DROP TABLE IF EXISTS `message`;
CREATE TABLE `message` (
  `id` int(11) NOT NULL,
  `from_id` int(50) DEFAULT NULL COMMENT '发消息的id,创建索引',
  `to_id` int(50) DEFAULT NULL COMMENT '收消息的id,创建索引',
  `conversation_id` int(50) DEFAULT NULL COMMENT '会话id,由通信双方id拼接,创建索引',
  `content` text COMMENT '消息内容',
  `status` int(50) DEFAULT NULL COMMENT '消息状态:0 未读,1 已读,2 删除',
  `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '消息发送时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of message
-- ----------------------------

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL COMMENT '用户id',
  `user_name` varchar(50) DEFAULT NULL COMMENT '用户名,创建索引',
  `password` varchar(100) DEFAULT NULL COMMENT '用户密码',
  `head_url` varchar(100) DEFAULT NULL COMMENT '用户头像地址',
  `salt` varchar(50) DEFAULT NULL COMMENT '盐',
  `email` varchar(50) DEFAULT NULL COMMENT '用户邮箱,创建索引',
  `type` int(50) DEFAULT '0' COMMENT '用户类型:0 普通,1 管理员, 2 版主',
  `status` int(50) DEFAULT '0' COMMENT '用户状态:0 未激活,1 已激活',
  `activation_code` varchar(50) DEFAULT NULL COMMENT '激活码',
  `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '注册时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '小明', '123', 'https://img.ivsky.com/img/tupian/li/202105/24/qiaokeliquqi-011.jpg', 'xxx', 'zqq@qq.com', '1', '1', 'xxxxxx', '2021-12-03 17:45:56');
INSERT INTO `user` VALUES ('2', '小张', '123', 'https://img.ivsky.com/img/tupian/li/202105/24/qiaokeliquqi-011.jpg', 'xxx', 'zqq@qq.com', '1', '1', 'xxxx', '2021-12-03 17:46:13');

2. 在IntelliJ IDEA中连接本地数据库(注意:根据提示设置serverTimezone)

3. 在pom.xml中添加mysql、mybatis依赖

<!--    mysql驱动    -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.25</version>
</dependency>
<!--    springboot集成mybatis依赖    -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>

4. 在src/main/resources目录下创建generatorMapper.xml文件(注意:如果generatorMapper.xml没有放在pom.xml同目录下,请指定generatorMapper.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>
    <!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径 -->
    <classPathEntry location="D:\workspace-git\mysql-connector-java-5.1.8.jar"/>
    <context id="default" targetRuntime="MyBatis3">
        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8"/>
        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
        <!-- JavaBean 实现 序列化 接口 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <!-- genenat entity时,生成toString -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>

        <!-- 是否去除自动生成的注释 true:是 : false:否 -->
        <commentGenerator>
            <property name="suppressAllComments" value="false"/>
            <!--生成的注释包含时间戳-->
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/community?characterEncoding=utf-8&amp;useSSL=false&amp;useInformationSchema=true"
                        userId="root"
                        password="root">
            <property name="useInformationSchema" value="false"/>
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Entity类存放位置-->
        <javaModelGenerator targetPackage="zqq.trys.community.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置(Mapper)-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="zqq.trys.community.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <!--    schema 数据库名    tableName 表名 domainObjectName 实体类名  方法一般不生成,设为false-->
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
        </table>

        <table tableName="comment" domainObjectName="Comment"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

        <table tableName="discuss_post" domainObjectName="DiscussPost"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

        <table tableName="login_ticket" domainObjectName="LoginTicket"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="message" domainObjectName="Message"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

5. 在pom.xml中添加mybatis代码自动生成插件

<!--      mybatis代码自动生成插件      -->
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.5</version>
    <configuration>
        <!--配置文件的位置 -->
        <configurationFile>src/main/resources/generatorMapper.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
</plugin>

6. 配置好maven插件后,重新加载maven(红框中选择其一),Plugins中就会出现mybatis-generator插件

 7. 单击mybatis-generator:generate,便会生成实体类、Mapper和*Mapper.xml文件(不用提前创建好包名,会自动生成)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zqq_2016

有用的话,来打赏博主吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值