ibernate Getting Started --- Summary: CRUD ---- complete [the first day]

The following additions and deletions is a class packaging, as written, can not call deletions, or additions and changes, altered data. Because the session sharing, and each method can not close the session, otherwise the call will be affected subsequent method.

package bean529;

import hibernate.factory.HibernateSessionFactory;

import org.hibernate.Session;
import org.hibernate.Transaction;

import bean.Customer;

public class CustomerManager {
    Session session = HibernateSessionFactory.getSession();
    Transaction tran = session.beginTransaction();
    //增加记录
    public void addCustomer(String customerID,String name,String phone){
        /**定义事务开始**/
        //注解实体类
        Customer dept = new Customer();
        dept.setCustomerID (customerID); 
        dept.setName (name); 
        dept.setPhone (Phone); 
        were saved using Session.save (the dept); 
        // commit the transaction 
        tran.commit (); 
    } 
    // delete records, by the above mentioned id 
    public  void delCustomer ( customerID String) {
         / ** first Find records to be deleted, * by the above mentioned id * / 
        the Customer the dept = (the Customer) Session.get (the Customer. class , "1003" ); 
        session.delete (the dept); 
        tran.commit (); 
    } 
    // modify records, to find the id, the modified 
    public  void updateCustomer (customerID String, String name, String Phone) {
        // first looks for records to be modified by the ID 
        the Customer Dept = (the Customer) Session.get (the Customer. Class , "1003" ); 
        dept.setName (name); 
        dept.setPhone (Phone); 
        Session.saveOrUpdate (Dept); 
        tran.commit ();     
    } 
    // test 
    public  static  void main (String [] args) { 
        CustomerManager Manager = new new CustomerManager ();
          // manager.addCustomer ( "1003", "WE", "31231");
         // manager.delCustomer ( "1003"); 
         manager.updateCustomer ( "1003", "name is modified", "number is modified" );
        
    }
}

Summary: If you need to display the query in the console, you can configure the properties in the configuration file hibernate.cfg.xml show_sql, the value is true. Subsequent batch queries should be considered, at the same time CRUD and other issues. So basic CRUD even perfect. Useful to query the update process. You can refer to. Before you know entangled in the servlet to implement various functions, they found bloated and maintenance is not good, but now I feel at ease ssh research framework is really a waste of time before feeling a part, but that's OK, at least they understand the current framework, more able to clear understanding deeper things.

       <! - Configuration Console view displays queries -> 
       < Property name = "show_sql" > to true </ Property >

After the addition of the phrase, when you save the data, the console:

 

Guess you like

Origin www.cnblogs.com/ciscolee/p/10945464.html