蚂蚁's profile阳光森林PhotosBlogListsMore Tools Help

Blog


    August, 2009

    MyEclipse+Hibernate 配置方法

    1.工具:myeclipse+hibernate2+mysql+mysql-connector-java-3.1.10-bin.jar(JDBC for mysql的驱动)

    数据库准备
    2.在mysql中建立一个数据库test:  create database test;
    3.建立user表
    CREATE TABLE USER (
        user_id CHAR(32) NOT NULL PRIMARY KEY,
        name VARCHAR(16) NOT NULL,
        sex CHAR(1),
        age INT
    );


    4.新建一个elipse项目test:“新建”-“java项目”(项目名称:test)
    5.右键点击项目test,“Myeclipse”-"Add Hibernate"-"Capabilities"
    6.在对话框中选中“add hibernate 2.1 libraries to project”;Libeary folder:"/lib";选中Append Hibernate 2.1 libraries to project classpath;其它默认,点下一步
    7.
    Configration File Path: /test/src
    Configration File Name: hibernate.cfg.xml
    {Use JDBC Driver连接}
    Database URL:jdbc:mysql://localhost:3306/test
    Driver Class:com.mysql.jdbc.Driver
    Username:你的mysql用户名
    Password:你的mysql密码
    选中Copy JDBC Driver and add to classpath...选项
    Hibernate Dialect:Mysql
    点下一步
    8.去掉Create SessionFactory Class选项,点完成
    9.mysql-connector-java-3.1.10-bin.jar到项目的lib目录下
    10.用记事本打开项目根目录下的.classpath文件,在<classpath>...</classpath>中加入一行
    <classpathentry kind="lib" path="lib/mysql-connector-java-3.1.10-bin.jar"/>
    11.在项目test上点右键,“刷新” (一定要作)
    12.在eclipse中src目录下建立User.java
    public class User {
        private String id;
        private String name;
        private char sex;
        private int age;
        public int getAge() {
            return age;
        }
        public String getId() {
            return id;
        }
        public String getName() {
            return name;
        }
        public char getSex() {
            return sex;
        }
        public void setAge(int i) {
            age = i;
        }
        public void setId(String string) {
            id = string;
        }
        public void setName(String string) {
            name = string;
        }
        public void setSex(char c) {
            sex = c;
        }
    }
    13.在src中建立User.hbm.xml文件
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping
        PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    <hibernate-mapping>
        <class name="User" table="USER">
            <id name="id" type="string" unsaved-value="null">
                <column name="user_id" sql-type="char(32)" />
                <generator class="uuid.hex"/>
            </id>
            <property name="name" type="string" not-null="true">
                <column name="name" length="16" not-null="true"/>
            </property>
            <property name="sex" type="char"/>
            <property name="age" type="int"/>
        </class>
    </hibernate-mapping>
    14.在src中建立test.java文件
    import net.sf.hibernate.*;
    import net.sf.hibernate.cfg.*;
    public class test {
        public static void main(String[] args) throws HibernateException {
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            User user = new User();
            user.setName("caterpillar");
            user.setSex('M');
            user.setAge(28);
            Session session = sessionFactory.openSession();
            Transaction tx= session.beginTransaction();
            session.save(user);
            tx.commit();
            session.close();
            sessionFactory.close();
            System.out.println("新增資料OK!請先用MySQL觀看結果!");
        }
    }
    15.修改hibernate.cfg.xml(注意其中的mysql用户名和密码要修改)
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
    <!-- DO NOT EDIT: This is a generated file that is synchronized -->
    <!-- by MyEclipse Hibernate tool integration.                   -->
    <hibernate-configuration>
        <session-factory>
            <!-- properties -->
            <property name="connection.username">你的mysql用户名</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
            <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
            <property name="connection.password">你的mysql密码</property>
            <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
            <!-- mapping files -->
            <!-- The following mapping element was auto-generated in -->
            <!-- order for this file to conform to the Hibernate DTD -->
            <mapping resource="User.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
    16.在项目test上点右键,“刷新”
    17.双击test.java,运行:“运行”-“运行方式”-“java应用程序”
    18.eclipse提示结果:
    log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    新增資料OK!請先用MySQL觀看結果!
    19.mysql的test库中增加了一行。

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://antsdong.spaces.live.com/blog/cns!CF43121027CEF87E!338.trak
    Weblogs that reference this entry
    • None