Hibernate优缺点:
优点:
1.Hibernate功能强大,较之JDBC方式操作数据库,代码量大大减少,提高持久化代码的开发速度,降低维护成本。
2.Hibernate支持许多面向对象的特性
3.可移植性好
4.Hibernate框架开源免费
缺点:
1.不适合以数据为中心的大量使用存储过程的应用。
2.大规模的批量插入、修改和删除不适合用Hibernate环境搭建
一、下载并导入Hibernate包,mysql连接驱动,log4j相关类包
二、创建配置文件Hibernate.cfg.xml
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/mysql</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<mapping resource="demo1/Item.hbm.xml"/>
</session-factory>
</hibernate-configuration>
三、创建实体类和映射文件
实体类:
package demo1;
public class User {
private int id;
private String name;
private String password;
private int age;
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
映射文件:
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="demo1.User" table="t_user">
<id name="id" type="int">
<generator class="identity"/>
</id>
<property name="name" type="string"/>
<property name="password" type="string"/>
<property name="age" type="int"/>
</class>
</hibernate-mapping>
四、创建相应数据库
CREATE TABLE t_user (
id INT NOT NULL,
name VARCHAR(32) NOT NULL,
password VARCHAR(32) NOT NULL,
age INT NOT NULL,
INDEX id (`id`)
)
五、创建测试类
package Dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import demo1.User;
public class TextDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg = null;
SessionFactory sf = null;
Session s = null;
Transaction ts = null;
User user = new User();
user.setName("老魏");
user.setPassword("123");
user.setAge(22);
try {
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
s = sf.openSession();
ts = s.beginTransaction();
s.save(user);
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if(ts != null){
ts.rollback();;
}
e.printStackTrace();
}finally{
if(s != null){
s.close();
}
}
}
}
六、运行结果