Hibernate基于配置文件(一)快速开始
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">root</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="show_sql">true</property><property name="hibernate.hbm2ddl.auto">update</property><mapping resource="org/leadfar/hibernate/model/User.hbm.xml"></mapping></session-factory></hibernate-configuration>
?
实体类
package org.leadfar.hibernate.model;import java.util.Date;public class User {private int id;private String name;private int age;private boolean sex;private Date birthday;private double money;/** * 无参构造方法 * getters * setters */User() {}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 int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean isSex() {return sex;}public void setSex(boolean sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public double getMoney() {return money;}public void setMoney(double money) {this.money = money;}}
?
实体类的映射文件
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="org.hibernate.auction"><class name="org.leadfar.hibernate.model.User" table="t_user" ><!-- id为数据库标识,作为主键 --><id name="id"><generator name="code">package org.leadfar.hibernate.model;import java.util.Date;import java.util.Iterator;import java.util.List;import java.util.Random;import junit.framework.TestCase;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class UserTest extends TestCase {Random r = new Random();///////////////////////增///////////////////public void testUser_01() throws Exception {//默认读取类路径根目录下的hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();//创建SessionFactorySessionFactory sfactory = cfg.buildSessionFactory();//创建Hibernate SessionSession session = sfactory.openSession();try {//开启事务session.beginTransaction();//创建实体对象User user = new User();user.setName("张三");user.setAge(20);user.setSex(false);user.setBirthday(new Date());user.setMoney(99999.99);//保存对象session.save(user);//提交事务session.getTransaction().commit();} catch(Exception e) {e.printStackTrace();session.getTransaction().rollback();} finally {session.close();}}///////////////////////增///////////////////public void testUser_add() throws Exception {//默认读取类路径根目录下的hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();//创建SessionFactorySessionFactory sfactory = cfg.buildSessionFactory();//创建Hibernate SessionSession session = sfactory.openSession();try {//开启事务session.beginTransaction();for(int i=-0; i<100;i++) {//创建实体对象User user = new User();user.setName("用户"+i);user.setAge(r.nextInt(99));user.setSex(Math.random() > 0.5 ? true : false);user.setBirthday(new Date());user.setMoney(99999.99);//保存对象session.save(user);}//提交事务session.getTransaction().commit();} catch(Exception e) {e.printStackTrace();session.getTransaction().rollback();} finally {session.close();}}///////////////////////查///////////////////public void testUser_findById() throws Exception {//默认读取类路径根目录下的hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();//创建SessionFactorySessionFactory sfactory = cfg.buildSessionFactory();//创建Hibernate SessionSession session = sfactory.openSession();try {//开启事务session.beginTransaction();User user = (User)session.get(User.class, 15);System.out.println(user.getId()+","+user.getName());//提交事务session.getTransaction().commit();} catch(Exception e) {e.printStackTrace();session.getTransaction().rollback();} finally {session.close();}}///////////////////////查///////////////////public void testUser_findAll() throws Exception {//默认读取类路径根目录下的hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();//创建SessionFactorySessionFactory sfactory = cfg.buildSessionFactory();//创建Hibernate SessionSession session = sfactory.openSession();try {//开启事务session.beginTransaction();//HQL Hibernate Query LanguageList<User> list = session.createQuery("from User").list();for (Iterator iterator = list.iterator(); iterator.hasNext();) {User user = (User) iterator.next();System.out.println(user.getId()+","+user.getName()+","+user.getBirthday()+","+user.isSex()+","+user.getMoney());}//提交事务session.getTransaction().commit();} catch(Exception e) {e.printStackTrace();session.getTransaction().rollback();} finally {session.close();}}///////////////////////改///////////////////public void testUser_update() throws Exception {//默认读取类路径根目录下的hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();//创建SessionFactorySessionFactory sfactory = cfg.buildSessionFactory();//创建Hibernate SessionSession session = sfactory.openSession();try {//开启事务session.beginTransaction();User user = (User)session.get(User.class, 101);user.setBirthday(null);user.setName("阿凡达");session.update(user);//提交事务session.getTransaction().commit();} catch(Exception e) {e.printStackTrace();session.getTransaction().rollback();} finally {session.close();}}///////////////////////删///////////////////public void testUser_delete() throws Exception {//默认读取类路径根目录下的hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();//创建SessionFactorySessionFactory sfactory = cfg.buildSessionFactory();//创建Hibernate SessionSession session = sfactory.openSession();try {//开启事务session.beginTransaction();User user = (User)session.get(User.class, 101);session.delete(user);//提交事务session.getTransaction().commit();} catch(Exception e) {e.printStackTrace();session.getTransaction().rollback();} finally {session.close();}}}
?