标签:hibernate hibernate.cfg.xml
1.hibernate是站在JDBC的基础上的框架,远比JDBC好用,它的的开发步骤如下:
(1)在工程下建一个lib包,导入用到的jar包
(2).写一个pojo类
(3).配置pojo类名.hbm.xml文件
(4).配置hibernate.cfg.xml文件
(5)写应用程序类
2.代码实战:
(1).导入如下的jar包,本人用数据库是MySQL
2.写一个POJO类
package cn.wwh.www.hibernate.aa.helloword;
/**
 *类的作用:
 *
 *
 *@author 一叶扁舟
 *@version 1.0
 *@创建时间: 2014-8-17   下午03:07:55
 */
public class User {
	private int id;
	private String name;
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return "User --> id=" + id + "\n name=" + name + "";
	}
}
<?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>
	<class name="cn.wwh.www.hibernate.aa.helloword.User" table="user">
		<id name="id" type="int" column="id">
			<generator class="native" />
		</id>
		<property name="name" type="string" not-null="true" column="name" />
	</class>
</hibernate-mapping>
4.配置hibernate.cfg.xml文件
<?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> <!-- 1、数据库信息:数据库方言(是一个类的全名)与数据库连接信息 --> <!-- 配置连接数据库所需的驱动 --> <span style="color:#ff0000;"><property name="connection.driver_class">com.mysql.jdbc.Driver</property></span> <!-- 连接数据库的数据库url --> <span style="color:#ff0000;"><property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property></span> <!-- 数据库的账号 --> <span style="color:#ff0000;"><property name="connection.username">root</property></span> <!-- 数据库的密码 --> <span style="color:#ff0000;"><property name="connection.password">wwh</property></span> <!-- 指定数据库的方言(本人用的是MySql) --> <span style="color:#ff0000;"><property name="dialect">org.hibernate.dialect.MySQL5Dialect</property></span> <!-- 2.配置其它 --> <!-- create: 先删表,再建表。 create-drop: 启动时建表,退出前删表。 update: 如果表结构不一致,就创建或更新。 validate: 启动时验证表结构,如果不致就抛异常。 --> <!-- 自动创建数据表 --> <property name="hbm2ddl.auto">update</property> <!-- 显示数据操作的sql语句 --> <span style="color:#3333ff;"><property name="show_sql">true</property></span> <!-- 格式化的显示sql语句 --> <span style="color:#3366ff;"><property name="format_sql">true</property></span> <!-- 3.导入映射配置文件 --> <span style="color:#ff0000;"><mapping resource="cn/wwh/www/hibernate/aa/helloword/User.hbm.xml"/></span> </session-factory> </hibernate-configuration>5.测试类:TestUser.java
package cn.wwh.www.hibernate.aa.helloword;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
/**
 *类的作用:
 * 
 * 
 *@author 一叶扁舟
 *@version 1.0
 *@创建时间: 2014-8-17 下午03:08:17
 */
public class TestUser {
	private static Configuration config;
	static {
		config = new Configuration();
		config.configure("hibernate.cfg.xml");
	}
	@Test
	public void testSave() throws Exception {
		Configuration config = new Configuration();
		// 默认加入hibernate.cfg.xml配置文件
		config.configure();
		SessionFactory factory = config.buildSessionFactory();
		Session session = factory.openSession();
		Transaction transaction = session.beginTransaction();
		for (int i = 0; i < 20; i++) {
			User user = new User();
			user.setName("无悔_一叶扁舟" + i);
			// 关闭session
			session.save(user);
		}
		// 事务提交
		transaction.commit();
		// 关闭session
		session.close();
		// 关闭工厂
		factory.close();
	}
	@Test
	public void testGet() throws Exception {
		SessionFactory factory = config.buildSessionFactory();
		Session session = factory.openSession();
		// 开始事务
		Transaction trans = session.beginTransaction();
		// 从数据库中获取id为1的User数据
		User user = (User) session.get(User.class, 1);
		// 输出到控制台上
		System.out.println(user);
		// 事务提交
		trans.commit();
		session.close();
		factory.close();
	}
}6.测试效果图:org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml异常,主要原因是:hibernate.cfg.xml中引用了错误的DTD文件径“http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd”
将这个文件路径改为:
“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”
hibernate的第一应用程序,布布扣,bubuko.com
标签:hibernate hibernate.cfg.xml
原文地址:http://blog.csdn.net/u011662320/article/details/38640273