自己亲测的东西才是最有效果的,下面贴出整个编写的过程。
1 hibernate环境搭建,这个博客非常给力:http://www.111cn.net/wy/js-ajax/93142.htm
需要用到的jar包:
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配置文件:
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<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">root</property>
<property name="connection.password">1q2w3e4r5t</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">hibernate</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 如果spring没有配置事务,这里可以设置自动提交 -->
<property name="hibernate.connection.autocommit">true</property>
<!-- pojo映射 -->
<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配置:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<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中添加:
<!-- 如果spring没有配置事务,这里可以设置自动提交 -->
<property name="hibernate.connection.autocommit">true</property>