Java實(shí)戰(zhàn)之鮮花商城系統(tǒng)的實(shí)現(xiàn)
項(xiàng)目介紹
該項(xiàng)目為前后臺(tái)項(xiàng)目,分為普通用戶與管理員兩種角色,前臺(tái)普通用戶登錄,后臺(tái)管理員登錄;
管理員角色包含以下功能:
管理員登錄,用戶管理,鮮花類別管理,鮮花管理,訂單管理并發(fā)貨,留言管理,系統(tǒng)公告管理等功能。
用戶角色包含以下功能:
用戶首頁(yè),用戶注冊(cè)登錄,查看鮮花詳情,加入購(gòu)物車,確認(rèn)訂單,查看我的訂單,商城留言板,商城公告等功能。
環(huán)境需要
1.運(yùn)行環(huán)境:最好是java jdk 1.8,我們?cè)谶@個(gè)平臺(tái)上運(yùn)行的。其他版本理論上也可以。
2.IDE環(huán)境:IDEA,Eclipse,Myeclipse都可以。推薦IDEA;
3.tomcat環(huán)境:Tomcat 7.x,8.x,9.x版本均可
4.硬件環(huán)境:windows 7/8/10 1G內(nèi)存以上;或者 Mac OS;
5.數(shù)據(jù)庫(kù):MySql 5.7版本;
6.是否Maven項(xiàng)目:否;
技術(shù)棧
1. 后端:Spring+SpringMVC+Mybatis
2. 前端:JSP+jQuery+Ajax
使用說明
1. 使用Navicat或者其它工具,在mysql中創(chuàng)建對(duì)應(yīng)名稱的數(shù)據(jù)庫(kù),并導(dǎo)入項(xiàng)目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse導(dǎo)入項(xiàng)目,Eclipse/MyEclipse導(dǎo)入時(shí),若為maven項(xiàng)目請(qǐng)選擇maven;若為maven項(xiàng)目,導(dǎo)入成功后請(qǐng)執(zhí)行maven clean;maven install命令,然后運(yùn)行;
3. 將項(xiàng)目中jdbc.properties配置文件中的數(shù)據(jù)庫(kù)配置改為自己的配置;
4. 運(yùn)行項(xiàng)目,輸入localhost:8080/ 登錄
用戶賬號(hào)/密碼: user/123456
管理員賬號(hào)/密碼:admin/admin
效果圖展示






核心代碼
商品管理控制層
/**
* 商品
**/
@RestController
@RequestMapping("flower")
public class FlowerController {
@Autowired
FlowersService flowerService;
@Autowired
FlowersDao flowersDao;
@RequestMapping("/test")
R test() {
R r = new R();
return r;
}
//find
@RequestMapping("/find")
R find(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey, @RequestParam("searchType") String searchType) {
R r = new R();
List<Flower> flowers = flowerService.find(searchKey, searchType);
if (flowers == null) {
return r.setCode(2000);
}
return getResponse(flowers, page, Constant.SHOW_PAGE_SIZE, r);
}
// 管理員查詢所有
@RequestMapping("/findAll")
R findAll(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey) {
R r = new R();
List<Flower> flowers = flowerService.findAll(searchKey);
if (flowers == null) {
return r.setCode(2000);
}
return getResponse(flowers, page, Constant.PAGE_SIZE, r);
}
// 返回結(jié)果
private R getResponse(List<Flower> flowers, int page, int pageSize, R r) {
Map<String, Object> map = new HashMap<>();
List<Flower> items = flowers.size() >= page * pageSize ?
flowers.subList((page - 1) * pageSize, page * pageSize)
: flowers.subList((page - 1) * pageSize, flowers.size());
int len = flowers.size() % pageSize == 0 ? flowers.size() / pageSize
: (flowers.size() / pageSize + 1);
for (Flower item : items) {
if (item.getImg_guid() == null) {
item.setImg_guid(Constant.DEFAULT_IMG);
}
item.setImg_guid(Constant.IMG_USE_PATH + item.getImg_guid());
}
map.put("items", items);
map.put("len", len);
return r.setCode(2000).setData(map);
}
@RequestMapping("/create")
R create(@RequestBody Flower flower) {
R r = new R();
int ans = flowerService.add(flower);
if (ans == 1) {
return r.setCode(2000).setMsg(HttpMsg.ADD_FLOWER_OK);
}
return r.setCode(4000).setMsg(HttpMsg.ADD_FLOWER_FAILED);
}
@RequestMapping("/update")
R update(@RequestBody Flower flower) {
R r = new R();
int ans = flowerService.update(flower);
if (ans >= 0) {
return r.setCode(2000).setMsg(HttpMsg.UPDATE_FLOWER_OK);
}
return r.setCode(4000).setMsg(HttpMsg.UPDATE_FLOWER_FAILED);
}
@RequestMapping("/changeState")
R changeState(@RequestBody Flower flower) {
R r = new R();
flowersDao.changeState(flower);
String msg = flower.getState() == 1?HttpMsg.GOODS_UP_OK:HttpMsg.GOODS_DOWN_OK;
return r.setCode(2000).setMsg(msg);
}
// 保存上傳的圖片
@RequestMapping("/updateImg")
R updateImg(@RequestParam("file") MultipartFile file) {
R r = new R();
// 只接收 jpg/png 圖片
String filename = file.getOriginalFilename();
if (!filename.endsWith(".jpg") && !filename.endsWith(".png")){
return r.setCode(4000).setMsg(HttpMsg.ERROR_FILE_TYPE);
}
String img_guid = UUID.randomUUID().toString().replaceAll("-", "") + ".jpg";
try {
savePic(file.getInputStream(), img_guid);
return r.setCode(2000).setMsg(HttpMsg.UPDATE_PIC_OK).setData(img_guid);
} catch (IOException e) {
return r.setCode(4000).setMsg(HttpMsg.UPDATE_PIC_FAILED);
}
}
@PutMapping("/updateImgGuid")
R updateImgGuid(@RequestParam("guid") String guid, @RequestParam("id") int id) {
R r = new R();
int ans = flowerService.updateImg(guid, id);
if (ans == 1) {
return r.setCode(2000).setMsg(HttpMsg.UPDATE_PIC_OK);
}
return r.setCode(4000).setMsg(HttpMsg.UPDATE_PIC_FAILED);
}
@DeleteMapping("/delete")
R delete(@RequestParam("id") int id) {
R r = new R();
int ans = flowerService.delete(id);
if (ans == 1) {
return r.setCode(2000).setMsg(HttpMsg.DELETE_FLOWER_OK);
}
return r.setCode(4000).setMsg(HttpMsg.DELETE_FLOWER_FAILED);
}
private void savePic(InputStream inputStream, String fileName) {
OutputStream os = null;
try {
String path = Constant.IMG_PATH;
// 1K的數(shù)據(jù)緩沖
byte[] bs = new byte[1024];
// 讀取到的數(shù)據(jù)長(zhǎng)度
int len;
// 輸出的文件流保存到本地文件
os = new FileOutputStream(new File(path + fileName));
// 開始讀取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 完畢,關(guān)閉所有鏈接
try {
os.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}訂單管理控制層
/**
* 用戶
**/
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
OrderService orderService;
@Autowired
UserDao userDao;
@Autowired
OrderDao orderDao;
@Autowired
FlowersDao flowersDao;
@RequestMapping("/test")
R test() {
R r = new R();
return r.setCode(4000).setMsg(Constant.IMG_PATH).setData(orderDao.findAll(null));
}
@RequestMapping("/queryByAccount")
R queryByAccount(@RequestParam("account") String account) {
R r = new R();
if (StringUtil.isEmpty(account)) {
return r.setCode(4000).setMsg(HttpMsg.INVALID_PARAM);
}
List<Order> orders = orderService.queryByAccount(account);
return r.setCode(2000).setData(orders);
}
@RequestMapping("/find")
R find(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey, @RequestParam("account") String account) {
R r = new R();
Map<String, Object> map = new HashMap<>();
List<Order> orders = orderService.find(searchKey, account);
if (orders == null) {
return r.setCode(2000);
}
map.put("items", orders);
map.put("len", orders.size());
return r.setCode(2000).setData(map);
}
@RequestMapping("/findAll")
R findAll(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey) {
R r = new R();
Map<String, Object> map = new HashMap<>();
List<Order> orders = orderService.findAll(searchKey);
if (orders == null) {
return r.setCode(2000);
}
List<Order> items = orders.size() >= page * Constant.PAGE_SIZE ?
orders.subList((page - 1) * Constant.PAGE_SIZE, page * Constant.PAGE_SIZE)
: orders.subList((page - 1) * Constant.PAGE_SIZE, orders.size());
int len = orders.size() % Constant.PAGE_SIZE == 0 ? orders.size() / Constant.PAGE_SIZE
: (orders.size() / Constant.PAGE_SIZE + 1);
List<OrderVo> vos = new ArrayList<>();
for (Order item : items) {
User user = userDao.queryById(item.getUid());
OrderVo vo = new OrderVo();
vo.setAddress(user.getAddress()).setPhone(user.getPhone()).setUsername(user.getName())
.setAmount(item.getAmount()).setFlower(item.getFlower()).setId(item.getId())
.setUid(item.getUid()).setOrder_guid(item.getOrder_guid()).setPrice(item.getPrice())
.setState(item.getState());
vos.add(vo);
}
map.put("items", vos);
map.put("len", len);
return r.setCode(2000).setData(map);
}
@RequestMapping("/update")
R update(@RequestBody Order order) {
R r = new R();
int ans = orderService.update(order);
if (ans >= 0) {
return r.setCode(2000).setMsg(HttpMsg.UPDATE_USER_OK);
}
return r.setCode(4000).setMsg(HttpMsg.UPDATE_USER_FAILED);
}
@RequestMapping("/changeState")
R changeState(@RequestBody Order order) {
orderDao.changeState(order);
return new R().setCode(2000).setMsg(HttpMsg.UPDATE_ORDER_OK);
}
@DeleteMapping("/delete")
R delete(@RequestParam("id") int id) {
R r = new R();
int ans = orderService.delete(id);
if (ans == 1) {
return r.setCode(2000).setMsg(HttpMsg.DELETE_USER_OK);
}
return r.setCode(4000).setMsg(HttpMsg.DELETE_USER_FAILED);
}
}
用戶管理控制層
/**
* 用戶
**/
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
UserService userService;
@Autowired
UserDao dao;
@RequestMapping("/test")
R test() {
R r = new R();
return r.setCode(4000).setMsg(Constant.IMG_PATH).setData(dao.findAll());
}
@RequestMapping("/queryInfoByAccount")
R queryInfoByAccount(@RequestParam("account") String account) {
R r = new R();
if (StringUtil.isEmpty(account)){
return r.setCode(4000).setMsg(HttpMsg.INVALID_PARAM);
}
User loginUser = userService.queryInfo(account);
if (loginUser == null){
return r.setCode(4000).setMsg(HttpMsg.INVALID_USER);
}
return r.setCode(2000).setData(loginUser);
}
@RequestMapping("/find")
R find(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey) {
R r = new R();
Map<String, Object> map = new HashMap<>();
List<User> users = userService.find(searchKey);
if (users == null) {
return r.setCode(2000);
}
List<User> items = users.size() >= page * Constant.PAGE_SIZE ?
users.subList((page - 1) * Constant.PAGE_SIZE, page * Constant.PAGE_SIZE)
: users.subList((page - 1) * Constant.PAGE_SIZE, users.size());
int len = users.size() % Constant.PAGE_SIZE == 0 ? users.size() / Constant.PAGE_SIZE
: (users.size() / Constant.PAGE_SIZE + 1);
map.put("items", items);
map.put("len", len);
return r.setCode(2000).setData(map);
}
@RequestMapping("/create")
R create(@RequestBody User user) {
R r = new R();
int ans = userService.add(user);
if (ans == 1) {
return r.setCode(2000).setMsg(HttpMsg.ADD_USER_OK);
}
return r.setCode(4000).setMsg(HttpMsg.ADD_USER_FAILED);
}
@RequestMapping("/update")
R update(@RequestBody User user) {
R r = new R();
int ans = userService.update(user);
if (ans >= 0) {
return r.setCode(2000).setMsg(HttpMsg.UPDATE_USER_OK);
}
return r.setCode(4000).setMsg(HttpMsg.UPDATE_USER_FAILED);
}
@DeleteMapping("/delete")
R delete(@RequestParam("id") int id) {
R r = new R();
int ans = userService.delete(id);
if (ans == 1) {
return r.setCode(2000).setMsg(HttpMsg.DELETE_USER_OK);
}
return r.setCode(4000).setMsg(HttpMsg.DELETE_USER_FAILED);
}
}
以上就是Java實(shí)戰(zhàn)之鮮花商城系統(tǒng)的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Java鮮花商城系統(tǒng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Java實(shí)戰(zhàn)之仿天貓商城系統(tǒng)的實(shí)現(xiàn)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之仿小米電子產(chǎn)品售賣商城系統(tǒng)的實(shí)現(xiàn)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)項(xiàng)目之在線服裝銷售商城系統(tǒng)的實(shí)現(xiàn)流程
- Java實(shí)戰(zhàn)花店商城系統(tǒng)的實(shí)現(xiàn)流程
- Java 實(shí)戰(zhàn)項(xiàng)目錘煉之在線蛋糕商城系統(tǒng)的實(shí)現(xiàn)
- Java 實(shí)戰(zhàn)項(xiàng)目之家居購(gòu)物商城系統(tǒng)詳解流程
相關(guān)文章
httpclient模擬post請(qǐng)求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猦ttpclient模擬post請(qǐng)求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
SpringCloud Gateway跨域配置代碼實(shí)例
這篇文章主要介紹了SpringCloud Gateway跨域配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
springboot與redis的簡(jiǎn)單整合實(shí)例
Redis是一個(gè)緩存、消息代理和功能豐富的鍵值存儲(chǔ)。StringBoot提供了基本的自動(dòng)配置。這篇文章主要介紹了springboot與redis的簡(jiǎn)單整合實(shí)例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2019-01-01
Java中的Semaphore信號(hào)量簡(jiǎn)析
這篇文章主要介紹了Java中的Semaphore信號(hào)量簡(jiǎn)析,Semaphore:信號(hào)量,用來限制能同時(shí)訪問共享資源的線程上限,使用Semaphore實(shí)現(xiàn)簡(jiǎn)單連接池,對(duì)比享元模式下的實(shí)現(xiàn)(用wait和notify),性能和可讀性要更好,需要的朋友可以參考下2023-12-12
SpringBoot多數(shù)據(jù)源配置方式以及報(bào)錯(cuò)問題的解決
這篇文章主要介紹了SpringBoot多數(shù)據(jù)源配置方式以及報(bào)錯(cuò)問題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java多線程編程之使用Exchanger數(shù)據(jù)交換實(shí)例
這篇文章主要介紹了Java多線程編程之使用Exchanger數(shù)據(jù)交換實(shí)例,本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-05-05

