基础

hibernate.cfg.xml

<?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>

		<!-- Database connection settings -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/shop</property>
		<property name="connection.username">root</property>
		<property name="connection.password">123456</property>

		<!-- SQL dialect -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">true</property>
		<!-- 添加java类和数据库表的映射 也可以在Configure对象处加载可多个-->
		<mapping resource="com/iotek/basic/pojo/Student.hbm.xml"/>
	</session-factory>

</hibernate-configuration>

Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.iotek.basic.pojo">

	<class name="Student" table="student">
		<!-- type属性的值有两种方式:
						java方式:java.lang.String java.lang.Integer java.lang.Long...
						hibernate方式:string int long...
		 -->
		<id name="id" column="id" type="string"/>
		<property name="name" type="string" column="name" not-null="true" />
		<property name="address" type="string" column="address" />
		<property name="gender" type="string" column="gender" />
		<property name="age" type="int" column="age" />
	</class>

</hibernate-mapping>


StudentTest.java

package com.iotek.basic.pojo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class StudentTest {

	public static void main(String[] args) {
		Student student = new Student();
		student.setId("2");
		student.setName("熊丰");
		student.setGender("male");
		student.setAddress("南京东路235号");
		student.setAge(22);
		
		// 创建一个Configuration对象
		Configuration cfg = new Configuration();
		cfg.configure();//默认是cfg.configure("hibernate.cfg.xml"),加载配置文件
		//cfg.addResource("com/iotek/basic/pojo/Student.hbm.xml");在代码处加载映射文件
		
		// 创建一个SessionFactory对象,读取<session-factory>标签
		//SessionFactory factory = cfg.buildSessionFactory();过期
		ServiceRegistryBuilder builder  = new ServiceRegistryBuilder();
		builder.applySettings(cfg.getProperties());
		ServiceRegistry sr = builder.buildServiceRegistry();
		SessionFactory factory = cfg.buildSessionFactory(sr);
		
		// 创建一个Session对象,得到数据库的连接
		Session session = factory.openSession();
		
		// Session进行数据库的crud操作
		Transaction trans=null;
		try{
		trans=session.beginTransaction();
		session.save(student);
		trans.commit();
		}catch(Exception e){
			System.out.println(e.getMessage());
			trans.rollback();
		}finally{
			session.close();
		}
	}

}

Student.java

package com.iotek.basic.pojo;

import java.io.Serializable;

public class Student implements Serializable {
	private String id;
	private String name;
	private String address;
	private String gender;
	private int age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

Hibernate中使用连接池的三种方式:

1.hibernate内置的连接方式 2.第三方c3p0方式,要自己设置参数 3.javaee开发环境中,引用tomcat中的数据源
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值