简体   繁体   English

在Java中使用Hibernate连接到mysql DB

[英]Connecting to mysql DB using hibernate in java

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();
        }
    }
}

sample input 样本输入

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));

My problem is, that I want to map the above code to mysql DB using hibernate .I just know basic of hibernate and mysql. 我的问题是,我想使用hibernate将以上代码映射到mysql DB。我只知道hibernate和mysql的基本知识。 How can I achieve this and what steps do i have to follow ?. 我怎样才能做到这一点,我必须遵循什么步骤? thanks in advance 提前致谢

You need a configuration file in your src folder (if you're using Netbeans). 您需要在src文件夹中配置文件(如果使用的是Netbeans)。

This file needs to be called hibernate.cfg.xml and its content to connect to a MySQL database would be: 该文件需要称为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>

Don't copy-past and test as it is. 不要照原样复制和测试。 You'll need to change some values in your hibernate.cfg.xml file. 您需要在hibernate.cfg.xml文件中更改一些值。

Also, you need to decide if you're going to use annotations or mapping files for mapping your objects. 另外,您需要确定是否要使用注释映射文件来映射对象。

Please, refer to Hibernate's Official documentation for that what I said and more. 请参阅有关我所说的内容以及更多内容的Hibernate官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM