Hibernate是一个开放源代码的对象关系映射框架,可以应用在任何使用JDBC的场合,既可以在Java的客户端程序实用,也可以在Servlet/JSP的Web应用中使用
优势:
简化开发,提高开发效率,持久层关系映射复杂情况下,hibernate开发效率相当高。
hibernate内部进行了JDBC的优化处理,以及增加缓存机制,大大提高了运行效率。
更好的移植性,Hibernate已经为我们屏蔽了数据库之间的差异
Hibernate使用 Java 反射机制 而不是字节码增强程序来实现透明性。
Hibernate的性能非常好,因为它是个轻量级框架。 映射的灵活性很出色。
Hibernate它支持各种关系数据库,从一对一到多对多的各种复杂关系。
代码结构
实体类User
public class User { private int id; private String name; private Date birthday; private int age; private int total; public User() { // TODO Auto-generated constructor stub } public User(String name, Date birthday, int age) { super(); this.name = name; this.birthday = birthday; this.age = 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 Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
orm映射文件User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!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="com.domain.User" table="user" catalog="ssh"> <id name="id" column="id" type="int"> <generator class="native"></generator> </id> <property name="name" column="name" type="java.lang.String"></property> <property name="age"> <column name="age" sql-type="int"></column> </property> <property name="birthday" type="date"></property> <property name="total" formula="(select count(*) from user)"></property> </class> </hibernate-mapping>
核心配置文件hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///ssh</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="javax.persistence.validation.mode">none</property>
//这里需要配置进来 <mapping resource="com/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
HibernateUtils工具类
public class HibernateUtils { private static Configuration configuration; private static SessionFactory sessionFactory; static { configuration = new Configuration().configure(); sessionFactory = configuration.buildSessionFactory(); } public static Session openSession() { return sessionFactory.openSession(); } }
UserTest测试类
public class UserTest { //派生属性 @Test public void total(){ Session session=HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); User user = (User) session.get(User.class, 6); System.out.println(user.getTotal()); transaction.commit(); session.close(); } //删除 @Test public void delete(){ Session session=HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); User user=new User(); user.setId(3); session.delete(user); transaction.commit(); session.close(); } //通过ID查询 @Test public void findById(){ Session session=HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); User user=(User) session.get(User.class, 3); System.out.println(user); transaction.commit(); session.close(); } //添加 @Test public void add(){ Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); for(int i=0;i<5;i++){ User user=new User("cai"+i,new Date(),12+i); session.save(user); } transaction.commit(); session.close(); } }