题目: 商品管理系统
Java
JDK8, IDEA, Tomcat7, MySQL
项目开发使用SSM(Mybatis + Spring + Springmvc)实现商品管理相关功能,MySQL作为后台数据库。功能包括查询商品列表,添加商品信息等,具体要求如下:
图-1
图-2
图-3
图-4
数据库表名称及要求:
表名 |
tb_goods_type |
中文表名称 |
商品类型表 |
|||||
主键 |
typeid |
|||||||
序号 |
字段名称 |
字段说明 |
类型 |
长度 |
属性 |
备注 |
||
1 |
typeid |
类型编号 |
Integer |
|
主键 |
自增 |
||
2 |
typename |
类型名称 |
VARCHAR(20) |
20 |
非空 |
|
表名 |
tb_goods |
中文表名称 |
商品表 |
|||||
主键 |
goodsid |
|||||||
序号 |
字段名称 |
字段说明 |
类型 |
长度 |
属性 |
备注 |
||
1 |
goodsid |
商品编号 |
Integer |
|
主键 |
自增 |
||
2 |
name |
商品名称 |
varchar(50) |
50 |
非空 |
|
||
3 |
brand |
品牌 |
varchar(50) |
50 |
非空 |
|
||
4 |
model |
型号 |
varchar(50) |
50 |
非空 |
|
||
5 |
price |
单价 |
double(10,2) |
10,2 |
非空 |
|
||
6 |
picture |
图片 |
varchar(255) |
255 |
非空 |
|
||
7 |
description |
商品描述 |
text |
|
|
|
||
8 |
typeid |
类型编号 |
Integer |
|
|
外键, 参照商品类型表 |
项目总览(dubbo)
pom.xml(WorkParent)
4.0.0
com.bisxt
WorkParent
1.0-SNAPSHOT
../Provider_goods
../Service_goods_web
junit
junit
pom
WorkParent
http://www.example.com
UTF-8
1.7
1.7
4.12
4.3.18.RELEASE
3.2.8
1.2.2
1.2.15
5.1.32
1.6.4
2.8.2
1.0.9
1.2
2.5
2.0
2.5
3.3.2
1.3.2
3.3
4.0.3
0.9.1
1.3.1
2.9.0
4.10.3
2.5.3
0.10
4.4.1
0.6
3.4.6
1.2.17
1.8.2
1.0
1.7.11.RELEASE
2.2
org.springframework.data
spring-data-redis
${spring.data.redis.version}
org.springframework
spring-context
org.springframework
spring-context-support
aopalliance
aopalliance
${aopalliance.version}
org.aspectj
aspectjweaver
${aspectj.version}
org.apache.zookeeper
zookeeper
${zookeeper.version}
joda-time
joda-time
${joda-time.version}
org.apache.commons
commons-lang3
${commons-lang3.version}
org.apache.commons
commons-io
${commons-io.version}
commons-net
commons-net
${commons-net.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
com.alibaba
dubbo
org.springframework
spring
${dubbo.version}
junit
junit
${junit.version}
test
org.slf4j
slf4j-log4j12
${slf4j.version}
log4j
log4j
${log4j.version}
org.mybatis
mybatis
${mybatis.version}
commons-fileupload
commons-fileupload
${commons-fileupload.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
com.github.miemiedev
mybatis-paginator
${mybatis.paginator.version}
com.github.pagehelper
pagehelper
${pagehelper.version}
mysql
mysql-connector-java
${mysql.version}
com.alibaba
druid
${druid.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
jstl
jstl
${jstl.version}
javax.servlet
servlet-api
${servlet-api.version}
provided
javax.servlet
jsp-api
${jsp-api.version}
provided
redis.clients
jedis
${jedis.version}
org.apache.solr
solr-solrj
${solrj.version}
com.101tec
zkclient
${zkclient.version}
org.noggit
noggit
${noggit.version}
org.apache.httpcomponents
httpclient
${httpcomponents.version}
org.apache.httpcomponents
httpmime
${httpcomponents.version}
org.apache.httpcomponents
httpcore
${httpcomponents.version}
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-jar-plugin
3.0.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
maven-site-plugin
3.7.1
maven-project-info-reports-plugin
3.0.0
org.apache.tomcat.maven
tomcat7-maven-plugin
${tomcat.version}
src/main/java
**/*.xml
src/main/resources
**/*.xml
**/*.properties
mapper和pojo都是逆向生成的(mybatis逆向生成配置文件)
com.bisxt.goods.service.GoodsService
package com.bisxt.goods.service;
import com.bisxt.goods.pojo.TbGoods;
import java.util.List;
public interface GoodsService {
/**
* 查询所有商品
* @return
*/
public List selGoods();
/**
* 添加s商品
* @param tbGoods
* @return
*/
public int saveGoods(TbGoods tbGoods);
/**
* 根据cid查询商品
* @param cid
* @return
*/
public TbGoods selByCidService(Integer cid);
public List selAllLikeGoods(String name);
}
com.bisxt.goods.service.impl.GoodsServiceImpl
package com.bisxt.goods.service.impl;
import com.bisxt.goods.mapper.TbGoodsMapper;
import com.bisxt.goods.pojo.TbGoods;
import com.bisxt.goods.pojo.TbGoodsExample;
import com.bisxt.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class GoodsServiceImpl implements GoodsService {
@Autowired
private TbGoodsMapper tbGoodsMapper;
@Override
public List selGoods() {
TbGoodsExample example=new TbGoodsExample();
List tbGoods = this.tbGoodsMapper.selectByExample(example);
return tbGoods;
}
@Override
public int saveGoods(TbGoods tbGoods) {
return this.tbGoodsMapper.insert(tbGoods);
}
@Override
public TbGoods selByCidService(Integer cid) {
return this.tbGoodsMapper.selectByPrimaryKey(cid);
}
@Override
public List selAllLikeGoods(String name) {
TbGoodsExample example=new TbGoodsExample();
TbGoodsExample.Criteria criteria = example.createCriteria();
if(name!=null &&!"".equals(name)){
criteria.andNameLike("%"+name+"%");
}
return this.tbGoodsMapper.selectByExample(example);
}
}
俩个工具类
com.bisxt.beans.FtpUtil
package com.bisxt.beans;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
* ftp上传下载工具类
*/
public class FtpUtil {
/**
* Description: 向FTP服务器上传文件
* @param host FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param basePath FTP服务器基础目录
* @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String host, int port, String username, String password, String basePath,
String filePath, String filename, InputStream input) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
//切换到上传目录
if (!ftp.changeWorkingDirectory(basePath+filePath)) {
//如果目录不存在创建目录
String[] dirs = filePath.split("/");
String tempPath = basePath;
for (String dir : dirs) {
if (null == dir || "".equals(dir)) continue;
tempPath += "/" + dir;
if (!ftp.changeWorkingDirectory(tempPath)) {
if (!ftp.makeDirectory(tempPath)) {
return result;
} else {
ftp.changeWorkingDirectory(tempPath);
}
}
}
}
//设置上传文件的类型为二进制类型
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//上传文件
if (!ftp.storeFile(filename, input)) {
return result;
}
input.close();
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
/**
* Description: 从FTP服务器下载文件
* @param host FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param remotePath FTP服务器上的相对路径
* @param fileName 要下载的文件名
* @param localPath 下载后保存到本地的路径
* @return
*/
public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
String fileName, String localPath) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
public static void main(String[] args) {
try {
FileInputStream in=new FileInputStream(new File("D:\\1.jpg"));
boolean flag = uploadFile("192.168.10.128", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2015/01/21", "gaigeming.jpg", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
com.bisxt.beans.IDUtils
package com.bisxt.beans;
import java.util.Random;
import java.util.UUID;
/**
* 各种id生成策略
* @version 1.0
*/
public class IDUtils {
/**
* 图片名生成
*/
public static String genImageName() {
//取当前时间的长整形值包含毫秒
long millis = System.currentTimeMillis();
//long millis = System.nanoTime();
//加上三位随机数
Random random = new Random();
int end3 = random.nextInt(1000);
//如果不足三位前面补0
String str = millis + String.format("%03d", end3);
return str;
}
/**
* 商品id生成
*/
public static long genItemId() {
//取当前时间的长整形值包含毫秒
long millis = System.currentTimeMillis();
//long millis = System.nanoTime();
//加上两位随机数
Random random = new Random();
int end2 = random.nextInt(99);
//如果不足两位前面补0
String str = millis + String.format("%02d", end2);
long id = new Long(str);
return id;
}
public static void main(String[] args) {
for(int i=0;i< 100;i++)
System.out.println(genItemId());
}
}
com.bisxt.goods.service.ServiceGoodsService
package com.bisxt.goods.service;
import com.bisxt.goods.pojo.TbGoods;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
public interface ServiceGoodsService {
/**
* 查询所有商品
* @return
*/
public List selAllGooods();
/**
* 新增商品
* @param tbGoods
* @return
*/
public int saveGoodsService(TbGoods tbGoods, MultipartFile file);
/**
* 根据cid 查询商品
* @param cid
* @return
*/
public TbGoods selByCidService(Integer cid);
/**
* 模糊查询
* @param name
* @return
*/
public List selAllgoodsService(String name);
}
com.bisxt.goods.service.impl.ServiceGoodsServiceImpl
package com.bisxt.goods.service.impl;
import com.bisxt.beans.FtpUtil;
import com.bisxt.beans.IDUtils;
import com.bisxt.goods.pojo.TbGoods;
import com.bisxt.goods.service.GoodsService;
import com.bisxt.goods.service.ServiceGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class ServiceGoodsServiceImpl implements ServiceGoodsService {
@Value("${FTP_HOST}")
private String FTP_HOST;
@Value("${FTP_PORT}")
private Integer FTP_PORT;
@Value("${FTP_USERNAME}")
private String FTP_USERNAME;
@Value("${FTP_PASSWORD}")
private String FTP_PASSWORD;
@Value("${FTP_PATH}")
private String FTP_PATH;
@Value("${IMAGE_HTTP_PATH}")
private String IMAGE_HTTP_PATH;
@Autowired
private GoodsService goodsServiceProxy;
@Override
public List selAllGooods() {
List tbGoods = this.goodsServiceProxy.selGoods();
return tbGoods;
}
@Override
public int saveGoodsService(TbGoods tbGoods, MultipartFile file) {
String fileName=null;
boolean flag=false;
int n=0;
try {
String originalFilename = file.getOriginalFilename();
System.out.println("hhhhhh"+originalFilename);
String etc=originalFilename.substring(originalFilename.lastIndexOf("."));
fileName = IDUtils.genImageName()+etc;
InputStream local = file.getInputStream();
Date date=new Date();
SimpleDateFormat sf=new SimpleDateFormat("/yyyy.MM.dd/");
String path=sf.format(date);
flag=FtpUtil.uploadFile(FTP_HOST,FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_PATH,path ,fileName,local);
tbGoods.setPicture(IMAGE_HTTP_PATH+path+fileName);
n = this.goodsServiceProxy.saveGoods(tbGoods);
}catch (Exception e){
e.printStackTrace();
}
return n;
}
@Override
public TbGoods selByCidService(Integer cid) {
return this.goodsServiceProxy.selByCidService(cid);
}
@Override
public List selAllgoodsService(String name) {
return this.goodsServiceProxy.selAllLikeGoods(name);
}
}
com.bisxt.goods.controller.GoodsController
package com.bisxt.goods.controller;
import com.bisxt.goods.pojo.TbGoodType;
import com.bisxt.goods.pojo.TbGoods;
import com.bisxt.goods.service.AllGoodstypeService;
import com.bisxt.goods.service.ServiceGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@Controller
public class GoodsController {
@Autowired
private ServiceGoodsService serviceGoodsService;
@Autowired
private AllGoodstypeService allGoodstypeService;
@RequestMapping("/goods/select")
public String selGoodsController(Model model){
List tbGoods = this.serviceGoodsService.selAllGooods();
model.addAttribute("list", tbGoods);
List tbGoodTypes = this.allGoodstypeService.selAlltypes();
model.addAttribute("tbGoodTypes",tbGoodTypes);
return "welcome";
}
@RequestMapping("/goods/insert")
public String selGoodsController(TbGoods tbGoods, MultipartFile file){
int i = this.serviceGoodsService.saveGoodsService(tbGoods, file);
if(i>0){
return "forword:/goods/select" ;
}
return "addUser";
}
@RequestMapping("/goods/ajax")
@ResponseBody
public TbGoods selByCidGoodsController(Integer cid){
return this.serviceGoodsService.selByCidService(cid);
}
@RequestMapping("/goods/like")
public String selByLike(String name,Model model){
List tbGoods = this.serviceGoodsService.selAllgoodsService(name);
model.addAttribute("list", tbGoods);
return "welcome";
}
}