多表联合查询,及更改访问url

一. 多表连接一般分为内连接和外连接,今天重点讲解外连接。外连接又包括左外连接和右外连接,我这个项目用的是左外连接,所以就拿左外连接举例吧。

  1. 左连接:left join 或 left outer join

    (1).左连接顾名思义,就是从左边表开始查询,但是如果左边表的某行在右表中没有匹配,那么相关联的结果集中右表的列表的将是空值(null)。

    (2). sql语句

String sql="select a.*,b.type_name,c.vendor_name,d.office_name from t_m3s_device  a" +
        " left join t_m3s_device_type b on a.type_id=b.type_id left join t_m3s_vendor c" +
        " on a.manufacturer=c.vendor_id left join t_m3s_office d on a.office_id=d.office_id" +
        " where a.device_id=?";

注释:我这里加的where条件,是因为一会我只需查询单条记录即可,并不是所有查询,所以加了where条件,如果是查询全部的话,即可去掉。

2.右连接:right join 或 right outer join

二.更改访问url

1.首先是更改struts的配置

<struts>
    <package name="deviceWeb" extends="common">
        <action name="app" class="deviceWebAction" method="deviceWebList">
            <result>device.jsp</result>
        </action>
    </package>

</struts>

2.其次是更改shiro配置,让其成为“游客”身份,也就是不用登陆,任何人都可以访问

<property name="filterChainDefinitions">
            <value>
                /app = anon
                /${adminPath}/login.action = authc
                /${adminPath}/logout.action = logout
            </value>
        </property>

注释:具体细节就不往上发表了,anon代表的就是“游客”身份

3.由于需求是查询单条记录,规定的访问路径是sid就是对应的数据库中的device_id,后面的数字也是可以变化的,所以前台就需要这样写

<s:iterator value="deviceList" var="item">
            <form action="${ctx}/app?sid=<s:property value="device_id" />" method="post">
                <table id="contentTable" class="table table-bordered">
                    <tr>
                        <td>设备编号:<s:property value="device_id" /></td>
                    </tr>
                    <tr>
                        <td>设备类型:<s:property value="type_name" /></td>
                    </tr>
                    .
                    .
                    .
                    .
                </table>
            </form>
        </s:iterator>

注释:由于后台返回是一个list集合,所以就用了struts的iterator标签

4.所以后台就需要获得前台的sid,并且给它赋值,否则sid就会是空值

    private List<DeviceWeb> deviceList;
    private String sid;     //前台sid

    public String deviceWebList(){

        try{
            this.setActionErrors(actionErrors);
            DeviceWeb d=new DeviceWeb();
            sid=request.getParameter("sid");        
            d.setDevice_id(sid);                //将前台sid赋值
            this.deviceList=this.deviceWebService.deviceWebList(d);

        }catch(Exception e){
            this.addActionError("查询设备失败");
            e.printStackTrace();
        }

        return SUCCESS;
    }
    public List<DeviceWeb> getDeviceList() {
        return deviceList;
    }
    public void setDeviceList(List<DeviceWeb> deviceList) {
        this.deviceList = deviceList;
    }
    public String getSid() {
        return sid;
    }
    public void setSid(String sid) {
        this.sid = sid;
    }

注释:切记不要忘记给sid赋值,我就是忘记了赋值,所以一开始是空值;还有就是不要忘记给sid和deviceList写set,get方法,这也是很容易忘掉的

5.看一下效果,界面是这样滴
这里写图片描述

注释:我前台是用bootstrap3写的,所以pc端和app端都支持,兼容效果也不错

有什么不足之处,欢迎大家给我留言,谢谢大家。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值