Hibernate框架入门案例

我们使用hibernate框架,界面要通过service,来访问数据持久层(对jdbc封装,同时给上层提供了大量接口和类),再访问数据库中的源。

今天该入门案例就暂时跳过service,界面直接访问数据持久层。


记得导入mysql的驱动包。

Employee.java

package com.hsp.domain;
//建议domain对象就是对应表的首字母大小
//这是一个domain对象(实际上就是javabean/有些人pojo)
//他和Employee对应

public class Employee {
	private Integer id;
	private String name;
	private String email;
	private java.util.Date hiredate;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public java.util.Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(java.util.Date hiredate) {
		this.hiredate = hiredate;
	}
}
Employee.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="com.hsp.domain">
	
	<class name="Employee" table="employee">
		<!--<property name="id" type="java.lang.Integer"
		column name="id" not-null="false"/>
		<generator class="identity"/>
		</property>-->
		
		<id name="id" 
			column="id" 
			type="java.lang.Integer">
			<generator class="native"/>
		</id>
		
		<property name="name" type="java.lang.String">
		<column name="name" not-null="false"  />
		</property>
		
		<property name="email" type="java.lang.String">
		<column name="email" not-null="false"  />
		</property>
		
		<property name="hiredate" type="java.util.Date">
		<column name="hiredate" not-null="false"  />
		</property>
	</class>
</hibernate-mapping>


在配置这个xml文件前,要先在数据库中建表。

create table employee(

id int auto_increment primary key,--自增长

name varchar(64) not null,

email varchar(64) not null,

hiredate date not null);


主键与之对应:

<id name="id" 
column="id" 
type="java.lang.Integer">
<generator class="native"/>
</id>

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">
<!-- 用于配置domain关系和表的映射关系 -->
<hibernate-configuration>

    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        
     
        <!--是否在后台显示Hibernate用到的SQL语句,开发时设置为true,便于差错,程序运行时可以在Eclipse的控制台显示Hibernate的执行Sql语句。项目部署后可以设置为false,提高运行效率--> 
        <!--<property name="hibernate.show_sql">true </property>-->
       	<!--connection.useUnicode连接数据库时是否使用Unicode编码--> 
        <!--<property name="Connection.useUnicode">true </property>--> 
        <!--connection.characterEncoding连接数据库时数据的传输字符集编码方式,最好设置为gbk,用gb2312有的字符不全--> 
   		<!--<property name="connection.characterEncoding">utf-8 </property>-->   
         
         
        <!--<property name="connection.url">jdbc:mysql://localHost:3306/demo?useUnicode=true&characterEncoding=utf8</property>-->
        <property name="connection.url">jdbc:mysql://localHost:3306/demo</property>
    	   
        
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>
        <!-- 配置数据库的方言/ -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect </property>
        <!-- 配置显示hibernate生成的 sql ,特别说明,在开发阶段设为true利于调试,在使用项目则设为false-->
        <property name="show_sql">true</property>
		<!-- 指定管理的对象映射文件 -->
        <mapping resource="com/hsp/domain/Employee.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


对于mysql插入中文字段,编译出错的情况,网络上面提供的方法:

<property name="connection.url">jdbc:mysql://localHost:3306/demo?useUnicode=true&characterEncoding=utf8</property>

也未能奏效。

用上一种也如此,具体解决方案,望各位博友赐教!


TestMain.java

package com.hsp.view;

import java.util.Date;

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

import com.hsp.domain.Employee;

public class TestMain {

	public static void main(String[] args) {
		//使用hibernate实现crud
		//现在我们暂时不用service,直接测试
		//1.创建configuration,给对象用于读取hibernate.cfg.xml
		Configuration configuration=new Configuration().configure();
		//2.得到SessionFactory(会话工厂,这是一个重量级的类,因此要保证在一个应用程序中只能有一个)
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		//3.创建session相当于jdbc connection(不是servlet和jsp的session)
		Session session=sessionFactory.openSession();
		//4.对hibernate而言,要求crud,要用事务提交
		Transaction transaction=session.beginTransaction();
		//添加一个雇员
		Employee employee = new Employee();
		employee.setId(2);
		employee.setName("hello");
		employee.setEmail("hqg@qq.com");
		employee.setHiredate(new Date());
		//insert...
		//保存
		session.save(employee);//insert into...(被封装)
		//提交
		transaction.commit();
		session.close();
	}

}


employee.setName("hello");如果插入英文,则能正常显示。而中文的乱码问题,楼主尝试了很久都没解决^_^
这就是个简单的hibernate案例啦,特作笔记加深印象。


可参考:

hibernate配置文件hibernate.cfg.xml的详细解释

http://www.cnblogs.com/jqyp/archive/2010/06/28/1766851.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值