Java 中的 Mysql 查询



我有一个来自 netbeans 中生成的数据库的服务休息。这将为每个表 创建实体类。

@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
        @NamedQuery(name = "Users.findAll", 
                query = "SELECT u FROM Users u"),
        @NamedQuery(name = "Users.findById", 
                query = "SELECT u FROM Users u WHERE u.id = :id"),
        @NamedQuery(name = "Users.findByName", 
                query = "SELECT u FROM Users u WHERE u.name = :name"),
        @NamedQuery(name = "Users.findByTelephone", 
                query = "SELECT u FROM Users u WHERE u.telephone = :telephone"),
        @NamedQuery(name = "Users.findByYear", 
                query = "SELECT u FROM Users u WHERE u.year = :year")})
public class Users implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Integer id;
    @Size(max = 25)
    @Column(name = "name")
    private String name;
    @Column(name = "telephone")
    private Integer telephone;
    @Column(name = "year")
    private Integer year;
    public Users() {
    }
    public Users(Integer id) {
        this.id = id;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getTelephone() {
        return telephone;
    }
    public void setTelephone(Integer telephone) {
        this.telephone = telephone;
    }
    public Integer getYear() {
        return year;
    }
    public void setYear(Integer year) {
        this.year = year;
    }
    @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 Users)) {
            return false;
        }
        Users other = (Users) 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 "glee.Users[ id=" + id + " ]";
    }

如何从另一个类查询具有此类的数据库? ?例如,做一个选择:从bb.users中选择name;并将其保存在数组中,例如谢谢

@NamedQuery(name,query)注释用于对查询进行硬编码 进入持久性类。query您可以使用 name指定的属性。

使用休眠会话时,可以使用 getNamedQuery 来 获取查询。

实体管理器一起使用时,您可以使用 创建命名查询

这两者都将检索查询,并且您可以使用基本查询操作执行查询。

列出用户 = session.getNamedQuery("Users.findById"(.setParameter("id", "1"(.list((;

相关内容

  • 没有找到相关文章

最新更新