gpt4 book ai didi

java - 在java中使用hibernate连接到mysql数据库

转载 作者:行者123 更新时间:2023-11-29 22:23:15 25 4
gpt4 key购买 nike

public class Tree<I, A> {
private final HashMap<I, Node<I, A>> map = new HashMap<>();
private final Node<I, A> root;

public Tree(I id, A value) {
root = new Node<>(id, value);
map.put(id, root);
}

public void addChild(I parentId, I id, A value) {
Node<I, A> parent = map.get(parentId);
Node<I, A> child = new Node<>(id, value);
parent.children.add(child);
map.put(id, child);
}

public A getById(I id) {
return map.get(id).value;
}

public String subtreeToString(I id) {
return map.get(id).toString();
}

private static class Node<I, A> {
private final I id;
private final A value;
private final ArrayList<Node<I, A>> children = new ArrayList<>();

private Node(I id, A value) {
this.id = id;
this.value = value;
}

private void print(int depth, PrintWriter pw) {
for (int i = 0; i < depth; i++) {
pw.print("\t");
}
pw.println("[" + id + ", " + value + "]");
for (Node<I, A> child : children) {
child.print(depth + 1, pw);
}
}

@Override
public String toString() {
StringWriter writer = new StringWriter();
print(0, new PrintWriter(writer));
return writer.toString();
}
}
}

示例输入

Tree<Integer, String> tree = new Tree<>(1, "Bob");
tree.addChild(1, 2, "John");
tree.addChild(1, 3, "James");
tree.addChild(2, 4, "David");
tree.addChild(2, 5, "Alice");

System.out.println(tree.subtreeToString(1));
System.out.println(tree.subtreeToString(2));

我的问题是,我想使用 hibernate 将上面的代码映射到 mysql DB。我只知道 hibernate 和 mysql 的基础知识。我怎样才能实现这一目标以及我必须遵循哪些步骤?提前致谢

最佳答案

您的 src 文件夹中需要一个配置文件(如果您使用的是 Netbeans)。

此文件需要命名为 hibernate.cfg.xml,其连接到 MySQL 数据库的内容为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Conection string for a MySQL DB -->
<property name="connection.url">jdbc:mysql://localhost:3306/your_db_name</property>
<!-- Your DB username -->
<property name="connection.username">db_username</property>
<!-- Your password DB username -->
<property name="connection.password">password_db_username</property>
<!-- Default schema -->
<property name="default_schema">public</property>
<!-- Hibernate dialect for MySQL DB -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Shows SQL instructions in shell when you run your program and do any CRUD operation into the DB -->
<property name="show_sql">true</property>
<!-- Updates DB schema on startup -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>

不要照原样复制粘贴并进行测试。您需要更改 hibernate.cfg.xml 文件中的一些值。

此外,您需要决定是否要使用 annotationsmapping files用于映射您的对象。

请引用Hibernate's Official documentation为此我所说的以及更多。

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

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