Hibernate配置XML连接数据库
一、hibernate.cfg.xml
1、配置连接Oracle
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hbm2ddl.auto">create</property> <property name="dialect"> org.hibernate.dialect.Oracle9Dialec </property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE </property> <property name="connection.username">system</property> <property name="connection.password">root</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="format_sql">true</property> <property name="show_sql">true</property> <mapping resource="cn/wym/hibernate/Users.hbm.xml" /> </session-factory> </hibernate-configuration>
2、配置连接MySQL
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="hbm2ddl.auto">update</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.url">jdbc:mysql:///test</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="myeclipse.connection.profile">MysqlConnection</property> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping resource="cn/wym/hiberate/domain/User.hbm.xml" /> </session-factory> </hibernate-configuration>
3、配置连接Microsoft SQL 2005
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="hbm2ddl.auto">create</property> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=testhjxc</property> <property name="connection.username">wym</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <property name="myeclipse.connection.profile">MS2005</property>
<property name="format_sql">true</property> <property name="show_sql">true</property>
<mapping resource="cn/wym/hiberate/domain/User.hbm.xml" /> </session-factory> </hibernate-configuration>
二、cn/wym/hiberate/domain/User.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!--该文件要清楚地表述出 类 和 表 的对应关系--> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.wym.hiberate.domain"> <class name="User" table="user"> <id name="id" column="id"> <!-- 映射主键 --> <generator class="native"> </generator> </id> <property name="name"/> <property name="birthday"/> </class> </hibernate-mapping>
三、cn/wym/hiberate/domain/User.java
package cn.wym.hiberate.domain; import java.util.Date; public class User { private String name; private int id; private Date birthday; /** * 必须要有的 */ public User() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
漫人生路上,该放下的要放下,往事如过眼烟云,浅笑则安...