【问题标题】:Making java Entity classes from MySQL tables in netbenas从netbeans中的MySQL表制作java实体类
【发布时间】:2018-09-20 15:35:23
【问题描述】:

我有一个 MySQL 数据库,其中包含一些数据,我想将其转换为 java Entity 类。我有我的 Persistence.xml 文件,但我不确定我该怎么做。我可以对其进行逆向工程,并从我的 MySQL 工作台导出数据,但是在我与数据库建立连接之后,我可以使用 Netbeans 中的函数来完成此操作吗?

【问题讨论】:

    标签: java mysql netbeans


    【解决方案1】:

    NetBeans 有一个向导可以完全按照您的意愿行事:

    • Projects 面板中,在您的项目中选择要包含实体类的包。
    • 右键单击并从上下文菜单中选择 New > Entity Classes from Database...

    • 从数据库新建实体类向导运行。

    • 数据库表页面上:

      • 数据库连接下拉列表中选择您要使用的连接。
      • 可用表列表中选择要为其生成实体类的表。
      • 点击添加>全部添加>按钮将表格移动到Selected Tables列表,然后点击下一步 > 按钮。
    • 实体类 页面上选择您想要的任何选项,尽管默认值通常是合适的。点击下一步>按钮。
    • 映射选项 页面上选择您想要的任何选项,尽管默认值通常是合适的。点击完成按钮。

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

    /*
     * 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 + " ]";
        }
    
    }
    

    【讨论】:

    • 这正是我正在寻找的答案。谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-11-30
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    相关资源
    最近更新 更多