gpt4 book ai didi

java - 使用mysql在hibernate中自动创建数据库

转载 作者:行者123 更新时间:2023-11-30 22:24:02 24 4
gpt4 key购买 nike

这是我的代码,我错了。在运行 Test.java 时出现“请求的资源不可用”之类的错误。我将所有这些文件保存在同一个包中

hibernate .cfg.xml

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/studentdb </property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="connection.pool_size"> 1</property>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>


<!-- List of XML mapping files -->
<mapping resource="student.hbm.xml"/>

</session-factory>
</hibernate-configuration>

测试.java

public class Test {

public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
cfg.buildSessionFactory();
}

}

student.hbm.xml

<hibernate-mapping>
<class name="com.javathub.Student" table="stu_details">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="name"></property>
<property name="branch"></property>
<property name="fee"></property>
</class>


</hibernate-mapping>

Student.java

public class Student {
private int id;
private String name;
private String branch;
private double fee;
public Student(){

}
public Student(int id, String name, String branch, double fee) {
super();
this.id = id;
this.name = name;
this.branch = branch;
this.fee = fee;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public double getFee() {
return fee;
}
public void setFee(double fee) {
this.fee = fee;
}

}

请帮助我,在此先感谢

最佳答案

保留src文件夹下的student.hbm.xml和hibernate.cfg.xml,不要在package里,重新运行。

关于java - 使用mysql在hibernate中自动创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35838847/

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