1.新建maven项目
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baosight.webapp</groupId>
<artifactId>Invoice</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Invoice Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.14.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.14.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>3.0.14.Final</version>
</dependency>
<dependency>
<groupId>com.github.growth2</groupId>
<artifactId>lazybones-mybatis-generator-plugin</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>Invoice</finalName>
</build>
</project>
2.引入其他jar包
前两个jar包设置cors跨域
后两个jar包设置数据库连接
3.web.xml的配置
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.baosight.webapp.app.InvoiceApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!--cors跨域 -->
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<servlet-name>/*</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
前面配置servlet映射
<url-pattern>/services/*</url-pattern>
//表示请求路径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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="com/baosight/webapp/jdbc.properties">
</properties>
<typeAliases>
<typeAlias alias="production" type="com.baosight.webapp.bean.Production"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/baosight/webapp/sqlmap/production.xml"/>
</mappers>
</configuration>
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{
<span style="white-space:pre"> </span>//读取配值文件
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<Object> singletons = new HashSet<Object>();
public InvoiceApplication() {
singletons.add(new OptProductionService());
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
application同样需要在web.xml中配置
每写一个sercvices需要在application中加入,生成对象
7.javabean文件:production.java
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="production">
<select id="query" resultType="production">
SELECT
PNUMBER as "pnumber",
PICTURE as "picture",
CATEGORY as "category",
BRAND as "brand",
PNAME as "pname",
BARCODE as "barcode",
BPRICE as "bprice",
SPRICE as "sprice"
FROM B_PRODUCTION WHERE 1=1 ORDER BY pnumber
</select>
<select id="selectProBypNumber"
parameterType="java.lang.String"
resultType="production"
>
select * from b_production where PNUMBER = #{pNumber}
</select>
<select id="selectProBypName"
parameterType="java.lang.String"
resultType="production"
>
select * from b_production where pname = #{pname}
</select>
<select id="selectProBycategory"
parameterType="java.lang.String"
resultType="production"
>
select * from b_production where category = #{category}
</select>
<update id="addPicture">
UPDATE B_PRODUCTION
SET
PICTURE = #{picture}
WHERE
PNUMBER = #{pNumber}
</update>
<insert id="addPro" parameterType="production">
insert into
b_production(pNumber,category ,brand, pName, barcode, bPrice, sPrice)
values(#{pNumber},#{category},#{brand},#{pName},#{barcode},#{bPrice},#{sPrice})
</insert>
<delete id="delete" parameterType="java.lang.String">
DELETE FROM B_PRODUCTION WHERE
PNUMBER = #{pNumber}
</delete>
<update id="update" parameterType="production">
UPDATE B_PRODUCTION
SET
CATEGORY = #{category},
BRAND = #{brand},
PNAME = #{pName},
BARCODE = #{barcode},
BPRICE = #{bPrice},
SPRICE = #{sPrice}
WHERE
PNUMBER = #{pNumber}
</update>
</mapper>
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<Production> 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();
}
}