思路:
若模糊查询只涉及到第一张表,则在查询的时候,查询语句应当为第一张为主表;
若模糊查询字段涉及得到第一张表和第二张表时,那么在访问数据库的hql语句中,应当设第二张表为主要查询的表;
如果模糊查询字段涉及到三张表时,那么在访问数据库的hql语句中,应当设第三张表为主要查询的表;
项目工程图
Action类
package com.mingde.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import com.mingde.dao.IBaseDao;
import com.mingde.dao.impl.BaseDaoImpl;
import com.mingde.po.Bus_company;
import com.mingde.po.Bus_route;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class BAction extends ActionSupport {
private IBaseDao bd=new BaseDaoImpl();
private List<Bus_route> rlist=new ArrayList<>();
private List<Bus_company> clist=new ArrayList<>();
private Map<Integer,Bus_company> map=new HashMap<>();
private String bcname;
private String brterminus;
private String bregcefdvp;
public String list()throws Exception{
//构造hql语句
String hql="from Bus_route where 1=1 " ;
if(bcname!=null && !"".equals(bcname)){
hql+=" and company.bcname like '%"+bcname+"%' ";
}
if(brterminus!=null && !"".equals(brterminus)){
hql+=" and brterminus like '%"+brterminus+"%'";
}
if(bregcefdvp!=null && !"".equals(bregcefdvp)){
hql+=" and bregcefdvp like '%"+bregcefdvp+"%'";
}
//执行hql语句,得到结果集合
rlist=bd.findAll(hql);
//通过for循环去除重复公司(将符合条件的公司放入map集合中,再将符合条件的路线放入对应的公司)
for(Bus_route r:rlist){ //将所有查询到的路线拿出来
if(r.getCompany()!=null){ //如果该路线的公司不为空的话就执行一下代码
//如果map集合不包含该路线公司的id的话,就将该路线公司的id和该公司一起放入map集合中,
//然后将放入到map集合中的公司new一个HashSet来存放路线
if(!map.containsKey(r.getCompany().getBcid())){
map.put(r.getCompany().getBcid(), r.getCompany());
r.getCompany().setRoutes(new HashSet<>());
r.getCompany().getRoutes().add(r); //将路线放入map集合中对应的公司
}else{//否则如果map集合包含了该公司的话,那么就将该路线直接放如map集合中所对应的公司
map.get(r.getCompany().getBcid()).getRoutes().add(r);
}
}
}
System.out.println(map);
return "list";
}
//所有的属性的get和set方法在此省略……
}
JSP显示页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>列表</h2>
<table align="center" width="800">
<tr>
<td colspan=6>
<s:form action="bus_list" theme="simple">
公司名称:<s:textfield name="bcname"></s:textfield>
起始站:<s:textfield name="brterminus"></s:textfield>
终点站:<s:textfield name="bregcefdvp"></s:textfield>
<s:submit value="查询"></s:submit>
</s:form>
</td>
</tr>
<tr>
<th>公司ID</th><th>公司名称</th><th>公司地址</th><th>负责人</th><th>电话</th><th>操作</th>
</tr>
<s:iterator value="map.values()">
<tr align="center">
<td><s:property value="bcid"/></td>
<td><s:property value="bcname"/></td>
<td><s:property value="bcaddress"/></td>
<td><s:property value="bcprincipal"/></td>
<td><s:property value="bctel"/></td>
<td>
修改
删除
</td>
</tr>
<tr>
<td colspan=6>
<table align="center" border=1 rules="all" bgcolor="pink">
<tr>
<th>路线编号</th><th>路线名称</th><th>起始站</th><th>终点站</th><th>开班时间</th><th>结束时间</th><th>操作</th>
</tr>
<s:iterator value="routes">
<tr>
<td><s:property value="brid"/></td>
<td><s:property value="brname"/></td>
<td><s:property value="brterminus"/></td>
<td><s:property value="bregcefdvp"/></td>
<td><s:property value="brstart"/></td>
<td><s:property value="brend"/></td>
<td>
修改
删除
</td>
</tr>
</s:iterator>
</table>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>
其他的配置
Dao层
package com.mingde.dao.impl;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.mingde.dao.IBaseDao;
public class BaseDaoImpl implements IBaseDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public List findAll(String hql) {
Session session =sessionFactory.getCurrentSession();
return session.createQuery(hql).list();
}
}
实体类
Bus_company.java(公司:第一张表)
public class Bus_company {
private int bcid;
private String bcname;
private String bcaddress;
private String bcprincipal;
private String bctel;
private Set<Bus_route> routes =new HashSet<>();
}
Bus_route.java(路线:第二张表)
public class Bus_route {
private int brid;
private String brname;
private String brterminus;
private String bregcefdvp;
private String brstart;
private String brend;
private Bus_company company;
private Set<Bus_emp> emps = new HashSet<>();
}
Bus_emp.java(员工:第三张表)
public class Bus_emp {
private int eid;
private String ename;
private String esex;
private Date incomedate;
private String eaddress;
private Bus_route route;
}
hbm.xml文件配置
Bus_company.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 2017-8-24 19:14:15 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.mingde.po.Bus_company" table="BUS_COMPANY">
<id name="bcid" type="int">
<column name="BCID" />
<generator class="sequence" >
<param name="sequence">sequ_busc</param>
</generator>
</id>
<property name="bcname" type="java.lang.String">
<column name="BCNAME" />
</property>
<property name="bcaddress" type="java.lang.String">
<column name="BCADDRESS" />
</property>
<property name="bcprincipal" type="java.lang.String">
<column name="BCPRINCIPAL" />
</property>
<property name="bctel" type="java.lang.String">
<column name="BCTEL" />
</property>
<set name="routes" table="BUS_ROUTE" inverse="true" lazy="false" fetch="join">
<key>
<column name="BCID" />
</key>
<one-to-many class="com.mingde.po.Bus_route" />
</set>
</class>
</hibernate-mapping>
Bus_route.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 2017-8-24 19:14:15 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.mingde.po.Bus_route" table="BUS_ROUTE">
<id name="brid" type="int">
<column name="BRID" />
<generator class="sequence" >
<param name="sequence">sequ_busr</param>
</generator>
</id>
<property name="brname" type="java.lang.String">
<column name="BRNAME" />
</property>
<property name="brterminus" type="java.lang.String">
<column name="BRTERMINUS" />
</property>
<property name="bregcefdvp" type="java.lang.String">
<column name="BREGCEFDVP" />
</property>
<property name="brstart" type="java.lang.String">
<column name="BRSTART" />
</property>
<property name="brend" type="java.lang.String">
<column name="BREND" />
</property>
<many-to-one name="company" class="com.mingde.po.Bus_company" fetch="join" lazy="false">
<column name="BCID" />
</many-to-one>
<set name="emps" table="BUS_EMP" inverse="true" lazy="false">
<key>
<column name="BRID" />
</key>
<one-to-many class="com.mingde.po.Bus_emp" />
</set>
</class>
</hibernate-mapping>
Bus_emp.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 2017-8-24 19:14:15 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.mingde.po.Bus_emp" table="BUS_EMP">
<id name="eid" type="int">
<column name="EID" />
<generator class="sequence" >
<param name="sequence">bus_emp</param>
</generator>
</id>
<property name="ename" type="java.lang.String">
<column name="ENAME" />
</property>
<property name="esex" type="java.lang.String">
<column name="ESEX" />
</property>
<property name="incomedate" type="java.sql.Date">
<column name="INCOMEDATE" />
</property>
<property name="eaddress" type="java.lang.String">
<column name="EADDRESS" />
</property>
<many-to-one name="route" class="com.mingde.po.Bus_route" fetch="join" lazy="false">
<column name="BRID" />
</many-to-one>
</class>
</hibernate-mapping>
Spring配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="user" value="scott" ></property>
<property name="password" value="123" ></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置Hibernate常用属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
<!-- 配置加载映射文件 -->
<property name="mappingResources">
<list>
<value>com/mingde/po/Bus_company.hbm.xml</value>
<value>com/mingde/po/Bus_route.hbm.xml</value>
<value>com/mingde/po/Bus_emp.hbm.xml</value>
</list>
</property>
</bean>
<!-- 如下进行声明式事务的配置 -->
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
<!-- 定义通知 -->
<tx:advice id="myadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 使用AOP配置声明式事务 -->
<aop:config>
<!-- 配置aop切入点,即在哪个类的哪个方法中调用相关的代码(定义调用的位置) -->
<aop:pointcut expression="execution(* com.mingde.dao..*.*(..))" id="myponintcut1"/>
<!-- 定义访问者 -->
<aop:advisor advice-ref="myadvice" pointcut-ref="myponintcut1"/>
</aop:config>
</beans>
applicationDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bd" class="com.mingde.dao.impl.BaseDaoImpl">
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.devMode" value="true"></constant>
<package name="struts" extends="struts-default">
<action name="*_*" class="com.mingde.action.BAction" method="{2}">
<result name="{2}">/WEB-INF/{1}/{2}.jsp</result>
<result name="to_list" type="redirect">{1}_list</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Spring_008_AOP</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Struts过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载在classpath目录下的配置文件 (有了这个配置才能加载classpath目录下的applicationContext.xml文件) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application*.xml</param-value>
</context-param>
</web-app>