成功将一条数据插入数据表中
-----------------------手工搭建hibernate环境---------------------------
/*
* 准备好hibernate软件
* 使用MyEclipse5.5+MySQL
*/
1、首先创建实体类 userinfo
2、配置映射类信息
可以这样添加一个引入"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" 的xml文件
在相应的实体类包里面添加一个userinfo.hbm.xml文件
-new
-other
-在wizards中输入 xml-选择 XML(Basic Templates)
-选择Create XML file from a DTD file
-在file name 中输入映射类配置文件名称 userinfo.hbm.xml
-选择Advanced按钮
-勾选Link to file in the file system
-在hibernate工具包中选择../hibernate-3.2/src/org/hibernate/hibernate-mapping-3.0.dtd
-选择select XML Catalog entry
-在Workbench Files中选择-//Hibernate/Hibernate-configuration-3.0.dtd
-Next
-Finish
然后配置映射类信息
<?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="com.wl.ssh.po.UserInfo" table="user_info">
<!-- 配置主键策略 数据库 u_id 主键自增-->
<id name="id" column="u_id">
<generator class="native"/>
</id>
<!-- 若不配置column,hibernate会自动使用javabean的属性作为字段名 -->
<property name="username" column="u_name"></property>
<property name="age" column="u_age"></property>
<!--
此配置使用type=timestamp
因为hibernate不知道将该字段转换成何种时间类型,SQL date,或timestamp,还是time 字段
把这个属性映射成timestamp转换器,
这样确定将javabean属性类型转换成相应的数据库明确的时间类型。
这样我们预留了日期和时间的全部信息。
-->
<property name="creationTime" type="timestamp" column="u_creationTime"></property>
</class>
</hibernate-mapping>
3、配置数据库配置信息
可以这样添加一个引入""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"" 的xml文件
在src包下添加一个hibernate.cfg.xml文件
-new
-other
-在wizards中输入 xml-选择 XML(Basic Templates)
-选择Create XML file from a DTD file
-在file name 中输入映射类配置文件名称 userinfo.hbm.xml
-选择Advanced按钮
-勾选Link to file in the file system
-在hibernate工具包中选择../hibernate-3.2/src/org/hibernate/hibernate-mapping-3.0.dtd
-选择select XML Catalog entry
-在Workbench Files中选择-//Hibernate/Hibernate-configuration-3.0.dtd
-Next
-Finish
然后配置数据库配置信息
<?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>
<!-- MySQL数据库连接配置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/userdb</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<!-- 显示插入sql语句信息 -->
<property name="hibernate.show_sql">true</property>
<!-- 数据库方言配置 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 引入相关映射类配置信息 -->
<mapping resource="com/wl/ssh/po/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、添加相应的jar(这是几个最基本的这几个jar都必须的添加)
--添加记录所需要的包
hibernate3.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
--
-- 查询记录追加包
antlr-2.7.6.jar
--
mysql-connector-java-3.1.7-bin.jar(相应的数据库驱动包,这里是采用mysql数据库)
5、创建hibernate工具类
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
protected static Session session = null;
protected Transaction transction = null;
private static SessionFactory sessionFactory = null;
private static Configuration config = null;
static {
config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
}
public static Session openSession() {
return sessionFactory.openSession();
}
public static void closeSession() {
if (session.isOpen() && sessionFactory.isClosed() && session != null) {
session.close();
sessionFactory.close();
session = null;
sessionFactory = null;
}
}
}
6、添加一条记录
public boolean addUserInfo(UserInfo userinfo){
try {
//1、获取一个会话
session = super.openSession();
//2、开启事务
this.transction = this.session.beginTransaction();
//3、保存对象
this.session.save(userinfo);
//4、提交事务
this.transction.commit();
return true;
} catch (HibernateException e) {
e.printStackTrace();
//5、回滚事务
this.transction.rollback();
return false;
}
}
7、测试,完成添加。