2011年11月16日

(转贴)数据库连接(内连接,外连接,交叉连接)

数据库连接分为:内连接,外连接(左、右连接,全连接),交叉连接
文章地址 : http://www.zxbc.cn/html/20080527/51189.html
转载 
内连接:把两个表中数据对应的数据查出来 
外连接:以某个表为基础把对应数据查出来(全连接是以多个表为基础) 
student表 
no name 
1     a 
2     b 
3     c 
4     d 
grade表 
no grade 
1     90 
2     98 
3     95 
内连接 inner join(查找条件中对应的数据,no4没有数据不列出来) 
语法:select * from student inner join grade on student.no = grade.no 
结果 
student.no name grade.no grade 
1             a             1         90 
2             b             2         98 
3             c             3         95 
左连接(左表中所有数据,右表中对应数据) 
语法:select * from student left join grade on student.no = grade.no 
结果: 
student.no name grade.no grade 
1                 a         1         90 
2                 b         2         98 
3                 c         3         95 
4                 d     
右连接(右表中所有数据,左表中对应数据) 
语法:select * from student right join grade on student.no = grade.no 
结果: 
student.no name grade.no grade 
1                 a         1         90 
2                 b         2         98 
3                 c         3         95 
全连接 
语法:select * from student full join grade on student.no = grade.no 
结果: 
no name grade 
1     a     90 
2     b     98 
3     c     95 
4     d 
1     a     90 
2     b     98 
3     c     95 
注:access 中不能直接使用full join ,需要使用union all 将左连接和右连接合并后才可以

交叉连接
将两个表所有行组合,连接后的行数为两个表行数的乘积(笛卡尔积)
语法,借用上面的例子应该是
select * from student cross join grade

行数应该为12行 :
no name grade 
1     a     90 
2     b     98 
3     c     95 
4     d  
1     a     90 
2     b     98 
3     c     95 
4     d 
1     a     90 
2     b     98 
3     c     95 
4     d 

posted @ 2011-11-30 17:24 AK47 阅读(475) | 评论 (0)编辑 收藏

JAXB向Xml非根节点添加一个或多个属性

JAXB 向Xml非根节点添加一个或多个属性,直接上代码,关于JAXB的相关注解可查阅JAVA API。

原创文章,转载请注明出处。http://www.blogjava.net/kangdy/archive/2011/11/23/364635.html

code1: colors类  根节点
code1
package com.kangdy.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Colors")
@XmlAccessorType(XmlAccessType.FIELD)
public class Colors {
    
    @XmlElement(name = "red",nillable=true)
    private Red red;
    
    @XmlElement(name = "blue",nillable=true)
    private Blue blue;

    public Red getRed() {
        return red;
    }

    public Blue getBlue() {
        return blue;
    }

    public void setRed(Red red) {
        this.red = red;
    }

    public void setBlue(Blue blue) {
        this.blue = blue;
    }
}

code2:  Red类  子节点
code2package com.kangdy.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "red")
@XmlAccessorType(XmlAccessType.FIELD)
public class Red {
    
    private String value;
    
    @XmlAttribute(name = "att1")
    private String att;
    
    public String getValue() {
        return value;
    }
    
    public void setValue(String value) {
        this.value = value;
    }

    public String getAtt() {
        return att;
    }

    public void setAtt(String att) {
        this.att = att;
    }
    
}


code3:  类 Blue 子节点
code3
package com.kangdy.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "blue")
@XmlAccessorType(XmlAccessType.FIELD)
public class Blue {
    private String value;
    
    @XmlAttribute(name = "att2")
    private String att2;
    
    @XmlAttribute(name = "att1")
    private String att;
    
    public String getAtt() {
        return att;
    }

    public void setAtt(String att) {
        this.att = att;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getAtt2() {
        return att2;
    }

    public void setAtt2(String att2) {
        this.att2 = att2;
    }
}

code4: main类
code4
package com.kangdy.test;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Jaxbtest {
    public static void main(String[] args) throws Exception {

        StringWriter writer = new StringWriter();
        JAXBContext jc = JAXBContext.newInstance(Colors.class);
        Marshaller ma = jc.createMarshaller();
        ma.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        
        Colors colors = new Colors();
        Red red = new Red();
        red.setAtt("att-red");
        red.setValue("red");
        Blue blue = new Blue();
        blue.setValue("blue");
        blue.setAtt("att-blue");
        blue.setAtt2("blue-att2");
        colors.setRed(red);
        colors.setBlue(blue);
        
        ma.marshal(colors, writer);
        System.out.println(writer.toString());

    }
}

运行结果:
结果
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Colors>
    <red att1="att-red">
        <value>red</value>
    </red>
    <blue att1="att-blue" att2="blue-att2">
        <value>blue</value>
    </blue>
</Colors>

posted @ 2011-11-23 14:33 AK47 阅读(10080) | 评论 (4)编辑 收藏

(转载)关于paramsPrepareParamsStack

原帖地址:
http://hi.baidu.com/%CC%AB%C6%BD%D1%F31986/blog/item/110b13b1384e805e08230259.html
转贴

paramsPrepareParamsStack在Struts 2.0中是一个很奇妙的interceptor stack,以至于很多人疑问为何不将其设置为默认的interceptor stack。paramsPrepareParamsStack主要解决了ModelDriven和Preparable的配合问题,从字面上理解来说, 这个stack的拦截器调用的顺序为:首先params,然后prepare,接下来modelDriven,最后再params。Struts 2.0的设计上要求modelDriven在params之前调用,而业务中prepare要负责准备model,准备model又需要参数,这就需要在 prepare之前运行params拦截器设置相关参数,这个也就是创建paramsPrepareParamsStack的原因。流程如下:
   1. params拦截器首先给action中的相关参数赋值,如id  
   2. prepare拦截器执行prepare方法,prepare方法中会根据参数,如id,去调用业务逻辑,设置model对象
   3. modelDriven拦截器将model对象压入value stack,这里的model对象就是在prepare中创建的
   4. params拦截器再将参数赋值给model对象
   5. action的业务逻辑执行 依据此stack,一个action的代码通常如下

public class UserAction extends ActionSupport implements ModelDriven, Preparable {
    private User user;
    private int id;
    private UserService service; // user business service

    public void setId(int id) {
        this.id = id;
    }

    /**
     * create a new user if none exists, otherwise load the user with the
     * specified id
     */
    public void prepare() throws Exception {
        if (id == 0) {
            user = new User();
        } else {
            user = service.findUserById(id);
        }
    }

    public Object getModel() {
        return user;
    }

    /**
     * create or update the user and then view the created user
     */
    public String update() {
        if (id == 0) {
            service.create(user);
        } else {
            service.update(user);
        }
        return "redirect";
    }

    /**
     * delete the user and go to a default home page
     */
    public String delete() {
        service.deleteById(id);
        return "home";
    }

    /**
     * show the page allowing the user to view the existing data
     */
    public String view() {
        return "view";
    }

    /**
     * show the page allowing the user to view the existing data and change the
     * values
     */
    public String edit() {
        return "input";
    }

在上述代码中,edit和view都不需要根据id再为界面准备数据,因为prepare方法已经准备好了model,这些方法很简单。对于update 方法,prepare首先会从数据库中加载数据,然后params拦截器会将参数值付给model,在update直接更新就可以,不会出现数据被乱更新 的情况。象Hibernate框架,会判断哪些字段更新了,然后进行更新,性能也不会损失。
通过paramsPrepareParamsStack可以让流程更明确,代码更简洁,也更利于大家的交流。

posted @ 2011-11-16 15:39 AK47 阅读(425) | 评论 (0)编辑 收藏

<2011年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜