手动配置 Hibernate

注意:hibernate.cfg.xml一定是src包下,和cn文件夹同级。而 User.java和User.hbm.xml一定是对应的

 

看看“hibernate.cfg.xml”的配置

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<!-- 数据库URL -->
		<property name="connection.url">
			jdbc:oracle:thin:@localhost:1521:oracle11
		</property>
		<!-- 数据库用户 -->
		<property name="connection.username">
			A_hr
		</property>
		<!-- 数据库用户密码 -->
		<property name="connection.password">
			123456
		</property>
		<!-- 数据库 JDBC 驱动 -->
		<property name="connection.driver_class">
			oracle.jdbc.driver.OracleDriver
		</property>
		<!-- 是否将运行期生成的 SQL 输出到日志以供调试  -->
		<property name="show_sql">true</property>
		<!-- 每个数据库都有其对应的 Dialect 以匹配其平台特征 -->
		<property name="dialect">
			org.hibernate.dialect.Oracle10gDialect
		</property>
		<mapping resource="cn/entity/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

 如图:

 

然后写“User.java”实体类

package cn.entity;

/**
 * 用户表实体类
 */
public class User {
	
	private int id;//编号
	private String name;//姓名
	private int age;//年龄
	private double hight;//身高
	
	/*
	 * 构造方法
	 * */
	public User() {
	}
	
	public User(String name, int age, double hight) {
		this.name = name;
		this.age = age;
		this.hight = hight;
	}
	/*
	 * 封装方法
	 * */
	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 double getHight() {
		return hight;
	}
	public void setHight(double hight) {
		this.hight = hight;
	}
	
}

 

然后,配置“User.java”实体类,配置名为“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>
    <class name="cn.entity.User" table="t_user" >
        <id name="id" type="java.lang.Integer">
        	<generator class="sequence">
        		<param name="sequence">seq_t_user</param>
        	</generator>
        </id>
        <property name="name"  type="java.lang.String">
        	<column name="name"></column>
        </property>
        <property name="age"  type="java.lang.Integer">
        	<column name="age"></column>
        </property>
        <property name="hight"  type="java.lang.Double">
        	<column name="hight"></column>
        </property>
    </class>
    </hibernate-mapping>

 

最后:写上测试类的代码

package cn.test;


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import cn.entity.User;

public class Test {
	public static void main(String[] args) {
		add();
	}
	private static void add(){
		//构造配置对象,并且读取配置
		Configuration cfg = new Configuration().configure();
		//SessionFactory = 管理连接工厂
		SessionFactory sf = cfg.buildSessionFactory();
		//Session连接
		Session session = sf.openSession();
		//增删改用事务
		Transaction tx = null;
		try {
			tx=session.beginTransaction();
			User user = new User("何开", 25, 1.72);
			//面向对象 == 不用SQL
			session.save(user);
			tx.commit();
			System.out.println("保存成功!!!");
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}finally{
			session.close();
			sf.close();
		}
	}
}

 

效果图:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值