Hibernate逍遥游记-第2章-使用hibernate.properties
2016-03-19 20:44
495 查看
1.
2.
3.
4.
package mypack; import org.hibernate.*; import org.hibernate.cfg.Configuration; import java.util.*; public class BusinessService{ public static SessionFactory sessionFactory; /** 初始化Hibernate,创建SessionFactory实例 */ static{ try{ // 根据默认位置的Hibernate配置文件的配置信息,创建一个Configuration实例 Configuration config = new Configuration(); //加载Monkey类的对象-关系映射文件 config.addClass(Monkey.class); // 创建SessionFactory实例 sessionFactory = config.buildSessionFactory(); SessionFactory sessionFactory2 = config.buildSessionFactory(); System.out.println("---------->"+(sessionFactory2==sessionFactory)); }catch(RuntimeException e){e.printStackTrace();throw e;} } /** 查询所有的Monkey对象,然后打印Monkey对象信息 */ public void findAllMonkeys(){ Session session = sessionFactory.openSession(); //创建一个会话 Transaction tx = null; try { tx = session.beginTransaction(); //开始一个事务 Query query=session.createQuery("from Monkey as m order by m.name asc"); List monkeys=query.list(); for (Iterator it = monkeys.iterator(); it.hasNext();) { Monkey monkey=(Monkey) it.next(); System.out.println("ID="+monkey.getId() +",姓名="+monkey.getName() +",年龄="+monkey.getAge() +",性别="+(monkey.getGender()=='M'?"公猴":"母猴")); } tx.commit(); //提交事务 }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } /** 持久化一个Monkey对象 */ public void saveMonkey(Monkey monkey){ Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(monkey); tx.commit(); }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } /** 按照OID加载一个Monkey对象,然后修改它的属性 */ public void loadAndUpdateMonkey(Long monkey_id,int age){ Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Monkey m=(Monkey)session.get(Monkey.class,monkey_id); m.setAge(age); tx.commit(); }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } /**删除Monkey对象 */ public void deleteMonkey(Monkey monkey){ Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.delete(monkey); tx.commit(); }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } public void test(){ Monkey monkey=new Monkey(); monkey.setName("智多星"); monkey.setAge(1); monkey.setGender('M'); saveMonkey(monkey); findAllMonkeys(); loadAndUpdateMonkey(monkey.getId(),2); findAllMonkeys(); deleteMonkey(monkey); findAllMonkeys(); } public static void main(String args[]){ new BusinessService().test(); sessionFactory.close(); } }
2.
<?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> <class name="mypack.Monkey" table="MONKEYS"> <id name="id" column="ID" type="long"> <generator class="increment"/> </id> <property name="name" column="NAME" type="string" not-null="true" /> <property name="age" column="AGE" type="int" /> <property name="gender" column="GENDER" type="character"/> </class> </hibernate-mapping>
3.
hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.url=jdbc:mysql://localhost:3306/SAMPLEDB?useUnicode=true&characterEncoding=GBK hibernate.connection.username=root hibernate.connection.password=1234 hibernate.show_sql=true
4.
相关文章推荐
- debian ubuntu linux系好用的包管理工具 aptitude
- Linux 文件传输 lrzsz,scp
- 官网下载apache服务器并运行
- 官网下载apache服务器并运行
- CentOS6.5中修改yum源
- linux下文件的复制、移动与删除
- Linux 下查看局域网内所有主机IP和MAC
- Linux Is Not Matrix——扩容的时间2
- linux内核的组成,王明学learn
- OpenCV 2.2.0及以上版本CvvImage的使用
- linux下基本性能监控命令和性能分析(vmstat)
- Linux命令之chown
- Linux的userdel和deluser命令
- Jenkins进阶系列之——09配置Linux系统ssh免密码登陆
- linux下jenkins安装
- Hadoop 基础架构 详解
- Xshell的安装与使用,向云服务器上传文件
- Debian Linux SSH/SFTP 服务设置
- hadoop节点运行的reduce和map任务数
- Linux添加软件连接方法