Hibernate 3学习之入门案例

最近在学习java EE的一个持久层框架 Hibernate,刚写了一个入门级的案例,赶紧记录下来

话不多说,上项目…..

项目的结构
在此输入图片描述

接下来是 hibernate.cfg.cml ,可以说是核心配置文件了


            <?xml version='1.0' encoding='utf-8'?>
            <!DOCTYPE hibernate-configuration PUBLIC
                    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

            <hibernate-configuration>

                <session-factory>

                    <!-- Database connection settings -->
                    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
                    <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
                    <property name="connection.username">root</property>
                    <property name="connection.password">root@wangming</property>

                    <!-- JDBC connection pool (use the built-in) -->
                    <property name="connection.pool_size">1</property>

                    <!-- SQL dialect -->
                    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

                    <!-- Echo all executed SQL to stdout -->
                    <property name="show_sql">true</property>

                    <!-- Drop and re-create the database schema on startup -->
                    <property name="hbm2ddl.auto">none</property>

                    <mapping resource="com/wm/model/Account.cfg.xml"/>

                </session-factory>

            </hibernate-configuration>

next…..

Account.java


        package com.wm.model;

    public class Account {

        private Integer id;
        private String firstname;
        private String lastname;
        private String emailAddress;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getFirstname() {
            return firstname;
        }
        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }
        public String getLastname() {
            return lastname;
        }
        public void setLastname(String lastname) {
            this.lastname = lastname;
        }
        public String getEmailAddress() {
            return emailAddress;
        }
        public void setEmailAddress(String emailAddress) {
            this.emailAddress = emailAddress;
        }           
    }

Account.cfg.xml文件


        <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <hibernate-mapping package="com.wm.model">

        <class name="Account" table="account">
            <id name="id" column="id">
                <generator class="increment"/>
            </id>
            <property name="firstname" />
            <property name="lastname" />
            <property name="emailAddress" />
        </class>
    </hibernate-mapping>

这里的Account对应的是数据库的一张表
里面的字段

                 id                      int  
                 firstname           String 
                 lastname            String
                 emailAddress     String`

建好这张表,接下来就可以测试了

当然,因为时间原因,配置都很基础,但是运行还是没有问题的,可以作为查看hibernate效果的
一个小的测试吧,接下来我会继续深入下去

你可能感兴趣的:(Hibernate 3学习之入门案例)