简单的hibernate环境搭建、自动生成model/配置/hibernate.xml配置文件

自己亲测的东西才是最有效果的,下面贴出整个编写的过程。
1 hibernate环境搭建,这个博客非常给力:http://www.111cn.net/wy/js-ajax/93142.htm
需要用到的jar包:
简单的hibernate环境搭建、自动生成model/配置/hibernate.xml配置文件_第1张图片

2 使用myeclipse自动生成model/model配置文件/hibernate.xml配置文件,详细步骤点开下面链接:http://jingyan.baidu.com/article/27fa7326e9ef8b46f8271f2a.html
讲解一下大概步骤:
(1)在DB Browser界面生成与数据库的链接
(2)MyEclipse Java Enterprise界面增加hibernate 功能
(3)生成实体类、实体类配置文件、Dao文件、hibernate.xml配置文件
(4)修改Dao文件
具体贴出hibernate.xml配置文件:




<hibernate-configuration>

    <session-factory>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/hibernate
        property>
        <property name="connection.username">rootproperty>
        <property name="connection.password">1q2w3e4r5tproperty>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        property>
        <property name="myeclipse.connection.profile">hibernateproperty>
        <property name="show_sql">trueproperty>
        <property name="format_sql">trueproperty>
        
        <property name="hibernate.connection.autocommit">trueproperty>
        
        <mapping resource="com/zx/model/Customer.hbm.xml" />
    session-factory>

hibernate-configuration>

model类(当然,这里就需要在MySql中创建一个Customer表):

public class Customer implements java.io.Serializable {
    // Constructors
    private Integer cid;
    private String username;
    private String password;
    //省略set、get
}

model配置:




<hibernate-mapping>
    <class name="com.zx.model.Customer" table="customer" catalog="hibernate">
        <id name="cid" type="java.lang.Integer">
            <column name="CID" />
            <generator class="native" />
        id>
        <property name="username" type="java.lang.String">
            <column name="USERNAME" length="12" not-null="true" />
        property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" length="12" />
        property>
    class>
hibernate-mapping>

dao层:

public class CustomerDAO{
    private static final Log log = LogFactory.getLog(CustomerDAO.class);

    public static void main(String[] args) {
        Customer customer = new Customer();
        customer.setUsername("张三");
        customer.setPassword("123123");
        CustomerDAO customerDAO = new CustomerDAO();
        customerDAO.save(customer);
    }

    public void save(Customer transientInstance) {
        log.debug("saving Customer instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
}

通过上面的代码,就可以实现存储了,相信删除、更改都不难了。
在以上环境搭建和测试过程中我遇到的问题:
1 存储时sql语句打印出来了,却没有存储到数据库中,很有可能是事物并没有提交,需要在hibernate.xml中添加:


<property name="hibernate.connection.autocommit">trueproperty>

你可能感兴趣的:(hibernate学习笔记)