hibernate 非xml实体类配置方法!

hibernate 非xml实体类配置方法!

    这个是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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

 <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/cms?useUnicode=true&amp;characterEncoding=UTF-8</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
         
        <!--  test2 注入Entity.class-->
        <mapping  class="com.bird.entity.JcChannel"/>
        <mapping  class="com.bird.entity.JcChannelExt"/>
        <mapping  class="com.bird.entity.JcChnlGroupContri"/>
        <mapping  class="com.bird.entity.JcSiteFlow"/>
        <mapping  class="com.bird.entity.ITest"/>
     
    </session-factory>


</hibernate-configuration>
View Code

其中          <mapping  class="com.bird.entity.ITest"/>   指向类名。

下面是这个类的代码,其中用了ITest注解。

package com.bird.entity;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "i_test")
public class ITest implements Serializable {

    private static final long serialVersionUID = 1L;
    private int id;
    private String  name;
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    } 
    
}
View Code

 

最重要的是工具类 HibernateSessionFactory.java  ,

因为采用了 Configuration configuration = new AnnotationConfiguration();  这段代码创建Configuration ,所以才能不用去写实体类的xml配置文件了。

记住这个类:new AnnotationConfiguration();  

(这个类要用     session.beginTransaction().commit();  提交请求!)

 

TestUti.java   测试类代码,含有删改查功能

package com.bird.channel;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.bird.entity.JcChannel;
import com.bird.entity.JcChannelExt;
import com.bird.entity.JcChnlGroupContri;
import com.bird.entity.JcSiteFlow;
import com.bird.util.HibernateSessionFactory;

public class TestUtil {
     //查询的例子
    public List<JcChannel> getChannelList() {
        Session session = HibernateSessionFactory.getSession();
        String hql = "from JcChannel where  parent_id is not null ";
        Query query = session.createQuery(hql);
        List<JcChannel> jchList = (List<JcChannel>) query.list();
        for (int i = 0; i < jchList.size(); i++) {
            JcChannel jcEn = jchList.get(i);
            System.out.println(jcEn.getChannel_id());
        }
        return jchList;
    }
    //查询最大id的例子
    public int getChannelMaxIdByHql() {
        int id = 0;
        Session session = HibernateSessionFactory.getSession();
        String hql = "select max(channel_id) from JcChannel";
        Query query = session.createQuery(hql);
        List jchList = query.list();
        if (jchList.size() > 0) {
            id = Integer.parseInt(jchList.get(0).toString());
        }
        return id;
    }
    //查询最大id的例子
    public int getChannelMaxIdBySql() {
        int id = 0;
        Session session = HibernateSessionFactory.getSession();
        String hql = "select max(channel_id) from jc_channel ";
        Query query = session.createSQLQuery(hql);
        List jchList = query.list();
        if (jchList.size() > 0) {
            id = Integer.parseInt(jchList.get(0).toString());
        } 
        return id;
    }
    // 修改的例子
     public int updateChannelMaxId() {
     Session session = HibernateSessionFactory.getSession();
     String hql = "update jc_channel  t set t.rgt = 2  ";
     Query query = session.createSQLQuery(hql);
     int over = query.executeUpdate();
     session.beginTransaction().commit(); 
     return over;
     }
    // 删除的例子
     public int deleteChannelMaxId(int id) {
     Session session = HibernateSessionFactory.getSession();
     String hql = "delete from  jc_channel  where channel_id = ?  ";
     Query query = session.createSQLQuery(hql).setParameter(0, id);
     int over = query.executeUpdate();
     session.beginTransaction().commit(); 
     return over;
     }
  
}
View Code

 

 

 

 

posted on 2013-08-09 14:20  BIGBIRD大鸟  阅读(515)  评论(0编辑  收藏  举报