Hibernate写的一个computer管理例子

    技术2022-05-11  84

     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)