首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从netbenas中的MySQL表生成java实体类

从netbenas中的MySQL表生成java实体类
EN

Stack Overflow用户
提问于 2018-09-20 23:35:24
回答 1查看 698关注 0票数 0

我有一个MySQL数据库,其中包含一些数据,我希望将其转换为java实体类。我有我的Persistence.xml文件,但我不确定如何才能做到这一点。我可以对其进行反向工程,并从我的MySQL工作台导出数据,但是在与数据库建立连接之后,我是否可以使用Netbeans中的函数来完成此操作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-21 04:03:07

NetBeans提供了一个向导,可以执行您想要的操作:

  • Projects面板中,在项目中选择要包含实体class(es).
  • Right的包,然后从上下文菜单中选择“新建”>“entity Classes from Database...”。

  • 将运行从数据库新建实体类向导。数据库表页上的

<代码>F220

代码语言:javascript
复制
- Select the connection you want to use from the **Database Connection** drop list. 
- Select the table(s) you want to generate entity classes for from the **Available Tables** list.
- Click the **Add >** or **Add All >** button to move the table(s) to the **Selected Tables** list, then click the **Next >** button.

  • 在Entity Classes页面上选择所需的任何选项,尽管默认值通常是适当的。单击Next >按钮。
  • 在 Options页上选择所需的任何选项,但默认值通常是适当的。单击Finish按钮。

运行向导后,将在您选择的包中创建实体类。例如:

代码语言:javascript
复制
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mysql8demo;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author johndoe
 */
@Entity
@Table(name = "country")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Country.findAll", query = "SELECT c FROM Country c")
    , @NamedQuery(name = "Country.findById", query = "SELECT c FROM Country c WHERE c.id = :id")
    , @NamedQuery(name = "Country.findByCountryCode", query = "SELECT c FROM Country c WHERE c.countryCode = :countryCode")
    , @NamedQuery(name = "Country.findByCountryName", query = "SELECT c FROM Country c WHERE c.countryName = :countryName")
    , @NamedQuery(name = "Country.findByCurrencyCode", query = "SELECT c FROM Country c WHERE c.currencyCode = :currencyCode")
    , @NamedQuery(name = "Country.findByPopulation", query = "SELECT c FROM Country c WHERE c.population = :population")
    , @NamedQuery(name = "Country.findByCapital", query = "SELECT c FROM Country c WHERE c.capital = :capital")
    , @NamedQuery(name = "Country.findByContinent", query = "SELECT c FROM Country c WHERE c.continent = :continent")
    , @NamedQuery(name = "Country.findByAreaInSqKm", query = "SELECT c FROM Country c WHERE c.areaInSqKm = :areaInSqKm")})
public class Country implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "countryCode")
    private String countryCode;
    @Basic(optional = false)
    @Column(name = "countryName")
    private String countryName;
    @Column(name = "currencyCode")
    private String currencyCode;
    @Column(name = "population")
    private String population;
    @Column(name = "capital")
    private String capital;
    @Column(name = "continent")
    private String continent;
    @Column(name = "areaInSqKm")
    private Integer areaInSqKm;

    public Country() {
    }

    public Country(Integer id) {
        this.id = id;
    }

    public Country(Integer id, String countryCode, String countryName) {
        this.id = id;
        this.countryCode = countryCode;
        this.countryName = countryName;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public String getCurrencyCode() {
        return currencyCode;
    }

    public void setCurrencyCode(String currencyCode) {
        this.currencyCode = currencyCode;
    }

    public String getPopulation() {
        return population;
    }

    public void setPopulation(String population) {
        this.population = population;
    }

    public String getCapital() {
        return capital;
    }

    public void setCapital(String capital) {
        this.capital = capital;
    }

    public String getContinent() {
        return continent;
    }

    public void setContinent(String continent) {
        this.continent = continent;
    }

    public Integer getAreaInSqKm() {
        return areaInSqKm;
    }

    public void setAreaInSqKm(Integer areaInSqKm) {
        this.areaInSqKm = areaInSqKm;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Country)) {
            return false;
        }
        Country other = (Country) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "mysql8demo.Country[ id=" + id + " ]";
    }

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52428654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档