Wednesday, March 23, 2016

Creating Primary and foreign key relationship between two tables in mysql

Primary and foreign key relationship between two tables

1.First we need to create a Primary key field table
CREATE TABLE Sale(
    sale_id CHAR(40),
    PRIMARY KEY(sale_id),
    discount DOUBLE,
    type VARCHAR(255),
    price DOUBLE
) ENGINE=INNODB;

2.After creating primary field table, we have to create second table 

CREATE TABLE Normal_Sale(
    sale_id CHAR(40),
    PRIMARY KEY(sale_id),
    FOREIGN KEY (sale_id) REFERENCES Sale(sale_id)
) ENGINE=INNODB;

Sunday, March 13, 2016

XML Request and Response from Spring restful web services using POJO

How to call a spring restful web service to consume and produce XML as request and response using a Pojo class.

1.I have a Pojo class called as ItemDTO,which looks like this to support xml as input to the service

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 = "ITEM_STOCK")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemDTO implements Serializable {


private static final long serialVersionUID = 4520881241472376875L;

@XmlElement
private String item_name;
@XmlElement
private String item_desl;
@XmlElement
private int item_count;
   
       //Setters and Getters of the variables above.
}

2.Spring rest Controller

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ims.xml.DTO.ItemDTO;


@Controller
@RequestMapping(value="ims")
public class InventoryHomeController {

@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)

public @ResponseBody ItemDTO home(Locale locale, Model model) {

model.addAttribute("serverTime", formattedDate);
ItemDTO i = new ItemDTO("renolds", "ballpointpen", 10);

return i;
}
}

OUT PUT IS:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>

<ITEM_STOCK>

<item_name>renolds</item_name>

<item_desl>ballpointpen</item_desl>

<item_count>10</item_count>

</ITEM_STOCK>