SpringBoot19 利用mybatis自动生成项目

Posted 寻渝记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot19 利用mybatis自动生成项目相关的知识,希望对你有一定的参考价值。

 

1 环境配置

   =

 

2 新建一个新的springboot项目

  

  2.1 选择一些必要的依赖

    web jpa mysql

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.test.demo</groupId>
    <artifactId>mybatis_demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mybatis_demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
View Code

  2.2 添加mybatis生成代码所需的相关

<!-- Mybatis-Plus  自动生成实体类-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.test.demo</groupId>
    <artifactId>mybatis_demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mybatis_demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Mybatis-Plus  自动生成实体类-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
View Code

  2.3 配置数据源

server:
  servlet:
    context-path: /dev
  port: 9999

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/testdemo?useUnicode=true&characterEncoding=UTF-8&&useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
#  jpa:
#    database: mysql
#    properties:
#      hibernate:
#        show-sql: true
#        format-sql: true
  jpa:
    database: mysql
    show-sql: true

  2.4 编写一个测试控制层

    

package cn.test.demo.mybatis_demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王杨帅
 * @create 2018-04-06 21:11
 * @desc 测试控制类
 **/
@RestController
@RequestMapping(value = "/test")
public class TestContorller {

    @GetMapping(value = "test01")
    public String test01() {
        String result = "===test01===";
        System.out.println(result);
        return result;
    }
}
View Code

  2.5 编写代生成器

    需要根据自己需要更改生成文件存放位置,以及一些其他信息

package cn.test.demo.mybatis_demo.util;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @author 王杨帅
 * @create 2018-04-06 21:43
 * @desc 自动生成代码工具类
 **/
public class AutoGenerateCode {
    public static void main(String[] args) throws InterruptedException {
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置定义
        GlobalConfig gc = new GlobalConfig();

        gc.setOutputDir("F:\\\\javaProgramming\\\\springBoot\\\\testDemo\\\\mybatis_demo\\\\src\\\\main\\\\java\\\\cn\\\\test\\\\demo\\\\mybatis_demo\\\\util"); // 设置存储路径
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
//        gc.setEnableCache(true);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setAuthor("王杨帅"); // 作者信息

        // 自定义文件命名,注意 %s 会自动填充表实体属性!
        gc.setMapperName("%sDao");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc); // 设置全局配置

        // 数据源配置定义
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        /*dsc.setTypeConvert(new MySqlTypeConvert(){
            // 自定义数据库表字段类型转换【可选】
            @Override
            public DbColumnType processTypeConvert(String fieldType) {
                System.out.println("转换类型:" + fieldType);
                return super.processTypeConvert(fieldType);
            }
        });*/
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://localhost:3306/testdemo?useUnicode=true&amp;characterEncoding=UTF-8&amp;generateSimpleParameterMetadata=true");
        dsc.setUsername("root");
        dsc.setPassword("182838");
        mpg.setDataSource(dsc); // 设置数据源

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
        // strategy.setTablePrefix(new String[] { "tlog_", "tsys_" });// 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        // strategy.setInclude(new String[] { "user" }); // 需要生成的表
        // strategy.setExclude(new String[]{"test"}); // 排除生成的表
        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("org");
        pc.setModuleName("ibase4j");
        mpg.setPackageInfo(pc);

        // 执行生成
        mpg.execute();
    }
}
View Code

  2.6 创建表

/*
Navicat MySQL Data Transfer

Source Server         : mysql5.4
Source Server Version : 50540
Source Host           : localhost:3306
Source Database       : testdemo

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

Date: 2018-04-08 12:54:08
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `equipment_check_item`
-- ----------------------------
DROP TABLE IF EXISTS `equipment_check_item`;
CREATE TABLE `equipment_check_item` (
  `id_` bigint(20) NOT NULL AUTO_INCREMENT COMMENT \'记录ID\',
  `eci_code` bigint(20) NOT NULL COMMENT \'点检项目编号\',
  `et_code` bigint(20) NOT NULL COMMENT \'项目类型编号\',
  `eci_name` varchar(60) NOT NULL COMMENT \'点检项目名称\',
  `eci_order` mediumint(4) NOT NULL COMMENT \'点检项目顺序\',
  `enable_` tinyint(1) NOT NULL COMMENT \'记录有效性\',
  `remark_` varchar(100) NOT NULL,
  `create_by` bigint(20) NOT NULL,
  `create_time` datetime NOT NULL,
  `update_by` bigint(20) NOT NULL,
  `update_time` datetime NOT NULL,
  PRIMARY KEY (`id_`),
  UNIQUE KEY `eci_code` (`eci_code`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of equipment_check_item
-- ----------------------------
INSERT INTO `equipment_check_item` VALUES (\'1\', \'20180001\', \'20180001\', \'油量检查\', \'1\', \'1\', \'测试\', \'1\', \'2018-04-01 19:05:10\', \'1\', \'2018-04-04 19:05:23\');

-- ----------------------------
-- Table structure for `equipment_check_resord`
-- ----------------------------
DROP TABLE IF EXISTS `equipment_check_resord`;
CREATE TABLE `equipment_check_resord` (
  `id_` bigint(20) NOT NULL AUTO_INCREMENT COMMENT \'记录ID\',
  `erc_code` bigint(20) NOT NULL COMMENT \'设备点检记录编码\',
  `ei_code` bigint(20) NOT NULL COMMENT \'点检设备编码\',
  `ci_code` bigint(20) NOT NULL COMMENT \'点检职员编码\',
  `erc_result` tinyint(1) NOT NULL DEFAULT \'6\' COMMENT \'点检结果:0正常 1故障 2报废 3待报废 4停用 5未使用 6待检\',
  `erc_work` tinyint(1) NOT NULL DEFAULT \'0\' COMMENT \'点检后是否可作业:1可接收作业 0不可接收作业\',
  `erc_date` datetime NOT NULL COMMENT \'点检日期\',
  `enable_` tinyint(1) NOT NULL DEFAULT \'1\' COMMENT \'记录状态:0无效,1有效\',
  `remark_` varchar(100) DEFAULT NULL COMMENT \'备注信息\',
  `create_by` bigint(20) NOT NULL COMMENT \'点检记录创建者\',
  `create_time` datetime NOT NULL COMMENT \'点检记录创建时间\',
  `update_by` bigint(20) NOT NULL COMMENT \'点检记录更新者\',
  `update_time` datetime NOT NULL COMMENT \'点检记录更新时间\',
  PRIMARY KEY (`id_`),
  UNIQUE KEY `erc_code` (`erc_code`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of equipment_check_resord
-- ----------------------------
INSERT INTO `equipment_check_resord` VALUES (\'1\', \'2018040501\', \'2018040501\', \'2018040501\', \'6\', \'0\', \'2018-04-01 13:51:41\', \'1\', null, \'2018040501\', \'2018-04-05 13:51:53\', \'2018040501Spring boot入门:SpringBoot集成结合AdminLTE(Freemarker),利用generate自动生成代码,利用DataTable和PageHelper进行分页显示(示例(代码

使用MyBatis Generator自动创建代码

SpringBoot整合ORM开发框架MyBatis和JPA

SSM框架——使用MyBatis Generator自动创建代码

记一次IDEA利用mybatis-generator自动生成dao和mapper

19.springboot+mybatis多数据源