hibernate使用步骤
1导入jar包
2.在src文件下创建hibernate.cfg.xml文件,用来编写配置
hibernate.cfg.xml配置
至少5句话
mysql数据库驱动
<proprrty name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
数据库名称
<proprrty name="hibernate.connection.url">jdbc:mysql:///test</property>
数据库用户名
<proprrty name="hibernate.connection.username">root</property>
数据库密码
<proprrty name="hibernate.connection.password">123</property>
mapping resource(多个)
<mapping resource="com/example/hibernate/User.hbm.xml"/>
驱动
数据库路径
数据库用户名
数据库密码
加上mapping
3.创建对应的实体类
public Product() {
}
private Integer id;
private String name;
private Integer price;
private String description;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="price")
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + ", description=" + description + "]";
}
}
4.创建类和表的映射文件***.hbm.xml
<hibernate-mapping package="com.test.entity">
<class name="Product" table="products" lazy="true">
<!--id:主键 name:实体类中属性名 column:表中列名 -->
<id name="id" column="id" >
<generator class="identity">
</generator>
</id>
** <!--property 普通属性和列的对应 -->**
<property name="name" column="name" >
<!-- <column name="" sql-type=""></column> -->
</property>
<property name="price" column="price"></property>
<property name="description" column="description" ></property>
</class>
</hibernate-mapping>
<>
类名对应标名
<>
主键使用id标签
<>
**主键生成方式(自增长主键)
<>
非主键
<>
5.测试