Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】

1. 新建springboot项目:

Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】_第1张图片

Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】_第2张图片

创建项目的时候将WEB相关的jar包引入,具体如下图

Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】_第3张图片

Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】_第4张图片

点击完成之后新建项目如下:

Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】_第5张图片

2. 添加数据源

在application.properties文件中添加如下配置项:

spring.datasource.url=jdbc:mysql://localhost:3306/demo001?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true

此时会报如下错误:Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】_第6张图片

在pom.xml文件中添加数据源连接依赖,上面的错误提示就消失。

        
            mysql
            mysql-connector-java
            5.1.47
        

 

 

3. 引入mybatis generator插件

在pom.xml文件中添加如下配置:

            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
                
                    
                    src/main/resources/generatorConfig.xml
                    true
                    true
                
                
                    
                        Generate MyBatis Artifacts
                        
                            generate
                        
                    
                
                
                    
                        org.mybatis.generator
                        mybatis-generator-core
                        1.3.5
                    
                
            
在resources目录相面创建generatorConfig.xml配置文件,配置文件内容如下:




    
    
    
    

        
            
            
        
        
        
        
        
        
            
        

        
        
            
            
            
            
        
        
        
            
            
        
        
        
            
            
        
        
        
        

注意             location="E:/workspaces/maven/localRepository/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar"/>

这里需要根据自己的实际情况配置

generatorConfig.xml配置文件刚刚创建好之后会有下面的报错:

Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】_第7张图片

针对上面的报错只需要在pom.xml文件中添加如下依赖即可

        
            org.mybatis.generator
            mybatis-generator-core
            1.3.5
            provided
        

4. 创建表结构

在mysql中新建数据库:

Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】_第8张图片

在demo001中创建如下表表结构。

Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】_第9张图片

建表脚本如下:

/*
Navicat MySQL Data Transfer

Source Server         : local-db
Source Server Version : 80012
Source Host           : localhost:3306
Source Database       : demo001

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

Date: 2019-03-08 17:02:19
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for order_detail
-- ----------------------------
DROP TABLE IF EXISTS `order_detail`;
CREATE TABLE `order_detail` (
  `detail_id` varchar(32) NOT NULL,
  `order_id` varchar(32) NOT NULL,
  `product_id` varchar(32) NOT NULL,
  `product_name` varchar(64) NOT NULL COMMENT '商品名称',
  `product_price` decimal(8,2) NOT NULL COMMENT '商品单价',
  `product_quantity` int(11) NOT NULL COMMENT '商品数量',
  `product_icon` varchar(512) DEFAULT NULL COMMENT '商品图片',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`detail_id`),
  KEY `idx_order_id` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='订单详情表';

-- ----------------------------
-- Records of order_detail
-- ----------------------------
INSERT INTO `order_detail` VALUES ('123456', '1232457', '123456', '米饭', '3.50', '5', 'http://xxxx.jpg', '2018-06-09 19:09:59', '2018-06-09 19:09:59');
INSERT INTO `order_detail` VALUES ('1528550976711891529', '1528550976288278577', '123457', '猪肉', '13.50', '1', 'http://xxxx.jpg', '2018-06-09 21:29:36', '2018-06-09 21:29:36');
INSERT INTO `order_detail` VALUES ('1528551420523701313', '1528551420077261427', '123456', '米饭', '3.50', '20', 'http://xxxx.jpg', '2018-06-09 21:37:00', '2018-06-09 21:37:00');
INSERT INTO `order_detail` VALUES ('1528551420560542384', '1528551420077261427', '123457', '猪肉', '13.50', '20', 'http://xxxx.jpg', '2018-06-09 21:37:00', '2018-06-09 21:37:00');
INSERT INTO `order_detail` VALUES ('1528567256972410899', '1528567256596679399', '123456', '米饭', '3.50', '20', 'http://xxxx.jpg', '2018-06-10 02:00:57', '2018-06-10 02:00:57');
INSERT INTO `order_detail` VALUES ('1528567257038728332', '1528567256596679399', '123457', '猪肉', '13.50', '20', 'http://xxxx.jpg', '2018-06-10 02:00:57', '2018-06-10 02:00:57');
INSERT INTO `order_detail` VALUES ('1528567611673430696', '1528567558709217255', '123456', '米饭', '3.50', '21', 'http://xxxx.jpg', '2018-06-10 02:07:04', '2018-06-10 02:07:04');
INSERT INTO `order_detail` VALUES ('1528567827118238739', '1528567823793330643', '123456', '米饭', '3.50', '21', 'http://xxxx.jpg', '2018-06-10 02:10:27', '2018-06-10 02:10:27');
INSERT INTO `order_detail` VALUES ('1528568250844874326', '1528568250772812293', '123456', '米饭', '3.50', '21', 'http://xxxx.jpg', '2018-06-10 02:17:30', '2018-06-10 02:17:30');
INSERT INTO `order_detail` VALUES ('1528597123769198210', '1528597123682698937', '123456', '米饭', '3.50', '5', 'http://xxxx.jpg', '2018-06-10 10:18:44', '2018-06-10 10:18:44');
INSERT INTO `order_detail` VALUES ('1528597123905891240', '1528597123682698937', '123457', '猪肉', '13.50', '5', 'http://xxxx.jpg', '2018-06-10 10:18:44', '2018-06-10 10:18:44');
INSERT INTO `order_detail` VALUES ('1528597123928299665', '1528597123682698937', '12344455', '三生三世', '2.50', '5', 'http://xxxx.jpg', '2018-06-10 10:18:44', '2018-06-10 10:18:44');
INSERT INTO `order_detail` VALUES ('1528597458657441319', '1528597458646619370', '123456', '米饭', '3.50', '5', 'http://xxxx.jpg', '2018-06-10 10:24:18', '2018-06-10 10:24:18');
INSERT INTO `order_detail` VALUES ('1528597458695997797', '1528597458646619370', '123457', '猪肉', '13.50', '5', 'http://xxxx.jpg', '2018-06-10 10:24:18', '2018-06-10 10:24:18');
INSERT INTO `order_detail` VALUES ('1528597458727306750', '1528597458646619370', '12344455', '三生三世', '2.50', '5', 'http://xxxx.jpg', '2018-06-10 10:24:18', '2018-06-10 10:24:18');
INSERT INTO `order_detail` VALUES ('1528597896228543307', '1528597896211793442', '123456', '米饭', '3.50', '5', 'http://xxxx.jpg', '2018-06-10 10:31:36', '2018-06-10 10:31:36');
INSERT INTO `order_detail` VALUES ('1528597896260558997', '1528597896211793442', '123457', '猪肉', '13.50', '5', 'http://xxxx.jpg', '2018-06-10 10:31:36', '2018-06-10 10:31:36');
INSERT INTO `order_detail` VALUES ('1528597896279905387', '1528597896211793442', '12344455', '三生三世', '2.50', '5', 'http://xxxx.jpg', '2018-06-10 10:31:36', '2018-06-10 10:31:36');
INSERT INTO `order_detail` VALUES ('1528608013775724790', '1528608013765692486', '123456', '米饭', '3.50', '5', 'http://xxxx.jpg', '2018-06-10 13:20:13', '2018-06-10 13:20:13');
INSERT INTO `order_detail` VALUES ('1528608013787905126', '1528608013765692486', '123457', '猪肉', '13.50', '5', 'http://xxxx.jpg', '2018-06-10 13:20:13', '2018-06-10 13:20:13');
INSERT INTO `order_detail` VALUES ('1528608013799648794', '1528608013765692486', '12344455', '三生三世', '2.50', '5', 'http://xxxx.jpg', '2018-06-10 13:20:13', '2018-06-10 13:20:13');
INSERT INTO `order_detail` VALUES ('1528608099572701570', '1528608099538818299', '123456', '米饭', '3.50', '5', 'http://xxxx.jpg', '2018-06-10 13:21:39', '2018-06-10 13:21:39');
INSERT INTO `order_detail` VALUES ('1528608099589736231', '1528608099538818299', '123457', '猪肉', '13.50', '5', 'http://xxxx.jpg', '2018-06-10 13:21:39', '2018-06-10 13:21:39');
INSERT INTO `order_detail` VALUES ('1528608099598889972', '1528608099538818299', '12344455', '三生三世', '2.50', '5', 'http://xxxx.jpg', '2018-06-10 13:21:39', '2018-06-10 13:21:39');
INSERT INTO `order_detail` VALUES ('1528610189405446126', '1528610188699835274', '123456', '米饭', '3.50', '5', 'http://xxxx.jpg', '2018-06-10 13:56:29', '2018-06-10 13:56:29');
INSERT INTO `order_detail` VALUES ('1528610189521630932', '1528610188699835274', '123457', '猪肉', '13.50', '5', 'http://xxxx.jpg', '2018-06-10 13:56:30', '2018-06-10 13:56:30');
INSERT INTO `order_detail` VALUES ('1528610189530925777', '1528610188699835274', '12344455', '三生三世', '2.50', '5', 'http://xxxx.jpg', '2018-06-10 13:56:30', '2018-06-10 13:56:30');
INSERT INTO `order_detail` VALUES ('1528622345533161423', '1528622345480683338', '123456', '米饭', '3.50', '5', 'http://xxxx.jpg', '2018-06-10 17:19:05', '2018-06-10 17:19:05');
INSERT INTO `order_detail` VALUES ('1528622345546657285', '1528622345480683338', '123457', '猪肉', '13.50', '5', 'http://xxxx.jpg', '2018-06-10 17:19:05', '2018-06-10 17:19:05');
INSERT INTO `order_detail` VALUES ('1528622345552697788', '1528622345480683338', '12344455', '三生三世', '2.50', '5', 'http://xxxx.jpg', '2018-06-10 17:19:05', '2018-06-10 17:19:05');
INSERT INTO `order_detail` VALUES ('1528622768069962321', '1528622768026101715', '123456', '米饭', '3.50', '5', 'http://xxxx.jpg', '2018-06-10 17:26:08', '2018-06-10 17:26:08');
INSERT INTO `order_detail` VALUES ('1528622768083623194', '1528622768026101715', '123457', '猪肉', '13.50', '5', 'http://xxxx.jpg', '2018-06-10 17:26:08', '2018-06-10 17:26:08');
INSERT INTO `order_detail` VALUES ('1528622768091257630', '1528622768026101715', '12344455', '三生三世', '2.50', '5', 'http://xxxx.jpg', '2018-06-10 17:26:08', '2018-06-10 17:26:08');
INSERT INTO `order_detail` VALUES ('1528622841426964017', '1528622841408887757', '123456', '米饭', '3.50', '5', 'http://xxxx.jpg', '2018-06-10 17:27:21', '2018-06-10 17:27:21');
INSERT INTO `order_detail` VALUES ('1528622841438756096', '1528622841408887757', '123457', '猪肉', '13.50', '5', 'http://xxxx.jpg', '2018-06-10 17:27:21', '2018-06-10 17:27:21');
INSERT INTO `order_detail` VALUES ('1528622841445288698', '1528622841408887757', '12344455', '三生三世', '2.50', '5', 'http://xxxx.jpg', '2018-06-10 17:27:21', '2018-06-10 17:27:21');

/*
Navicat MySQL Data Transfer

Source Server         : local-db
Source Server Version : 80012
Source Host           : localhost:3306
Source Database       : demo001

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

Date: 2019-03-08 17:02:29
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for order_master
-- ----------------------------
DROP TABLE IF EXISTS `order_master`;
CREATE TABLE `order_master` (
  `order_id` varchar(32) NOT NULL,
  `buyer_name` varchar(32) NOT NULL COMMENT '买家名字',
  `buyer_phone` varchar(32) NOT NULL COMMENT '买家电话',
  `buyer_address` varchar(128) NOT NULL COMMENT '买家地址',
  `buyer_openid` varchar(64) NOT NULL COMMENT '买家微信openid',
  `order_amount` decimal(8,2) NOT NULL COMMENT '订单总金额',
  `order_status` tinyint(3) NOT NULL DEFAULT '0' COMMENT '订单状态',
  `pay_status` tinyint(3) NOT NULL DEFAULT '0' COMMENT '订单支付状态',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`order_id`),
  KEY `idx_buyer_openid` (`buyer_openid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='订单表';

-- ----------------------------
-- Records of order_master
-- ----------------------------
INSERT INTO `order_master` VALUES ('1232456', '张三', '12345678901', '四川成都', 'zhangsan', '400.50', '0', '0', '2018-06-09 18:45:27', '2018-06-09 18:45:27');
INSERT INTO `order_master` VALUES ('1232457', '张三', '12345678901', '四川成都', 'zhangsan', '400.50', '0', '0', '2018-06-09 18:48:32', '2018-06-09 18:48:32');
INSERT INTO `order_master` VALUES ('1528550976288278577', '李四', '12345678900', '四川成都', 'lisi111', '13.50', '0', '1', '2018-06-09 21:29:36', '2018-06-09 23:48:58');
INSERT INTO `order_master` VALUES ('1528551420077261427', '王五', '12345678900', '四川成都', 'wangwu111', '340.00', '2', '0', '2018-06-09 21:37:00', '2018-06-09 23:07:48');
INSERT INTO `order_master` VALUES ('1528567256596679399', '王五', '12345678900', '四川成都', 'wangwu111', '340.00', '0', '0', '2018-06-10 02:00:57', '2018-06-10 02:00:57');
INSERT INTO `order_master` VALUES ('1528567558709217255', '李四', '12345678900', '四川成都', 'lisi11111', '73.50', '0', '0', '2018-06-10 02:07:04', '2018-06-10 02:07:04');
INSERT INTO `order_master` VALUES ('1528567823793330643', '李四', '12345678900', '四川成都', 'lisi11111', '73.50', '2', '0', '2018-06-10 02:10:27', '2018-06-10 13:16:01');
INSERT INTO `order_master` VALUES ('1528568250772812293', '李四', '12345678900', '四川成都', 'lisi11111', '73.50', '0', '0', '2018-06-10 02:17:31', '2018-06-10 02:17:31');
INSERT INTO `order_master` VALUES ('1528597123682698937', '李四', '12345678900', '四川成都', 'lisi11111', '97.50', '2', '0', '2018-06-10 10:18:44', '2018-06-10 13:04:41');
INSERT INTO `order_master` VALUES ('1528597458646619370', '李四', '12345678900', '四川成都', 'lisi11111', '97.50', '2', '0', '2018-06-10 10:24:18', '2018-06-10 17:21:51');
INSERT INTO `order_master` VALUES ('1528597896211793442', '李四', '12345678900', '四川成都', 'lisi11111', '97.50', '2', '0', '2018-06-10 10:31:36', '2018-06-10 17:26:08');
INSERT INTO `order_master` VALUES ('1528608013765692486', '李四', '12345678900', '四川成都', 'lisi11111', '97.50', '2', '0', '2018-06-10 13:20:13', '2018-06-10 17:27:21');
INSERT INTO `order_master` VALUES ('1528608099538818299', '李四', '12345678900', '四川成都', 'lisi11111', '97.50', '0', '0', '2018-06-10 13:21:39', '2018-06-10 13:21:39');
INSERT INTO `order_master` VALUES ('1528610188699835274', '李四', '12345678900', '四川成都', 'lisi11111', '97.50', '0', '0', '2018-06-10 13:56:30', '2018-06-10 13:56:30');
INSERT INTO `order_master` VALUES ('1528622345480683338', '李四', '12345678900', '四川成都', 'lisi11111', '97.50', '0', '0', '2018-06-10 17:19:05', '2018-06-10 17:19:05');
INSERT INTO `order_master` VALUES ('1528622768026101715', '李四', '12345678900', '四川成都', 'lisi11111', '97.50', '0', '0', '2018-06-10 17:26:08', '2018-06-10 17:26:08');
INSERT INTO `order_master` VALUES ('1528622841408887757', '李四', '12345678900', '四川成都', 'lisi11111', '97.50', '0', '0', '2018-06-10 17:27:21', '2018-06-10 17:27:21');


/*
Navicat MySQL Data Transfer

Source Server         : local-db
Source Server Version : 80012
Source Host           : localhost:3306
Source Database       : demo001

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

Date: 2019-03-08 17:02:37
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for product_category
-- ----------------------------
DROP TABLE IF EXISTS `product_category`;
CREATE TABLE `product_category` (
  `category_id` int(11) NOT NULL AUTO_INCREMENT,
  `category_name` varchar(64) NOT NULL COMMENT '类目名字',
  `category_type` int(11) NOT NULL COMMENT '类目编号',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='类目表';

-- ----------------------------
-- Records of product_category
-- ----------------------------
INSERT INTO `product_category` VALUES ('1', '热销榜', '1', '2018-05-28 22:53:02', '2018-05-28 23:13:30');
INSERT INTO `product_category` VALUES ('2', '女生最爱112', '2', '2018-05-28 23:08:49', '2018-05-28 23:08:49');
INSERT INTO `product_category` VALUES ('3', '男生最爱', '4', '2018-05-28 23:26:37', '2018-05-28 23:26:37');


/*
Navicat MySQL Data Transfer

Source Server         : local-db
Source Server Version : 80012
Source Host           : localhost:3306
Source Database       : demo001

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

Date: 2019-03-08 17:02:58
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for product_info
-- ----------------------------
DROP TABLE IF EXISTS `product_info`;
CREATE TABLE `product_info` (
  `product_id` varchar(32) NOT NULL,
  `product_name` varchar(64) NOT NULL COMMENT '商品名称',
  `product_price` decimal(8,2) NOT NULL COMMENT '商品单价',
  `product_stock` int(11) NOT NULL COMMENT '库存',
  `product_description` varchar(64) DEFAULT NULL COMMENT '描述',
  `product_icon` varchar(512) DEFAULT NULL COMMENT '小图',
  `product_status` int(11) NOT NULL DEFAULT '0' COMMENT '状态:正常0,下架1.',
  `category_type` int(11) NOT NULL COMMENT '类目编号',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='商品表';

-- ----------------------------
-- Records of product_info
-- ----------------------------
INSERT INTO `product_info` VALUES ('12344455', '三生三世', '2.50', '5', '555', 'http://xxxx.jpg', '0', '2', '2018-06-09 12:08:33', '2018-06-10 17:27:21');
INSERT INTO `product_info` VALUES ('123456', '米饭', '3.50', '13', '米饭很好吃', 'http://xxxx.jpg', '0', '1', '2018-05-29 23:36:49', '2018-06-10 17:27:21');
INSERT INTO `product_info` VALUES ('123457', '猪肉', '13.50', '53', '猪肉很好吃', 'http://xxxx.jpg', '0', '2', '2018-05-30 00:20:56', '2018-06-10 17:27:21');

5. 自动生成代码

Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】_第10张图片

代码成功生成

Springboot工具篇01:在idea中使用mybatis-generator之一【探索笔记】_第11张图片

至此,已经实现了在idea中创建springboot项目并使用mybatis-generator插件自动生成相关代码,可以免去很多繁杂的代码编写,为开发人员节约一定的时间。工欲善其事必先利其器……没有什么创新的,只是想记录一下,权当自己的笔记。

你可能感兴趣的:(mybatis,工具)