resteasy + mybatis 项目搭建

1.新建maven项目

pom.xml

  4.0.0
  com.baosight.webapp
  Invoice
  war
  0.0.1-SNAPSHOT
  Invoice Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
    
    	org.jboss.resteasy
    	resteasy-jaxrs
    	3.0.14.Final
    
    
    	org.jboss.resteasy
    	resteasy-jackson-provider
    	3.0.14.Final
    
    
    	org.jboss.resteasy
    	resteasy-multipart-provider
    	3.0.14.Final
    
    
    	com.github.growth2
    	lazybones-mybatis-generator-plugin
    	0.0.1
    



  

	
    	UTF-8
	


  
    Invoice
  

2.引入其他jar包

resteasy + mybatis 项目搭建_第1张图片
前两个jar包设置cors跨域
后两个jar包设置数据库连接

3.web.xml的配置


	Archetype Created Web Application

	
		resteasy-servlet
		
			org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
		
		
			javax.ws.rs.Application
			com.baosight.webapp.app.InvoiceApplication
		
	

	
		resteasy-servlet
		/services/*
	


	
		CORS
		com.thetransactioncompany.cors.CORSFilter
	
	
		CORS
		/*
	
	
		CORS
		/*
	

前面配置servlet映射
/services/*
//表示请求路径services下的路径是servlet请求
之前写成/*造成webapp下的页面无法请求
后面配置的是cors跨域
4.mybatis的配置
1)jdbc连接:jdbc.properties
jdbc.url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:xe
jdbc.username=briup
jdbc.password=briup
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
2)mybatis配值文件:configuration.xml



    
 	 
    
        
    


    
    
      
      
        
        
        
        
      
    
  

    
        
    
5.SessionFactory类
package com.baosight.webapp.common;

import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SessionFactory {
	private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static{
        try{
	//读取配值文件
            reader    = Resources.getResourceAsReader("com/baosight/webapp/Configuration.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getFactory(){
        return sqlSessionFactory;
    }
    public static SqlSession getSession(){
		SqlSession session = sqlSessionFactory.openSession();
		return session;
	}
}
6.application的编写
package com.baosight.webapp.app;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

import com.baosight.webapp.service.CheckStoreService;
import com.baosight.webapp.service.GetPostDataService;
import com.baosight.webapp.service.HelloWorldRestService;
import com.baosight.webapp.service.OptAccountService;
import com.baosight.webapp.service.OptCustomerService;
import com.baosight.webapp.service.OptIomenuService;
import com.baosight.webapp.service.OptOrderService;
import com.baosight.webapp.service.OptProductionService;
import com.baosight.webapp.service.OptPurchaseService;
import com.baosight.webapp.service.OptStorelistService;
import com.baosight.webapp.service.OptSupplierService;
import com.baosight.webapp.service.UploadFileService;
import com.baosight.webapp.service.getComdataService;

public class InvoiceApplication extends Application{
	private Set singletons = new HashSet();

	public InvoiceApplication() {
		
		singletons.add(new OptProductionService());
		
	}

	@Override
	public Set getSingletons() {
		return singletons;
	}
}
application同样需要在web.xml中配置 
    
   
每写一个sercvices需要在application中加入,生成对象

7.javabean文件:production.java
package com.baosight.webapp.bean;
//产品类
public class Production {
	//图片路径
	//picture category brand pNumber  pName  barcode bPrice sPrice
	private String picture;
	//大类
	private String category;
	//品牌
	private String brand;
//	货号
	private String pNumber;
//	品名
	private String pName;
	//条码
	private int barcode;
	//进价
	private double bPrice;
	//售价
	private double sPrice;


	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String  getpNumber() {
		return pNumber;
	}

	public void setpNumber(String pNumber) {
		this.pNumber = pNumber;
	}

	public String getpName() {
		return pName;
	}

	public void setpName(String pName) {
		this.pName = pName;
	}

	public int getBarcode() {
		return barcode;
	}

	public void setBarcode(int barcode) {
		this.barcode = barcode;
	}

	public double getbPrice() {
		return bPrice;
	}

	public void setbPrice(double bPrice) {
		this.bPrice = bPrice;
	}

	public double getsPrice() {
		return sPrice;
	}

	public void setsPrice(double sPrice) {
		this.sPrice = sPrice;
	}
}
8.sqlmap文件:production.xml




	

	



	

	

	
		UPDATE B_PRODUCTION
		SET
			PICTURE	= #{picture}
			WHERE
			PNUMBER = #{pNumber}
	


	
	    insert into
	    b_production(pNumber,category ,brand,   pName,  barcode, bPrice, sPrice)
  		values(#{pNumber},#{category},#{brand},#{pName},#{barcode},#{bPrice},#{sPrice})
  	

	
		DELETE FROM B_PRODUCTION WHERE
			PNUMBER = #{pNumber}
	
	
		UPDATE B_PRODUCTION
		SET
					CATEGORY	= #{category},
					BRAND	= #{brand},
					PNAME	= #{pName},
					BARCODE	= #{barcode},
					BPRICE	= #{bPrice},
					SPRICE	= #{sPrice}
			WHERE
			PNUMBER = #{pNumber}
	
9测试框架是否配置成功
package com.baosight.webapp.common;

import java.io.Reader;
import java.math.BigDecimal;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.baosight.webapp.bean.Production;

public class mybatisTest {
	private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static{
        try{
            reader    = Resources.getResourceAsReader("com/baosight/webapp/Configuration.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSession(){
        return sqlSessionFactory;
    }

    public static void main(String[] args) {
    	SqlSession session = sqlSessionFactory.openSession();
        try {
//        Production production = session.selectOne("com.baosight.webapp.bean.Production.selectProBypNumber",999);
//        List list = session.selectList("com.baosight.webapp.bean.Production.selectAll");
//          System.out.println(list.size());
        Production p=new Production();

        p.setbPrice(88.0);
        p.setBrand("h");
        p.setCategory("特步");
        p.setpName("运动鞋");
        p.setBarcode(78);
        p.setpNumber("9999");
        p.setsPrice(4.6);
        int pNumber=854;
        p.setPicture("yy");
        System.out.println(p.toString());
//        Production p2=session.selectOne("production.selectProBypNumber",854);
//        System.out.println(p2.toString());
//        session.insert("production.addPro",p);
//        session.delete("production.delete",p);
//        session.update("production.update",p);
//        session.insert("production.addPicture",p);

        System.out.println(p.toString());
        session.commit();
        }catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
        session.close();
        }
    }
}
10.编写services
package com.baosight.webapp.service;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import org.apache.ibatis.session.SqlSession;

import com.baosight.webapp.bean.Production;
import com.baosight.webapp.common.SessionFactory;
import com.baosight.webapp.common.splitParam;


//产品图片上传功能
//这里只能上传一张图片,如果有需求可以考虑用&字符分割上传路径
@Path("/services/optPro")
public class OptProductionService {
	private SqlSession session = SessionFactory.getSession();
	private splitParam sp=new splitParam();
	private Production p=new Production();
	//添加商品
	//pNumber picture category brand pName	barcode bPrice sPrice
	//http://localhost:8888/Invoice/optPro/add/parameters?pNumber=7&category=uu&brand=r&pName=oo&barcode=77&bPrice=6&sPrice=44
	@POST
	@Path("add")
	@Produces("application/json; charset=utf-8")
//	public String  addPro(String pNumber,String  category,String  brand,String pName,String barcode,String  bPrice,String  sPrice
//			) {
	public void  addPro(String data){
		System.out.println(data);
		String pNumber=sp.getParamValue(data, "pNumber");
		String  category=sp.getParamValue(data, "category");
		System.out.println(category);
		String  brand=sp.getParamValue(data, "brand");
		String pName=sp.getParamValue(data, "pName");
		String barcode=sp.getParamValue(data, "barcode");
		String bPrice=sp.getParamValue(data, "bPrice");
		String sPrice=sp.getParamValue(data, "sPrice");
		p.setpNumber(pNumber);
		p.setCategory(category);
		p.setBrand(brand);
		p.setpName(pName);
		p.setBarcode(Integer.parseInt(barcode));
		p.setbPrice(Double.parseDouble(bPrice));
		p.setsPrice(Double.parseDouble(sPrice));
		session.insert("production.addPro",p);
		session.commit();
	}

/*
	@POST
	@Path("add")
	public String  getName(String  pNumber,String category) {
		String result = "RESTEasy Hello World : " + pNumber+pNumber;

		return result;
	}
*/


	//添加图片
	//将这个功能融入文件上传的类中
	@GET
	@Path("addPicture/parameters")
	@Produces("application/json; charset=utf-8")
	public Response addPicture(@QueryParam("pNumber") String pNumber,
			@QueryParam("picture") String   picture){

		Production p=queryByPNumber(pNumber);
		p.setPicture(picture);
		session.update("production.addPicture", p);
		session.commit();
		return Response.status(201).entity(queryByPNumber(pNumber)).build();
	}

//	http://localhost:8888/Invoice/optPro/delProByPNumber/7
	//删除商品
	@GET
	@Path("delProByPNumber/{param}")
	@Produces("application/json; charset=utf-8")
	public Response delPro(@PathParam("param") String  pNumber){
		session.delete("production.delete", pNumber);
		session.commit();
		List list=queryAllPro();
		return Response.status(201).entity(list).build();
	}


	//修改商品
	@GET
	@Path("updatePro/parameters")
	@Produces("application/json; charset=utf-8")
	public Response updatePro(
			@QueryParam("pNumber") String pNumber,
			@QueryParam("category") String category,
			@QueryParam("brand") String   brand,
			@QueryParam("pName") String pName,
			@QueryParam("barcode") int barcode,
			@QueryParam("bPrice") double bPrice,
			@QueryParam("sPrice") double sPrice
			) {
		Production p=new Production();
		p.setpNumber(pNumber);
		p.setCategory(category);
		p.setBrand(brand);
		p.setpName(pName);
		p.setBarcode(barcode);
		p.setbPrice(bPrice);
		p.setsPrice(sPrice);
		session.update("production.update",p);
		session.commit();
//		System.out.println("添加成功");
		return Response.status(201).entity(queryByPNumber(pNumber)).build();
	}






	//查询所有商品
//	http://localhost:8888/Invoice/optPro/queryAll
	@GET
	@Path("queryAll")
	@Produces("application/json; charset=utf-8")
	public List queryAllPro(){
		List list=session.selectList("production.query");
		System.out.println(list.size());
		//System.out.println(list.get(0).getbPrice());
		return list;

	}
	@POST
    @Path("/post")
    @Consumes("application/json; charset=utf-8")
    public Response createProductInJSON(List list) {

        return Response.status(201).entity(list).build();

    }





	//按货号查询商品
//	http://localhost:8888/Invoice/optPro/queryByPNumber/12
	@GET
	@Path("/queryByPNumber/{param}")
	@Produces("application/json; charset=utf-8")
	public Production queryByPNumber(@PathParam("param") String  pNumber){
		Production p=session.selectOne("production.selectProBypNumber",pNumber);
		System.out.println(p.toString());
		return p;
	}
	@POST
    @Path("/post")
    @Consumes("application/json; charset=utf-8")
    public Response createProductInJSON(Production p) {

        return Response.status(201).entity(p).build();

    }


//	selectProBypName
	//按品名查询商品
//	http://localhost:8888/Invoice/optPro/queryBypName/手机
	@GET
	@Path("/queryBypName/{param}")
	@Produces("application/json; charset=utf-8")
	public Response queryBypName(@PathParam("param") String pname){
		System.out.println(pname);
		List list=session.selectList("production.selectProBypName",pname);

		return Response.status(201).entity(list).build();
	}

	//按品牌查询商品
//	http://localhost:8888/Invoice/optPro/queryBybrand/华为
	@GET
	@Path("/queryBybrand/{param}")
	@Produces("application/json; charset=utf-8")
	public Response queryBybrand(@PathParam("param") String brand){
		List list=session.selectList("production.selectProBybrand",brand);

		return Response.status(201).entity(list).build();
	}

	//按大类查询商品
//	http://localhost:8888/Invoice/optPro/queryBycategory/小家电
	@GET
	@Path("/queryBycategory/{param}")
	@Produces("application/json; charset=utf-8")
	public Response queryBycategory(@PathParam("param") String category){
		List list=session.selectList("production.selectProBycategory",category);

		return Response.status(201).entity(list).build();
	}


}









你可能感兴趣的:(resteasy)