我们使用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>
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>
<?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案例啦,特作笔记加深印象。
可参考: