标签:
<?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="hibernate.connection.username">root</property>
<!--密码 -->
<property name="hibernate.connection.password">root</property>
<!--url信息 -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/test
</property>
<!--数据库驱动信息 -->
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!--数据库方言信息 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 数据库表生成策略
create创建
update更新
-->
<property name="hbm2ddl.auto">update</property>
<!-- 是否打印SQL语句 -->
<property name="show_sql">true</property>
<!-- 是否格式化SQL语句 -->
<property name="format_sql">true</property>
<!-- sessionFactory.getCurrentSession()所需 -->
<property name="hibernate.current_session_context_class">thread</property>
<!--指定Hibernate映射文件需要关联的映射对象 -->
<mapping resource="com/foo/hibernate/bean/student.hbm.xml" />
</session-factory>
</hibernate-configuration>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 对应表STUDENT -->
<class name="com.foo.hibernate.bean.Student" table="STUDENT">
<!-- hibernate方言会将java类型转换成数据库数据类型 -->
<id name="id" type="int">
<!-- 字段 -->
<column name="ID" />
<!-- 生成策略
native由数据库生成,如果创建时设置值也会忽视
-->
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
</class>
</hibernate-mapping>
package com.lee.hibernate.bean;
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
标签:
原文地址:http://blog.csdn.net/ro_bot/article/details/51332579