程序员的资源宝库

网站首页 > gitee 正文

IDEA使用Easy Code插件生成代码

sanyeah 2024-04-02 19:56:45 gitee 11 ℃ 0 评论

Easy Code可以根据表自动生成代码,如controller、entity、server、serverImpl、dao、dao.xml等常用的类和代码,可以大大节省时间,提高编码效率


 1.(IDEA)File->Settings->Plugins,搜索"Easy Code",然后安装

2.安装后重启IDEA,使用IDEA-Database连接数据库

 

输入数据库连接信息,点击Test Connection测试连接

3.首先需要创建表,然后选中"表"名(user表为例)右击,点击EasyCode,再点击Generate code

选择生成代码的Package路径,和需要生成代码的模板Template,点击OK即可

 4.生成代码后的样例

 5.简单测试

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.3.0.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

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

</project>

application.yml

server:
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/coolw?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: 123456

mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  type-aliases-package: com.example.demo.entity

启动类添加注解@MapperScan("com.example.demo.dao")

@MapperScan("com.example.demo.dao")
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

启动服务,访问 http://localhost:8080/user/1,成功展示(图中JSON格式的数据,是在谷歌浏览器中安装了JSON插件【JSON-handle_0.5.6.crx】)

 

 6.扩充:如果项目中使用了mybatis-plus,也可以将生成的模板改成mybatis-plus,会以mybatis-plus的格式生成文件

(IDEA)File->Settings->Other Settings->EasyCode->Template。选择生成代码对应的模板,然后将Group Name选择"MybatisPlus"即可。例如将entity改成以mybatis-plus模板生成代码,

 7.扩充:设置生成模板代码的java类型和数据库类型匹配规则

(IDEA)File->Settings->Other Settings->EasyCode->Type Mapper

 

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表