码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate-ORM:17.Hibernate中多表分页携带条件

时间:2018-06-28 14:05:30      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:manage   1.3   where   resources   实体   关联关系   分页   utf-8   17.   

 

 

 

本次做个简单的多表带条件的分页,多对多,加多对一关联

 

1.三个实体类

  1.1Admin

 

public class Admin  {

   

    private Integer id;
    private Set<Notice> noticeSet=new <Notice>HashSet();
        //get/set方法
}

 

  1.2Notice通知

 

public class Notice {
    
    private int id;//流水好噢
    private String texts;通知内容
    private int readss;//是否查看
    private Date notTime;//通知时间

    private Set <Admin> adSet=new <Admin> HashSet();//和admin多对多    
    private Inform inform;      //通知类型
        //必须get/set方法
}

 

  1.3Inform 通知类型

 

public class Inform {

    
    private int id;
    private String classify;//通知类型

       //get/set方法  
}

 

2.他们对应的hbm.xml文件

  2.1Admin.hbm.xml

 

        <set name="noticeSet" table="Ad_not">
         <key column="a_id"></key>
         <many-to-many class="com.xy.model.Notice" column="n_id"></many-to-many>
        </set>



<!--这是配置的多对多-->

 

  2.2Inform.hbm.xml文件

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
     <!--此处要改为你的包-->
    <class name="com.dawn.model.Inform" table="inform" catalog="homework">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
              <generator class="identity" />
        </id>
        
        
        <property name="classify" type="java.lang.String">
            <column name="classify" length="100" />
       </property>
       
      </class>
      </hibernate-mapping>

 

  2.3Notice.hbm.xml

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<!--包名需要注意一下-->
<class name="com.dawn.model.Notice" table="notice" catalog="homework">
        <id name="id" type="java.lang.Integer">
        <column name="id"/>
        <generator class="identity" />
        </id>
        

   <property name="texts" type="java.lang.String">
     <column name="texts" length="1500"/>
   </property>
   
   <property name="readss" type="java.lang.Integer">
   <column name="readss"/>
   </property>

  <property name="notTime" type="java.util.Date">
   <column name="notTime"/>
   </property>
   
   <!-- 此处添加的是多对多的关系  新建一张表名为Ad_not示多对多关联关系-->
     <set name="adSet" table="Ad_not">
     <!-- key写自己的表的 -->
         <key column="n_id"></key>
         <!-- many-to-many写关联的那张表的 -->
         <!--包名需要注意一下-->
         <many-to-many class="com.dawn.model.Admin" column="a_id"></many-to-many>
        </set>
        <!-- 此处是关联的inform表  name是本表字段名,class是关联类的确切全路径,列是表中对应的列   千万记得lazy=false,就是不懒加载,如果懒加载查询之后,条页面会报错-->
<!--包名需要注意一下-->
    <many-to-one name="inform" class="com.dawn.model.Inform" column="informId" lazy="false"/>
</class>
</hibernate-mapping>

 

3.Hibernate在spring中的配置

  3.1SessionFactory的配置

 

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="current_session_context_class">thread</prop>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/dawn/model/Notice.hbm.xml</value>
                <value>com/dawn/model/Admin.hbm.xml</value>
                <value>com/dawn/model/Inform.hbm.xml</value>

                
                
            </list>
        </property>

 

  3.2事务的配置,用aop的方式

 

    <!-- xml方式事务的配置 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes> 
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <aop:config>
    <!-- 记得修改自己的包名 -->
        <aop:pointcut id="allManagerMethod"
            expression="execution(* com.dawn.action.*.*(..))" />
        <aop:advisor advice-ref="txAdvice"
            pointcut-ref="allManagerMethod" />
    </aop:config>

 

  3.3dao层的注入

 

    <!--DAO -->     
        <!--修改包名-->
    <bean id="adminDAO" class="com.dawn.dao.imp.AdminDAOImp">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean> 
         <!--修改包名-->
    <bean id="noticeDAO" class="com.dawn.dao.imp.NoticeDAOImp">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean> 

 

4.daoImpl的使用

package com.dawn.dao.imp;

import java.util.List;

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

import com.dawn.dao.NoticeDAO;
import com.dawn.model.Notice;
import com.dawn.util.Page;

public class NoticeDAOImp implements NoticeDAO{
    private SessionFactory sessionFactory;
    
    
    public Session getSessionFactory() {
        return sessionFactory.getCurrentSession();
    }


    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    @Override
    //分页返回集合
    public List<Notice> queryAll(Page page,int id) {
        String hql="from Notice n left outer join fetch n.adSet s where s.id=? "; 
        Query query=getSessionFactory().createQuery(hql).setParameter(0,id);
        query.setFirstResult((page.getPageIndex()-1)*page.getPageSize());
        List list=query.setMaxResults(page.getPageSize()).list();
        return list;
    } 
    
    //获取总数
    public int pageAll(int id) {
        String hql="from Notice n left outer join fetch n.adSet s where s.id=?"; 
        List list=getSessionFactory().createQuery(hql).setParameter(0,id).list();
        list.size();
        return list.size();
    } 
    

}

 

Hibernate-ORM:17.Hibernate中多表分页携带条件

标签:manage   1.3   where   resources   实体   关联关系   分页   utf-8   17.   

原文地址:https://www.cnblogs.com/DawnCHENXI/p/9237976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!