映射文件:
ShoeModel.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.kaisheng.pojo"> <class name="ShoeModel" table="t_shoemodel"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="modelName" column="modelname"></property> <many-to-one name="user" class="User" column="uid"></many-to-one> <set name="storeSet"> <key column="mid"></key> <one-to-many class="Store"/> </set> </class> </hibernate-mapping>
Store.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.kaisheng.pojo"> <class name="Store" table="t_store"> <id name="id" column="id"><generator class="native"></generator></id> <property name="shoeSize" column="shoesize"></property> <property name="shoeNum" column="shoenum"></property> <property name="shoeCode" column="shoecode"></property> <property name="storeTime" column="storetime"></property> <many-to-one name="model" class="ShoeModel" column="mid"></many-to-one> </class> </hibernate-mapping>
DAO与测试
package com.kaisheng.dao; import java.util.List; import org.hibernate.Query; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.kaisheng.pojo.Store; import com.kaisheng.util.Pages; @Repository public class StoreDao extends HibernateDaoSupport implements Idao<Store> { @SuppressWarnings("unchecked") public Pages<Store> findAllStayByPageNo(int pageNo) { Pages<Store> pages=new Pages<Store>(getAllStayCountSize()); pages.setPageSize(10); String hql="from Store where storestate=1"; Query query=getSession().createQuery(hql); query.setFirstResult(pages.getStart(pageNo)); query.setMaxResults(pages.getPageSize()); List<Store> list=query.list(); pages.setPageList(list); return pages; } /** * 查询全部未发帮记录 * @return */ public int getAllStayCountSize(){ String hql="select count(*) from Store where storeState=1"; return Integer.parseInt(getSession().createQuery(hql).uniqueResult().toString()); } public Store findById(int id) { return null; } /** * 保存或修改 */ public void save(Store store) { getHibernateTemplate().saveOrUpdate(store); } public void updateById(int id) { } @SuppressWarnings("unchecked") public List<Store> list(String hql) { List<Store> list=getSession().createQuery(hql).list(); return list; } public List<Store> findAll() { return null; } public static void main(String[] args){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); StoreDao dao=(StoreDao) context.getBean("storeDao"); Pages<Store> pages=dao.findAllStayByPageNo(1); System.out.println(pages.getPageList().get(0).getModel().getModelName()); } }