Hibernate4最简单一个项目实例

1

导入jar包,request目录下的

2 根目录下主配置文件

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

        <!-- Database connection settings -->
        <!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
                    <property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> -->

 		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/wanju
		</property>
		<property name="connection.username">root</property>
		<property name="connection.password">root</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>

        <!-- JDBC connection pool (use the built-in) 
        <property name="connection.pool_size">1</property>

        <property name="current_session_context_class">thread</property>

        <property name="cache.use_query_cache">true</property>
        <property name="cache.use_second_level_cache">true</property>
        <property name="cache.use_structured_entries">true</property>
        <property name="cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
        <property name="net.sf.ehcache.configurationResourceName">/hibernate-config/ehcache.xml</property>
        <property name="show_sql">true</property>

        <mapping resource="hibernate-config/domain/Event.hbm.xml"/>
        <mapping resource="hibernate-config/domain/Person.hbm.xml"/>
        <mapping resource="hibernate-config/domain/PhoneNumber.hbm.xml"/>
        <mapping resource="hibernate-config/domain/Account.hbm.xml"/>
        <mapping resource="hibernate-config/domain/HolidayCalendar.hbm.xml"/>

        <mapping resource="hibernate-config/domain/Item.hbm.xml"/>-->
		<mapping resource="com/sm/hibernate/dao/User.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

3 pojo

package com.sm.hibernate.dao;

public class User {

	private int id;
	private String name;
	private int qq;
	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 getQq() {
		return qq;
	}
	public void setQq(int qq) {
		this.qq = qq;
	}
}

对应的 xml


<?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>
	<class name="com.sm.hibernate.dao.User" table="hib_user">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<property name ="qq"></property>
		
    </class>
</hibernate-mapping>
4 测试

package com.sm.hibernate.Test;

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

import com.sm.hibernate.dao.User;

public class Test {

	public static void main(String[] args) {
		SessionFactory factory  = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();
		session.beginTransaction();
		User user = new User();
		user.setQq(323222);
		user.setName("abolo");
		session.save(user);
		session.getTransaction().commit();
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值