SpringBoot-mybatis的代码生成器EasyCode使用示例

一、环境准备:

1. 开发工具IDE:InterlliJ IDEA ;

2. 数据库:MySQL;

3. InterlliJ IDEA 安装好插件:Easy Code;

4. 启用IDEA 连接数据库;(如下)

第一步:

 

 

 

第二步:

 

 

 

第三步:

 

 

 

 

 

 第四步:

 

 

 

 第五步:我们用 user 表来演示:

user表的数据:

 

 

 

二、代码示例:

1. IDEA创建工程SpringBoot工程(自行百度)

2. 导入依赖包:(我的POM.xml文件如下)

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.study.module</groupId>
    <artifactId>mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis</name>
    <description>Demo project for Spring Boot: ORM Framework Mybatis</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!--MySQL数据库连接-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--阿里巴巴连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.22</version>
        </dependency>
        <!--实体类:Lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

 

3. 修改 SpringBoot的配置文件 application.properties → application.yaml:(我的application.yaml文件内容如下)

server:
  port: 8089
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/zero?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root123456
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:/mapper/*Dao.xml
  typeAliasesPackage: com.study.module.mybatis.entity

4. 项目初始结构图:

 

 

 5. 开始使用 Easy Code 生成代码:

第一步:

 

 

 第二步:

 

 

 第三步:

 

 

 第四步:

 

 

 第五步:

找到 UserDao,给接口加入 @Mapper 

 

 

 第六步:

 

 

 第七步:

 

 

 第八步:启动项目

 

 

 

三、结果验证:

1. 打开项目中的 UserController.java 文件,找到访问路径为:http://localhost:8089/user/selectOne?id=2

 

 2. 优化UserController的API请求规范,使得请求URL为  : http://localhost:8089/user/selectOne/2

 

 修改的UserController如下:

 

 四、感谢浏览!😊

 

posted @ 2020-04-11 23:22  superdrew  阅读(2820)  评论(0编辑  收藏  举报