Hibernate写的一个computer管理例子

 1。Computer类的实现。

     我们需要管理实验室里面的Compute,包括机器分配给哪个同学

package com.yinbodotcc.hibernate;

public class Computer
{
 private int id;
 private String user;
 private String type;//notepad or desktop
 
 public void setId(int id)
 {
  this.id=id;
 }
 public void setUser(String name)
 {
  this.user=name;
 }
 public void setType(String type)
 {
  this.type=type;
 }
 
 public int getId()
 {
  return id;
 }
 public String getUser()
 {
  return user;  
 }
 public String getType()
 {
  return type;
 }
}

2。作为操作的DAO接口 IComputerDao

package com.yinbodotcc.hibernate;

public interface IComputerDao
{
 public Computer findComputer(int id);
 public void addComputer(Computer computer);
}

3。IComputer接口实现ComputerDao

package com.yinbodotcc.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class ComputerDao implements IComputerDao
{
 SessionFactory sessionFactory;
 public ComputerDao(SessionFactory sessionFactory)
 {
  this.sessionFactory=sessionFactory;
 }
 
 public Computer findComputer(int id)
 {
  Session session=sessionFactory.openSession();
  Computer computer=(Computer)session.get(Computer.class, id);
  session.close();
  return computer;
 }
 public void addComputer(Computer computer)
 {
  Session session=sessionFactory.openSession();
  Transaction ts=session.beginTransaction();
  session.save(computer);
  ts.commit();
  session.close();
 }
}

4。类和数据库表的匹配关系设置 computer.hbm.xml文件

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping
  PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  <!--注意上面得PUBLIC是大写得,不能小写-->
 <hibernate-mapping>
  <class name="com.yinbodotcc.springHibernate.transaction.Computer" table="computer">
   <id name="id" column="id">
    <generator class="native"/>
   </id>
   
   <property name="user" column="user"/>
   <property name="type" column="type"/>
  </class>
 </hibernate-mapping>

5。Hibernate.cfg.xml

<?xml version="1.0" encoding="GBK"?>
<!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="show_sql">
    true
   </property>
   
   <property name="dialect">
    org.hibernate.dialect.MySQLDialect
   </property>
   
   <property name="connection.driver_class">
    com.mysql.jdbc.Driver
   </property>
   
   <property name="connection.url">
    jdbc:mysql://localhost/yay
   </property>
   
   <property name="connection.username">
    root
   </property>
   
   <property name="connection.password">
    qwe123
   </property>
   
   <mapping resource="computer.hbm.xml"/>
  </session-factory>
 </hibernate-configuration>

6。测试类:

package com.yinbodotcc.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Test
{
 
 public static void main(String agrs[])
 {
  Configuration config=new Configuration().configure();
  SessionFactory sessionFactory=config.buildSessionFactory();
  
  IComputerDao ic=new ComputerDao(sessionFactory);
  
  Computer c=new Computer();
  c.setId(2);
  c.setUser("yay");
  c.setType("notepad");
  
  ic.addComputer(c);
  
  c=ic.findComputer(2);
  if(c!=null)
  System.out.println("发现得电脑使用者是: "+c.getUser());
 }

}

注意这里面的全是Hibernate的部分,没有spring,以后回集成Spring和Hibernate

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值