首先建一个web project
导入Hibernate所需要的jar包,
.
+lib
antlr.jar
cglib.jar
asm.jar
asm-attrs.jars
commons-collections.jar
commons-logging.jar
ehcache.jar
hibernate3.jar
jta.jar
dom4j.jar
log4j.jar
这里我用的数据库是MySQL
然后创建所需要的Model,代码如下:
package com.xll.bean;
public class UserModel implements java.io.Serializable{
private static final long serialVersionUID = 1L;
private int uuid;
private String userId;
private String userName;
private int age;
private String pwd;
private String registerTime;
public UserModel(){}//无参构造
//setter and getter .....
在model的同级目录下建立UserModel.hbm.xml映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.xll.bean.UserModel" table="tbl_user" >
<id name="uuid" column="uuid" >
<generator class="assigned"/>
</id>
<property name="userId" column="userId" />
<property name="userName" column="userName" />
<property name="age" column="age" />
<property name="registerTime" column="registerTime" />
<property name="pwd" column="pwd" />
</class>
</hibernate-mapping>
然后在src下配置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">
<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/mydb</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping resource="com/xll/bean/UserModel.hbm.xml"/>
</session-factory>
</hibernate-configuration>
编写测试类 Client.java
package com.xll.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.xll.bean.UserModel;
public class Client {
public static void main(String[] args) {
UserModel um = new UserModel();
um.setAge(11);
um.setPwd("123123");
um.setRegisterTime("2011-10-10");
um.setUserId("432123");
um.setUserName("xiaoliang");
um.setUuid(6666);
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = null;
Transaction t = null;
try {
s = sf.openSession();
t = s.beginTransaction();
s.save(um);
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
s.close();
}
}
}
运行结果:
Hibernate: insert into tbl_user (userId, userName, age, registerTime, pwd, uuid) values (?, ?, ?, ?, ?, ?)//配置中的show_sql
分享到:
相关推荐
hibernate 资料hibernate 资料hibernate 资料hibernate 资料
hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3
hibernate教程hibernate教程hibernate教程
hibernate annotation hibernate3
hibernate5 hibernate PDF 讲义 动力 hibernate ssh hibernate5 节点
hibernate 5.2.15 hibernate 5.2.15 hibernate 5.2.15 hibernate 5.2.15 hibernate 5.2.15hibernate 5.2.15
Hibernate.jar包,Hibernate可以应用在任何使用JDBC的场合,包含 hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-...
Hibernate连表查询 Hibernate连表查询
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate...
hibernate 实战 hibernate 实战 hibernate 实战 hibernate 实战
Hibernate面试题专栏 - 最全的Hibernate面试题, Hibernate笔试题, Hibernate问题 Hibernate教程
hibernate第一个hibernate
HIBERNATE HIBERNATE HIBERNATE HIBERNATE
DmDialect-for-hibernate2.0、DmDialect-for-hibernate2.1、DmDialect-for-hibernate3.0、DmDialect-for-hibernate3.1、DmDialect-for-hibernate3.6、DmDialect-for-hibernate4.0、DmDialect-for-hibernate5.0、...
hibernate aiphibernate aiphibernate aip
Hibernate连接SQLite配置步骤: 1、添加SQLite库: 1.1、将两个jar包:com.zy.hibernate.dialect.SQLiteDialect.jar、sqlite-jdbc-3.7.2.jar拷贝到“\WEB-INF\lib”文件夹下; 2、配置hibernate: 2.1、将...
hibernate
ejb+hibernate资料 ejb+hibernate资料 ejb+hibernate资料 ejb+hibernate资料
包含hibernate所有所需jar包还有一些其他包日志包、jpa支持包等: 列如:hibernate-core-5.1.0.Final.jar hibernate-ehcache-5.1.0.Final.jar hibernate-entitymanager-5.1.0.Final.jar hibernate-envers-5.1.0....
Manning出版 Java Persistence With Hibernate 第二版 清晰pdf Hibernate, an open source project that is almost 15 years old. In a recent poll, Hibernate was among the top five tools used by many Java ...