目录
1.编写数据库的表和实体类
create database test;
create table customer(
id int primary key auto_increment,
name varchar(20),
age int,
city varchar(20)
);
package com.qwl.domain;
public class Customer {
private Integer id;
private String name;
private Integer age;
private String sex;
private String city;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
//重写Object类下的toString()方法
public String toString(){
return "Customer [id="+id+",name="+name+",age="+age+",sex="+sex+",city="+city+"]";
}
}
2.创建一个类与表的映射文件
1.名称
- 类名.hbm.xml-----------Customer.hbm.xml
2.位置
- 与实体类在同一个包下
3.约束
- hibernate3.jar/hibernate/hibernate-mapping-3.0.dtd文件中定义
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
4.元素
<hibernate-mapping>
<class name="com.qwl.domain.Customer" table="customer"><!-- name代表的是实体类名,table代表的是表名 -->
<id name="id" column="id"><!-- name代表的是customer类中的属性,column=id代表的是表中的字段 -->
<generator class="native"/><!-- 主键生成策略 -->
</id>
<!-- 其他属性使用proper标签来映射 -->
<property name="age" column="age" type="int"/><!-- 基于java写法 -->
<property name="name" column="name" type="string"/><!-- 基于hibernate写法 -->
<property name="city">
<column name="city" sql-type="varchar(20)"></column>
</property><!-- 基于mysql写法 -->
</class>
</hibernate-mapping>
3.创建一个hibernate配置文件
hibernate.cfg.xml放置在src下
- 位置:src下
- 名称:hibernate.cfg.xml
- 约束:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- 元素:
关于元素配置时<property name="">
name的值可以在参考hibernate/project/etc/hibernate.properties文件中
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">qaz123</property>
<!-- 指定方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 用来关联hbm配置文件 -->
<mapping resource="com/qwl/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.编写代码,执行crud操作
模板
- 1.得到配置对象
configuration config=new Configuration().config();
- 2,得到一个SessionFactory
SessionFactory factory=config.buildSessionFactory().
- 3,得到一个Session
Session session=factory.openSession();
- 4.开启事务
Transaction t=session.beginTransaction();
- 5,执行操作--具体的crud操作
- 6、提交事务
t. commit(t);
- 7.释放资源
session.close ();
factory.close ();
//插入
public void inset(Session session){
Customer c =new Customer();
c.setName("张三");
c.setAge(20);
c.setCity("北京");
session.save(c);
}
//修改
public void update(Session session){
Customer c =new Customer();
c.setName("李四");
c.setAge(20);
c.setCity("广州");
c.setId(1);
session.update(c);
}
//查询
public void get(Session session){
//对于hibrnate中根据id查找操作,它可以使用两个方法get和load
//Customer c =(Customer) session.get(Customer.class, 1);
Customer c =(Customer) session.load(Customer.class, 1);
}
//删除
public void delete(Session session){
Customer c =(Customer) session.load(Customer.class, 1);//先查询
session.delete(c);//删除
}