单表的增删改查(hibernate)

    技术2022-05-12  3

    第一步:增加一个普通的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)