gpt4 book ai didi

java - java中的Mysql查询

转载 作者:行者123 更新时间:2023-11-29 08:03:08 24 4
gpt4 key购买 nike

我有一个来自 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 + " ]";
}

如何从另一个类查询数据库? ?例如,执行选择: select name from bb.users;并将其保存在数组中,例如谢谢

最佳答案

@NamedQuery(name,query) annotation is used to hard code the Queries into the Persistence class. You can retrieve the query using the name attribute specified.

When using hibernate session, you can use getNamedQuery to get the query.

When used with EntityManager you can use createNamedQuery.

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

List Users = session.getNamedQuery("Users.findById").setParameter("id", "1").list();

关于java - java中的Mysql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23287895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com