IDEA+EasyCode實現代碼生成


IDEA+EasyCode實現代碼生成

Easy Code介紹

EasyCode是基於IntelliJ IDEA開發的代碼生成插件,支持自定義任意模板(Java,html,js,xml)。只要是與數據庫相關的代碼都可以通過自定義模板來生成。支持數據庫類型與java類型映射關系配置。支持同時生成生成多張表的代碼。每張表有獨立的配置信息。完全的個性化定義,規則由你設置。

搭建步驟

第一步:打開IntelliJ IDEA 新建一個maven工程,不勾選骨架

新建maven工程

配置項目的groupid、artifactid、version(自定義)

點擊下一步Next配置項目的groupid、artifactid、version

選擇工程存放目錄

選擇工程存放目錄

第二步:下載安裝EasyCode插件

file->settings->plugins 搜索Easy Code
在這里插入圖片描述
搜索到后點擊Install 我這里安裝過了 安裝完成會讓你重啟IDEA。
如何判斷是否安裝成功 file->settings->Other settings 看是否有Easy Code這個選項
判斷是否安裝成功

第三步:引入Easy Code模板 (可以根據個人情況定制 也可以使用默認的)

file->settings->Other settings->Template Setting在這里插入圖片描述

第四步:創建數據庫,數據表

在這里插入圖片描述

第五步:配置數據源(需要指定數據庫名稱,要生成數據表)

點擊IDEA右側的Datbase->點擊上方的加號->選擇Data Source.->Mysql在這里插入圖片描述

配置數據源

在這里插入圖片描述
注意:連接方式需要采用mysql8的連接方式,不然可能連接失敗
jdbc:mysql://localhost:3306/table?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false

連接成功如圖所示

在這里插入圖片描述

創建要相關的包,存放生成后的數據(以springboot項目為例)

在這里插入圖片描述

引入springboot的相關pom.xml依賴

<dependencies>
        <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>1.3.2</version>
        </dependency>
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>

需要用到mybatis的啟動器,所以也一起引用

打開Easy Code插件 選要生成的pojo,mapper,service

右擊表名->Easy Code->Generate Code
在這里插入圖片描述
在這里插入圖片描述
Module:當前項目模塊
package:生成代碼存放包的位置
Template:生成的模板

勾選All表示生成所有,勾選禁止提示,防止彈出很多個提示信息,點擊OK

在這里插入圖片描述
pojo*(entity):Brand.java代碼:

package com.dj.entity;

import java.io.Serializable;

/**
 * (Brand)實體類
 *
 * @author joker_dj
 * @since 2020-03-24 11:18:22
 */
public class Brand implements Serializable {
    private static final long serialVersionUID = 689051521458334271L;
    /**
    * ID
    */
    private Integer bid;
    /**
    * 品牌名稱
    */
    private String bname;
    /**
    * 日期
    */
    private String ctime;

    
    public Integer getBid() {
        return bid;
    }

    public void setBid(Integer bid) {
        this.bid = bid;
    }
    
    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }
    
    public String getCtime() {
        return ctime;
    }

    public void setCtime(String ctime) {
        this.ctime = ctime;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "bid=" + bid +
                "bname=" + bname +
                "ctime=" + ctime +
                 '}';      
    }
}

service接口:BrandService.java 代碼如下:

package com.dj.service;

import com.dj.entity.Brand;
import java.util.List;

/**
 * @InterfaceName BrandService
 * @Description (Brand)表服務接口
 * @author joker_dj
 * @date 2020-03-24 11:18:22
 * @Version 1.0
 **/
public interface BrandService {

    /**
     * @Description 添加Brand
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param brand 實例對象
     * @return 是否成功
     */
    boolean insert(Brand brand);

    /**
     * @Description 刪除Brand
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param bid 主鍵
     * @return 是否成功
     */
    boolean deleteById(Integer bid);

    /**
     * @Description 查詢單條數據
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param bid 主鍵
     * @return 實例對象
     */
    Brand queryById(Integer bid);

    /**
     * @Description 查詢全部數據
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * 分頁使用MyBatis的插件實現
     * @return 對象列表
     */
    List<Brand> queryAll();

    /**
     * @Description 實體作為篩選條件查詢數據
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param brand 實例對象
     * @return 對象列表
     */
    List<Brand> queryAll(Brand brand);

    /**
     * @Description 修改數據,哪個屬性不為空就修改哪個屬性
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param brand 實例對象
     * @return 是否成功
     */
    boolean update(Brand brand);

}

serviceImpl 實現類:BrandServiceImpl.java代碼如下:

package com.dj.service.impl;

import com.dj.entity.Brand;
import com.dj.dao.BrandDao;
import com.dj.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

 /**
 * @ClassName BrandServiceImpl
 * @Description (Brand)表服務實現類
 * @author joker_dj
 * @date 2020-03-24 11:18:22
 * @Version 1.0
 **/
@Service("brandService")
public class BrandServiceImpl  implements BrandService {

    @Autowired
    protected BrandDao brandDao;

    /**
     * @Description 添加Brand
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param brand 實例對象
     * @return 是否成功
     */
    @Override
    public boolean insert(Brand brand) {
        if(brandDao.insert(brand) == 1){
            return true;
        }
        return false;
    }

    /**
     * @Description 刪除Brand
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param bid 主鍵
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer bid) {
        if(brandDao.deleteById(bid) == 1){
            return true;
        }
        return false;
    }

    /**
     * @Description 查詢單條數據
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param bid 主鍵
     * @return 實例對象
     */
    @Override
    public Brand queryById(Integer bid) {
        return brandDao.queryById(bid);
    }

    /**
     * @Description 查詢全部數據
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * 分頁使用MyBatis的插件實現
     * @return 對象列表
     */
    @Override
    public List<Brand> queryAll() {
        return brandDao.queryAll();
    }

    /**
     * @Description 實體作為篩選條件查詢數據
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param brand 實例對象
     * @return 對象列表
     */
    @Override
    public List<Brand> queryAll(Brand brand) {
        return brandDao.queryAll(brand);
    }

    /**
     * @Description 修改數據,哪個屬性不為空就修改哪個屬性
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param brand 實例對象
     * @return 是否成功
     */
    @Override
    public boolean update(Brand brand) {
        if(brandDao.update(brand) == 1){
            return true;
        }
        return false;
    }

}

dao層:BrandDao.java代碼如下

package com.dj.dao;

import com.dj.entity.Brand;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;

 /**
 * @InterfaceName BrandDao
 * @Description (Brand)表數據庫訪問層
 * @author joker_dj
 * @date 2020-03-24 11:18:22
 * @Version 1.0
 **/
@Mapper
public interface BrandDao {

    /**
     * @Description 添加Brand
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param brand 實例對象
     * @return 影響行數
     */
    int insert(Brand brand);
    
    /**
     * @Description 刪除Brand
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param bid 主鍵
     * @return 影響行數
     */
    int deleteById(Integer bid);

    /**
     * @Description 查詢單條數據
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param bid 主鍵
     * @return 實例對象
     */
    Brand queryById(Integer bid);

    /**
     * @Description 查詢全部數據
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * 分頁使用MyBatis的插件實現
     * @return 對象列表
     */
    List<Brand> queryAll();

    /**
     * @Description 實體作為篩選條件查詢數據
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param brand 實例對象
     * @return 對象列表
     */
    List<Brand> queryAll(Brand brand);

    /**
     * @Description 修改Brand
     * @author joker_dj
     * @date 2020-03-24 11:18:22
     * @param 根據brand的主鍵修改數據
     * @return 影響行數
     */
    int update(Brand brand);

}

mapper.xml代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dj.dao.BrandDao">

    <!--brand的映射結果集-->
    <resultMap type="com.dj.entity.Brand" id="BrandMap">
        <result property="bid" column="bid" jdbcType="INTEGER"/>
        <result property="bname" column="bname" jdbcType="VARCHAR"/>
        <result property="ctime" column="ctime" jdbcType="VARCHAR"/>
    </resultMap>
    
    <!--全部字段-->
    <sql id="allColumn"> bid, bname, ctime </sql>
    
    <!--添加語句的字段列表-->
    <sql id="insertColumn">
        <if test="bname != null and bname != ''">
                bname,
        </if>
        <if test="ctime != null and ctime != ''">
                ctime,
        </if>
    </sql>
    
    <!--添加語句的值列表-->
        <sql id="insertValue">
        <if test="bname != null and bname != ''">
                #{bname},
        </if>
        <if test="ctime != null and ctime != ''">
                #{ctime},
        </if>
    </sql>
    
    <!--通用對Brand各個屬性的值的非空判斷-->
    <sql id="commonsValue">
        <if test="bname != null and bname != ''">
                bname = #{bname},
        </if>
        <if test="ctime != null and ctime != ''">
                ctime = #{ctime},
        </if>
    </sql>
    
    <!--新增brand:哪個字段不為空就添加哪列數據,返回自增主鍵-->
    <insert id="insert" keyProperty="bid" useGeneratedKeys="true">
        insert into brand
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="insertColumn"/>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <include refid="insertValue"/>
        </trim>
    </insert>
   
    <!--刪除brand:通過主鍵-->
    <delete id="deleteById">
        delete from brand
        <where>
            bid = #{bid}
        </where>
    </delete>
    
    <!--查詢單個brand-->
    <select id="queryById" resultMap="BrandMap">
        select
        <include refid="allColumn"></include>
        from brand
        <where>
            bid = #{bid}
        </where>
    </select>

    <!--查詢所有brand-->
    <select id="queryAllByLimit" resultMap="BrandMap">
        select
        <include refid="allColumn"></include>
        from brand
    </select>

    <!--通過實體作為篩選條件查詢-->
    <select id="queryAll" resultMap="BrandMap">
        select
          <include refid="allColumn"></include>
        from brand
        <trim prefix="where" prefixOverrides="and" suffixOverrides=",">
            <include refid="commonsValue"></include>
        </trim>
    </select>

    <!--通過主鍵修改數據-->
    <update id="update">
        update brand
        <set>
            <include refid="commonsValue"></include>
        </set>
        <where>
            bid = #{bid}
        </where>
    </update>

</mapper>

第六步:運行並調用

注意:我的mapper.xml文件存放的位置是在resources目錄下,不會被掃描到,所以在pom.xml文件中配置一下,使mapper.xml能夠被掃描
<!--靜態資源放行-->
  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

完整的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 http://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.4.RELEASE</version>
    </parent>
    <groupId>com.easycode</groupId>
    <artifactId>easycode</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <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>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>


    <!--靜態資源放行-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

在resources目錄下創建springboot全局配置文件Application.properties

配置文件如下:

##配置數據驅動信息  (key固定)
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql:///brand
spring.datasource.username = root
spring.datasource.password =

#tomcat端口號
server.port=8081

##給mybatis的實體類取別名  typeAliasesPackage
mybatis.type-aliases-package=com.dj.entity
## mybatis :mapper存放位置
mybatis.mapper-locations:classpath*:mybatis/mapper/*.xml

創建controller調用方法實現業務

controller:

package com.dj.controller;

import com.dj.entity.Brand;
import com.dj.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class webController {
    @Autowired
    BrandService service;

    @RequestMapping("/showAll")
    public List<Brand> show(){
        List<Brand> brands = service.queryAll();
        System.out.println(brands);
        return brands;
    }
}

運行查看結果

瀏覽器輸入 localhost:8081/showAll
在這里插入圖片描述

教程結束,此教程根據上面一步一步來應該是沒有問題的,如果有疑難解決不了,歡迎評論區留言


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM