[color=red]映射文件是联系java实体类 和实体类对应的数据表 的纽带[/color]
用映射文件 来联系实体类 和实体类所对应的表
把映射文件放到类的同名包下,xml格式 映射文件
这个映射文件和类同名
类 Customer.java
映射文件名 Customer.hbm.xml
dtd文件在hibernate-mapping-3.0.dtd
<hibernate-mapping>
//注意类java类名是全路径.类名 而且区分大小写,而表名是不区分大小写
<class name="cn.xiaoxian.hibernate.domain.Customer" table="customer">
//name的值是实体类的属性名 column的值是表的字段名 type的值是 映射类型
<id name="id" column="id" type="integer"> 区分大小写的 java类型是Integer 字段类型是int
<generator class="increment"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="integer"/>
</class>
</hibernate-mapping>
-------------------------------------------
指定 src下hibernate属性文件 hibernate.prpperties
指定连接信息什么的
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/hibernate
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect //方言类
hibernate.show_sql=true
------------------------
public class App{
public static void main (Sring[] args){
//载入hibernate.properties 属性信息载入进内存
Configuration conf = new Configuration();
// 添加映射文件
conf.addClass(Customer.class);
//会话工厂 相当于数据源 或连接池
SessionFactory sf = conf.buildSessionFactory();
//开启会话 相当于connection 开启和数据库之间的会话
Customer c = new Customer();
//开启事物
Transaction tx = s.beginTransaction();
Customer c = new Customer();
c.setName("tom");
c.setAge(12);
//保存
s.save(c);
tx.commit();
s.close();
}
}
用映射文件 来联系实体类 和实体类所对应的表
把映射文件放到类的同名包下,xml格式 映射文件
这个映射文件和类同名
类 Customer.java
映射文件名 Customer.hbm.xml
dtd文件在hibernate-mapping-3.0.dtd
<hibernate-mapping>
//注意类java类名是全路径.类名 而且区分大小写,而表名是不区分大小写
<class name="cn.xiaoxian.hibernate.domain.Customer" table="customer">
//name的值是实体类的属性名 column的值是表的字段名 type的值是 映射类型
<id name="id" column="id" type="integer"> 区分大小写的 java类型是Integer 字段类型是int
<generator class="increment"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="integer"/>
</class>
</hibernate-mapping>
-------------------------------------------
指定 src下hibernate属性文件 hibernate.prpperties
指定连接信息什么的
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/hibernate
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect //方言类
hibernate.show_sql=true
------------------------
public class App{
public static void main (Sring[] args){
//载入hibernate.properties 属性信息载入进内存
Configuration conf = new Configuration();
// 添加映射文件
conf.addClass(Customer.class);
//会话工厂 相当于数据源 或连接池
SessionFactory sf = conf.buildSessionFactory();
//开启会话 相当于connection 开启和数据库之间的会话
Customer c = new Customer();
//开启事物
Transaction tx = s.beginTransaction();
Customer c = new Customer();
c.setName("tom");
c.setAge(12);
//保存
s.save(c);
tx.commit();
s.close();
}
}