单表的增删改查(hibernate)

  第一步:增加一个普通的java类(polo)

package javamxj.hibernate;

public class User {
  private int id;
  private String username;
  private String password;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }
}

第二步:增加和该polo相对应的hibernate映射文件User.hbm.xml

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping>

    <class name="javamxj.hibernate.User" table="UserTable">
        <id name="id">
            <generator class="assigned" />
        </id>
        <property name="username"  />
        <property name="password" />  
    </class>

</hibernate-mapping>

第三步:编写测试类:

package javamxj.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;
import java.util.ArrayList;
import org.hibernate.Query;

public class Test {
  public static final SessionFactory sf;
  static {
    try {
      sf = new Configuration().configure().buildSessionFactory();
    }
    catch (Throwable ex) {
      System.err.println("Initial SessionFactory creation failed." + ex);
      throw new ExceptionInInitializerError(ex);
    }
  }
  /**
   * 增加一个对象
   * @param obj Object(增加的对象)
   */
  public static void save(Object obj) {
    try {
      Session session = sf.openSession();
      Transaction tx = session.beginTransaction();
      session.save(obj);
      tx.commit();
      session.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }

  }
  /**
   * 删除一个对象
   * @param primaryKey int (主键)
   */
  public static void del(int primaryKey) {

    try {
      Session session = sf.openSession();
      Transaction tx = session.beginTransaction();
      String hql = "delete User where id = :primaryKey";
      Query query = session.createQuery(hql);
      query.setShort("primaryKey", (short) primaryKey).executeUpdate();
      tx.commit();
      session.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }

  }
  /**
   * 修改一个对象
   * @param primaryKey int(根据主键修改)
   * @param newValue String(修改后的值)
   */
  public static void update(int primaryKey, String newValue) {

    try {
      Session session = sf.openSession();
      Transaction tx = session.beginTransaction();
      String hql = "update User set userName=:newValue where id = :primaryKey";
      Query query = session.createQuery(hql);
      query.setShort("primaryKey", (short) primaryKey).setString("newValue",
          newValue).executeUpdate();
      tx.commit();
      session.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }

  }
  /**
   * 根据hql语句,查找对象集合
   * @param hql String
   * @return List
   */
  public static List query(String hql) {
    List l = new ArrayList();
    try {
      Session session = sf.openSession();
      Transaction tx = session.beginTransaction();
      l = session.createQuery(hql).list();
      tx.commit();
      session.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return l;
  }

  public static void main(String[] args) {
    update(3, new String("wsyrnm"));
  }
}
第四步:配置数据库mysql。

先增加数据库hibernatetest,然后运行脚本:

CREATE TABLE `usertable` (
  `ID` int(6) NOT NULL auto_increment,
  `username` varchar(24) NOT NULL default '',
  `password` varchar(24) NOT NULL default '',
   PRIMARY KEY  (`ID`)
) TYPE=MyISAM;

终于配置完毕,运行测试类相应的方法就可以得到你想要的结果。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值