1、xml version=1.0 encoding=UTF-8 beans xmlns=http:/www.springframework.org/schema/beans xmlns:xsi=/www.w3.org/2001/XMLSchema-instancep=/www.springframework.org/schema/paop=/www.springframework.org/schema/aopcontext=/www.springframework.org/schema/contextjee=/www.springframework.org/schema/jeetx=/www.s
2、pringframework.org/schema/tx xsi:schemaLocation=/www.springframework.org/schema/aop http:/www.springframework.org/schema/aop/spring-aop-2.5.xsd /www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-2.5.xsd /www.springframework.org/schema/context /www.springframewor
3、k.org/schema/context/spring-context-2.5.xsd /www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-2.5.xsd /www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-2.5.xsd bean id=dataSource class=mons.dbcp.BasicDataSource usernamerootpasswordhl1437
4、driverClassNamecom.mysql.jdbc.Driver/beansessionFactoryorg.springframework.orm.hibernate3.LocalSessionFactoryBean ref=hibernateProperties hibernate.show_sqltruemappingResourceslistvaluecom/luo/ssh/entity/Address.hbm.xml/listaddressDaocom.luo.ssh.dao.impl.AddressDaoHibernateImpladdressServicecom.luo.
5、ssh.service.impl.AdressServiceImpladdressActioncom.luo.ssh.action.AddressAction/beans5.hibernate映射配置(基本)utf-8!DOCTYPE hibernate-mapping PUBLIC -/Hibernate/Hibernate Mapping DTD 3.0/ENhibernate-mappingclass name=com.luo.ssh.entity.Address table=address catalog=photoid name=id type=integercolumn name=
6、 /generator class=native/generator/idnamestring length=50level/upidinfo/class/hibernate-mapping6.struts配置DOCTYPE struts PUBLIC -/Apache Software Foundation/DTD Struts Configuration 2.1.7/EN/struts.apache.org/dtds/struts-2.1.7.dtdstrutspackage name=ssh extends=struts-defaultaction name=list method=pa
7、ram name=pageSize36result/list.jsp/actiondetail/detail.jsp/struts7.web.xml配置web-app version=2.5 xmlns= xmlns: xsi:- 指明spring容器位置 -context-paramparam-namecontextConfigLocationparam-valueclasspath:ssh.xml/context-paramlistenerlistener-class org.springframework.web.context.ContextLoaderListener/listene
8、r-class/listenerfilterfilter-namestruts2filter-class org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter/filter-class/filterfilter-mappingurl-pattern/*/filter-mappingwelcome-file-listwelcome-fileindex.jsp/welcome-file-list/web-app8.实体类Addresspackage com.luo.ssh.entity;import java.i
9、o.Serializable;/* * 地名类 * author 罗立明 * */public class Address implements Serializable /*地名的id,唯一*/ private int id; /*地名*/ private String name; /*地名的下属级别,直辖市,省,自治区为1,其他没下一级级别+1*/ private int level; /*地名上一级的id,直辖市,省,自治区的上级id为0*/ private int upid; /*保留字段,地名备注及描述*/ private String info; public int getId(
10、) return id; public void setId(int id) this.id = id; public String getName() return name; public void setName(String name) this.name = name; public int getLevel() return level; public void setLevel(int level) this.level = level; public int getUpid() return upid; public void setUpid(int upid) this.up
11、id = upid; public String getInfo() return info; public void setInfo(String info) this.info = info; Override public int hashCode() return id16; public boolean equals(Object obj) if (this = obj) return true; if (obj = null) return false; if (getClass() != obj.getClass() Address other = (Address) obj;
12、if (id != other.id) return true; public String toString() return id:+id+n地名:+name+n级别:+level+n上一级id:+upid;9.dao接口定义package com.luo.ssh.dao;import java.util.List;import com.luo.ssh.entity.Address;public interface AddressDao /* * 查询地址总数 * return public int count(); * 根据id查找地址 * param id public Address
13、 findById(int id); * 根据id查找所有下级地址 * param address public List findChildren(Address address); * 根据id查找该地址的详细信息,追溯到最高上级 * return 结果形如:湖北省-荆州市-公安县-狮子口镇 public String findDetailById(int id); * 根据地名模糊查询所有满足条件的地址 * param name findLikeName(String name); * 分页查询地址信息 * param page * param pageSize findPage(int
14、 page,int pageSize); * 查询全部 findAll();10.dao的Hibernate实现package com.luo.ssh.dao.impl;import java.sql.SQLException;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.springframework.orm.hibernate3.HibernateCallback;import org.springframework.orm
15、.hibernate3.HibernateTemplate;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.luo.ssh.dao.AddressDao;public class AddressDaoHibernateImpl extends HibernateDaoSupport implements AddressDao public Address findById(int id) HibernateTemplate template=getHibernateTemplate
16、(); Address address=(Address)template.get(Address.class, id); return address; findChildren(Address address) String hql=from Address where upid=?; return getHibernateTemplate().find(hql, address.getId(); public String findDetailById(int id) Address add=findById(id); String detail=add.getName(); while
17、(add.getLevel()!=1) add=findById(add.getUpid(); detail=add.getName()+-+detail; return detail; findLikeName(String name) from Address where name like ? return getHibernateTemplate().find(hql, %); public int count() select count(*) from Address return Integer.parseInt(getHibernateTemplate().find(hql).
18、get(0).toString(); findPage(final int page, final int pageSize) List list=(List)template.execute(new HibernateCallback() public Object doInHibernate(Session session) throws HibernateException, SQLException String hql=from Address Query query=session.createQuery(hql); query.setFirstResult(page-1)*pageSize); query.setMaxResults(pageSize); return query.list(); ); return list; findAll() return getHibernateTemplate().find(hql);11
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1