您的位置:首页 > 移动开发

Hibetnate(网上学习资料转载)

2011-03-30 17:36 253 查看
Hibetnate(网上学习资料转载)

 

Hibetnate(学习1)

 

Hibernate开发的步骤:
第一:持久化类:ORM的主要特点  对象(POJO)----à数据库
第二:类和数据库关系的映射(*.hbm.xml)
第三:应用程序的开发
Hibernate的操作
数据库如下:
 



id为指派形式
Hibernate环境的搭建
1.       Hibernate.cfg.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!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="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
        <property
name="hibernate.connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=hibernate</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password"/>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.show_sql">true</property>
        <mapping resource="com/Person.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
 
2.       持久化类  Person.java
    package com;
// Generated 2008-11-29 10:41:49 by Hibernate Tools 3.2.1.GA
 
 
 
/**
 * Person generated by hbm2java
 */
public class Person  implements java.io.Serializable {
 
     private String id;
     private String name;
     private String password;
     private String sex;
     private String email;
 
    public Person() {
    }
    public Person(String id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }
    public Person(String id, String name, String password, String sex, String email) {
       this.id = id;
       this.name = name;
       this.password = password;
       this.sex = sex;
       this.email = email;
    }
 
    public String getId() {
        return this.id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return this.password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSex() {
        return this.sex;
    }
 
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getEmail() {
        return this.email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
}
3.       映射文件  Person.hbm.xml
     <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2008-11-29 10:42:17 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="com.Person" table="person" schema="dbo" catalog="hibernate">
        <id name="id" type="string">
            <column name="id" length="32" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="20" not-null="true" />
        </property>
        <property name="password" type="string">
            <column name="password" length="20" not-null="true" />
        </property>
        <property name="sex" type="string">
            <column name="sex" length="2" />
        </property>
        <property name="email" type="string">
            <column name="email" length="30" />
        </property>
    </class>
</hibernate-mapping>
现在基本上ok,所需环境完成
 
 

Hibernate(学习2)

上一节已经配置好了,下来进行插入操作

                                                    PersonOperate.java

package com;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

/**

 *

 * @author Administrator

 */

public class PersonOperate {

    private Session session=null;

    public PersonOperate() {

        Configuration config =new Configuration().configure();

        SessionFactory factory=config.buildSessionFactory();

        this.session=factory.openSession();

    }

    //插入

    public void insert(Person p){

        Transaction tran=this.session.beginTransaction();

        this.session.save(p);

        tran.commit();

        this.session.close();

    }

}

                                                                   TestPO.java

package com;

/**

 *

 * @author Administrator

 */

public class TestPO {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        Person p = new Person();

        p.setId("我爱飞飞哦");

        p.setName("qunqun");

        p.setSex("女");

        p.setPassword("123456");

        p.setEmail("zdq0426@166.com
");

        PersonOperate po = new PersonOperate();

        po.insert(p);

    }

}

 

hibernate(学习3)

                                                              PersonOperate.java

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package com;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

/**

 *

 * @author Administrator

 */

public class PersonOperate {

    private Session session=null;

    public PersonOperate() {

        Configuration config =new Configuration().configure();

        SessionFactory factory=config.buildSessionFactory();

        this.session=factory.openSession();

    }

    //插入

    public void update(Person p){

        Transaction tran=this.session.beginTransaction();

        this.session.update(p);

        tran.commit();

        this.session.close();

    }

}

 TestPO.java

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package com;

/**

 *

 * @author Administrator

 */

public class TestPO {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        Person p = new Person();

        p.setId("我爱飞飞哦");

        p.setName("qunqun");

        p.setSex("男");

        p.setPassword("321456");

        p.setEmail("zdq0426@126.com
");

        PersonOperate po = new PersonOperate();

        po.update(p);

    }

}

修改的时候一般不会变得id,修改的是其他的信息,id必须存在

 

 

hibernate(学习4)

查询操作 

                                                         PersonOperate.java

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package com;

import java.util.Iterator;

import java.util.List;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

/**

 *

 * @author Administrator

 */

public class PersonOperate {

    private Session session = null;

    public PersonOperate() {

        Configuration config = new Configuration().configure();

        SessionFactory factory = config.buildSessionFactory();

        this.session = factory.openSession();

    }

    // 按ID查询:推荐使用HQL —— 是Hibernate官方推荐的查询语言

    public Person queryById(String id) {

        Person p = null;

        // 使用Hibernate查询语言

        String hql = "FROM Person as p WHERE p.id=?";

        // 通过Query接口查询

        Query q = this.session.createQuery(hql);

        q.setString(0, id);

        List l = q.list();

        Iterator iter = l.iterator();

        if (iter.hasNext()) {

            p = (Person) iter.next();

        }

        return p;

    }

}

                                           TestPO.java

package com;

/**

 *

 * @author Administrator

 */

public class TestPO {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        PersonOperate po = new PersonOperate();

        Person p = po.queryById("123");

        System.out.print(p.getName());

    }

}

 

hibernate(学习5)

查询全部

package com;

import java.util.Iterator;

import java.util.List;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

/**

 *

 * @author Administrator

 */

public class PersonOperate {

    private Session session = null;

    public PersonOperate() {

        Configuration config = new Configuration().configure();

        SessionFactory factory = config.buildSessionFactory();

        this.session = factory.openSession();

    }

    // 查询全部数据,写HQL

    public List queryAll() {

        List l = null;

        String hql = "FROM Person as p";

        Query q = this.session.createQuery(hql);

        l = q.list();

        return l;

    }

}

                              TestPO.java

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package com;

import java.util.Iterator;

import java.util.List;

/**

 *

 * @author Administrator

 */

public class TestPO {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        PersonOperate po = new PersonOperate();

        List l = po.queryAll();

        Iterator iter = l.iterator();

        while (iter.hasNext()) {

            Person p = (Person) iter.next();

            System.out.println(p.getId());

        }

    }

}

 

hibernate(学习6)

模糊查询

 

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package com;

import java.util.List;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

/**

 *

 * @author Administrator

 */

public class PersonOperate {

    private Session session = null;

    public PersonOperate() {

        Configuration config = new Configuration().configure();

        SessionFactory factory = config.buildSessionFactory();

        this.session = factory.openSession();

    }

    // 模糊查询

    public List queryByLike(String cond) {

        List l = null;

        String hql = "FROM Person as p WHERE p.name like ?";

        Query q = this.session.createQuery(hql);

        q.setString(0, "%" + cond + "%");

        l = q.list();

        return l;

    }

}

 

 

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package com;

import java.util.Iterator;

import java.util.List;

/**

 *

 * @author Administrator

 */

public class TestPO {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        PersonOperate po = new PersonOperate();

        List l = po.queryByLike("qun");

        Iterator iter = l.iterator();

        while (iter.hasNext()) {

            Person p = (Person) iter.next();

            System.out.println(p.getName());

        }

    }

}

 

hibernate(学习7)

删除操作

 

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package com;

import java.util.Iterator;

import java.util.List;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

/**

 *

 * @author Administrator

 */

public class PersonOperate {

    private Session session = null;

    public PersonOperate() {

        Configuration config = new Configuration().configure();

        SessionFactory factory = config.buildSessionFactory();

        this.session = factory.openSession();

    }

    // 按ID查询:推荐使用HQL —— 是Hibernate官方推荐的查询语言

    public Person queryById(String id) {

        Person p = null;

        // 使用Hibernate查询语言

        String hql = "FROM Person as p WHERE p.id=?";

        // 通过Query接口查询

        Query q = this.session.createQuery(hql);

        q.setString(0, id);

        List l = q.list();

        Iterator iter = l.iterator();

        if (iter.hasNext()) {

            p = (Person) iter.next();

        }

        return p;

    }

    // 删除数据

    // Hibernate2、Hibernate 3通用的删除

    // 使用此方法删除数据之前,必须先查找到数据对象,性能呢?,效率低,先查询,在删除

    public void delete(Person p) {

        Transaction tran = this.session.beginTransaction();

        // 执行语句

        this.session.delete(p);

        // 提交事务

        tran.commit();

    }

    // 在Hibernate 3之中根据HQL中的语句进行了修改,增加了删除指令 ,推荐使用此方法

    public void delete(String id) {

        String hql = "DELETE Person WHERE id=?";

        Query q = this.session.createQuery(hql);

        // 把参数设置

        q.setString(0, id);

        // 执行更新语句

        q.executeUpdate();

        // 进行事务处理

        this.session.beginTransaction().commit();

    }

}

 

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package com;

import java.util.Iterator;

import java.util.List;

/**

 *

 * @author Administrator

 */

public class TestPO {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

       /*

        PersonOperate po = new PersonOperate();

        Person p=po.queryById("123");

        System.out.println(p.getName());

        po.delete(p);

         */

       

        PersonOperate po = new PersonOperate();

        po.delete("我爱飞飞哦");

    }

}

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息