CRUD是指在做计算处理时增加、查询、更新和删除几个单词的首字母简写。主要被用在描述软件系统中数据库或者持久层的基本操作功能。下面示例演示了如何创建session实例,并使用session的相关方法,实现对数据库表记录的添加。修改。删除和查找。
1、 首先登录MySQL数据库,建立表box,SQL语句如下:
create table box(
id int(11) not null auto_increment,
width float default null,
length float default null,
height float default null,
name varchar(20) default null,
primary key(id));
2、 Hibernate 配置文件hibernate.cfg.xml
<?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>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="crud/Box.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3、Java 对应的类Box.java
package crud;
public class Box {
private Integer id;
private float width;
private float length;
private float height;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getLength() {
return length;
}
public void setLength(float length) {
this.length = length;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4、Box类的配置文件box.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="crud.Box" table="box">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="native"/>
</id>
<property name="width" type="java.lang.Float">
<column name="width" precision="12" scale="0"/>
</property>
<property name="length" type="java.lang.Float">
<column name="length" precision="12" scale="0"/>
</property>
<property name="height" type="java.lang.Float">
<column name="height" precision="12" scale="0"/>
</property>
<property name="name" type="java.lang.String">
<column name="name" length="20"/>
</property>
</class>
</hibernate-mapping>