【Hibernate3】(1)初识Hibernate

来源:互联网 发布:宣传片制作 知乎 编辑:程序博客网 时间:2024/05/21 08:44

一. 搭建环境

创建一个工程,需要到如以下的jar包:

还需要配置文件log4j.properties和hibernate.cfg.xml。
这个是hibernate.cfg.xml的内容:
<!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://localhost:3306/hibernate?createDatabaseIfNotExist=true</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123456</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="show_sql">true</property><mapping resource="org/hibernate/test/legacy/Simple.hbm.xml" /></session-factory></hibernate-configuration>
dialect和show_sql是指定了显示什么方言和在控制台显示sql语句。

二. 第一个程序

创建实体类User,具有以下属性:
private int id;private String username;private String sex;private int age;private Date birthday;private double salary;
创建User.hbm.xml文件,将其放在与实体同一级目录下:
<?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 package="org.hibernate.test.abstractembeddedcomponents.cid"><class name="com.thr.hibernate.entity.User" table="T_USER"><id name="id"><generator class="native"></generator></id><property name="username" /><property name="sex" /><properties name="age" /><property name="birthday" /><property name="salary" /></class></hibernate-mapping>
class配置实体类的全名,table配置数据库表明,id表示的是主键,其它的是User的属性。
最后将创建好的User.hbm.xml配置到hibernate.cfg.xml文件中:
<property name="hbm2ddl.auto">update</property><mapping resource="com/thr/hibernate/entity/User.hbm.xml" />
配置hbm2ddl.auto的参数update表示如果表不存在的情况下,自动创建表;存在的话就自动更新。
创建测试类测试一下:

1. 添加

public void test1() {User user = new User();user.setUsername("张三");user.setSex("男");user.setAge(27);user.setBirthday(new Date());user.setSalary(100.5);// 解析hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();// 创建SessionFactory(创建连接池)SessionFactory factory = cfg.buildSessionFactory();// 创建SessionSession session = factory.openSession();// 创建及开启事务对象Transaction trans = null;try {trans = session.beginTransaction();// 添加User实体对象session.save(user);trans.commit();} catch (HibernateException e) {trans.rollback();e.printStackTrace();} finally {if (session != null) {session.close();}}}

2. 查询

/** * 根据ID查询 */public void testQuery() {// 解析hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();// 创建SessionFactory(创建连接池)SessionFactory factory = cfg.buildSessionFactory();// 创建SessionSession session = factory.openSession();// 创建及开启事务对象Transaction trans = null;try {trans = session.beginTransaction();User user = (User) session.load(User.class, 1);System.out.println(user);trans.commit();} catch (HibernateException e) {trans.rollback();e.printStackTrace();} finally {if (session != null) {session.close();}}}

3. 删除

/** * 根据ID删除 */public void testDelete() {// 解析hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();// 创建SessionFactory(创建连接池)SessionFactory factory = cfg.buildSessionFactory();// 创建SessionSession session = factory.openSession();// 创建及开启事务对象Transaction trans = null;try {trans = session.beginTransaction();User user = (User) session.load(User.class, 1);session.delete(user);System.out.println(user);trans.commit();} catch (HibernateException e) {trans.rollback();e.printStackTrace();} finally {if (session != null) {session.close();}}}

4. 更新

/** * 根据ID更新 */public void testUpdate() {// 解析hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();// 创建SessionFactory(创建连接池)SessionFactory factory = cfg.buildSessionFactory();// 创建SessionSession session = factory.openSession();// 创建及开启事务对象Transaction trans = null;try {trans = session.beginTransaction();User user = (User) session.load(User.class, 2);user.setSex("女");user.setUsername("小雪");session.update(user);trans.commit();} catch (HibernateException e) {trans.rollback();e.printStackTrace();} finally {if (session != null) {session.close();}}}

5. 查询集合

/** * 查询集合 */public void testQueryAll() {// 解析hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();// 创建SessionFactory(创建连接池)SessionFactory factory = cfg.buildSessionFactory();// 创建SessionSession session = factory.openSession();// 创建及开启事务对象Transaction trans = null;try {trans = session.beginTransaction();Query query = session.createQuery("from User");List<User> users = query.list();for (User user : users) {System.out.println(user);}trans.commit();} catch (HibernateException e) {trans.rollback();e.printStackTrace();} finally {if (session != null) {session.close();}}}


0 0
原创粉丝点击