Git Product home page Git Product logo

jpa-gen-demo's Introduction

使用 idea 反向生成实体

生成文件效果

选择的jpagendemo作为生成目录生成的文件结构

  • entity
package com.marioplus.jpagendemo.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import com.marioplus.jpagendemo.common.BaseEntity;
import javax.persistence.*;
import lombok.EqualsAndHashCode;
import lombok.Data;

import java.io.Serializable;


/**
 * 用户表
 *
 * @author auto generated
 * @date 2020-01-31 01:02:41
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "user")
@ApiModel(value = "用户表")
public class User extends BaseEntity implements Serializable {

	/**
	 * 姓名
	 * nullable : true
	 * default  : null
	 */
	@ApiModelProperty(value = "姓名")
	@Column(name = "name", nullable = true, length = 20)
	private String name;

	/**
	 * 年龄
	 * nullable : true
	 * default  : null
	 */
	@ApiModelProperty(value = "年龄")
	@Column(name = "age", nullable = true, length = 11)
	private Integer age;

	/**
	 * 联系方式
	 * nullable : true
	 * default  : null
	 */
	@ApiModelProperty(value = "联系方式")
	@Column(name = "mobile", nullable = true, length = 11)
	private String mobile;

	/**
	 * 性别
	 * nullable : true
	 * default  : null
	 */
	@ApiModelProperty(value = "性别")
	@Column(name = "sex", nullable = true)
	private String sex;
}
  • service
package com.marioplus.jpagendemo.service;

import com.marioplus.jpagendemo.repository.UserRepository;
import com.marioplus.jpagendemo.common.BaseService;
import com.marioplus.jpagendemo.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * User service层
 *
 * @author auto generated
 * @date 2020-01-31 01:02:41
 */
@Service
public class UserService extends BaseService<User, Long> {

	@Resource
	private UserRepository rep;
}
  • repository
package com.marioplus.jpagendemo.repository;

import com.marioplus.jpagendemo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * User Repository层
 *
 * @author auto generated
 * @date 2020-01-31 01:02:41
 */
public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {

}
  • repositoryCustom
package com.marioplus.jpagendemo.repository;

/**
 * User 自定义Repository层
 *
 * @author auto generated
 * @date 2020-01-31 01:02:41
 */
public interface UserRepositoryCustom {

}
  • repositoryCustomImpl
package com.marioplus.jpagendemo.repository.impl;

import com.marioplus.jpagendemo.repository.UserRepositoryCustom;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * User 自定义Repository实现层
 *
 * @author auto generated
 * @date 2020-01-31 01:02:41
 */
@Repository
public class UserRepositoryCustomImpl implements UserRepositoryCustom {

	@PersistenceContext
	private EntityManager em;
}

如何使用

如果需要使用 lombok 和 swagger 生成配置需要加入相应的 maven 依赖

  1. view -> Tool Windows -> Database

  1. + -> Data source -> MySQL

image.png

  1. 配置数据库信息

image.png

  1. 在database视图区域任意地方右键,然后 Scripted Extensions -> Go to Scripts Directory

image.png

image.png

  1. 复制jpa-auto-generate.groovy到4中跳转的文件夹schema

  2. database视图区域选择你想要生成的表,然后Scripted Extensions -> jpa-auto-generate.groovy 可以使用ShiftCtrl多选

image.png

  1. 弹出的文件选择框中,选择生成位置

image.png

详细配置

配置在生成文件的 config

config = [
        // 生成开关
        generate: [
                entity          : true,
                service         : true,
                repository      : true,
                repositoryCustom: true
        ],
        // 实体生成设置
        entity  : [
                // 继承父类设置
                parent         : [
                        // 是否继承父类
                        enable    : true,
                        // 父类名称
                        name      : "BaseEntity",
                        // 父类包名
                        package   : "com.marioplus.jpagendemo.common",
                        // 父类的属性,父类已有属性不在出现在生成的实体内
                        properties: ["id", "createDate", "lastModifiedDate", "version"],
                ],
                // 是否序列化
                impSerializable: true,
                // 是否生成 jpa 相关内容,设置为 false 可以生成与 jpa 无关的实体
                jpa            : true,
                // 是否生成 swagger 文档相关注解,相关说明来数据库注释
                useSwagger     : true,
                // 是否使用 lombok 注解代替 get、set方法
                useLombok      : true
        ],
        // service 生成设置
        service : [
                // 参照 entity 部分的 parent
                parent: [
                        enable : true,
                        name   : "BaseService",
                        package: "com.marioplus.jpagendemo.common"
                ]
        ]
]
  1. 使用 lombok 或者 swagger 需要添加相关 maven 依赖
  2. 重复生成会覆盖之前生成的

jpa-gen-demo's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.