SpringBoot实现多数据源的切换实践
352
2022-08-05
Java实战之小米交易商城系统的实现
目录一、项目简述二、效果图三、核心代码用户购物车控制层后台登录控制层后台商品管理控制层后台品类控制层
一、项目简述
环境配置:
Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)
项目技术:
jsP +Spring + SpringMVC + MyBatis + html+ css + javascript + jquery + Ajax + layui+ maven等等。
二、效果图
三、核心代码
用户购物车控制层
@CrossOrigin
@RestController
@RequestMapping("carts")
public class CartController {
@Autowired
private RegLogServiceImp regLogServiceImp;
@Autowired
private GoodServiceImp goodServiceImp;
@Autowired
private CartServiceImp cartServiceImp;
@PostMapping("/push")
public CartResult putShop(@RequestBody pushCart pushcart) throws IOException {
CartResult cartResult = new CartResult();
boolean success = TokenUtil.verify(pushcart.getToken());
if (success) {
cartResult.setStatus(0);
User user = regLogServiceImp.getUserByUserName(pushcart.getUsername().trim());
String userid = user.getUserid();
CartGood cartGood = goodServiceImp.getGoodByid(userid, pushcart.getProductId());
if (cartGood == null) {
int goodcount = 1;
CartGoodInsert goodByidinsert = goodServiceImp.getGoodByidinsert(pushcart.getProductId());
goodServiceImp.insCartGood(userid, pushcart.getProductId(), goodByidinsert.getName(),
goodByidinsert.getPrice(), goodcount, goodByidinsert.getSubtitle(), goodByidinsert.getProductStock(),
goodByidinsert.getStatus(), true);
} else {
goodServiceImp.updateCartGood(userid, cartGood.getCount() + 1, pushcart.getProductId(), (cartGood.getCount() + 1) * cartGood.getPrice());
}
cartProductVoList cartProductVoList = cartServiceImp.getCartProduct(userid);
int cartTotalPrice = 0;
boolean selectedAll = true;
int cartTotalQuantity = 0;
for (cartProduct cartProduct : cartProductVoList.getList()) {
cartTotalPrice += cartProductzdZra.getProductTotalPrice();
if (!cartProduct.isProductSelected()) {
selectedAll = false;
}
cartTotalQuantity += cartProduct.getCount();
}
cartResult.setStatus(0);
cartResult.setCartTotalPrice(cartTotalPrice);
cartResult.setCartTotalQuantity(cartTotalQuantity);
cartResult.setData(cartProductVoList);
cartResult.setImageHost("mi.com");
cartResult.setSelectedAll(selectedAll);
} else {
cartResult.setStatus(10);
}
return cartResult;
}
@GetMapping("/sum")
public CartResult getCartSum(@RequestParam("username") String username) throws IOException {
CartResult cartResult = new CartResult();
User user = regLogServiceImp.getUserByUserName(username);
String userid = user.getUserid();
cartProductVoList cartProductVoList = cartServiceImp.getCartProduct(userid);
if(cartProductVoList==null){
cartResult.setStatus(0);
cartResult.setCartTotalQuantity(0);
}
else {
int cartTotalQuantity=0;
for(cartProduct cartProduct:cartProductVoList.getList()){
cartTotalQuantity+=cartProduct.getCount();
}
cartResult.setStatus(0);
cartResult.setCartTotalQuantity(cartTotalQuantity);
}
return cartResult;
}
@RequestMapping("/del")
public CartResult delCarts(@RequestParam("id") int goodid,
@RequestParam("username") String username) throws IOException {
User user = regLogServiceImp.getUserByUserName(username);
String userid = user.getUserid();
goodServiceImp.delGoodByid(userid,goodid);
CartResult cartResult = getCarts(username);
return cartResult;
}
@RequestMapping("/getcarts")
public CartResult getCarts (@RequestParam("username") String username) throws IOException {
CartResult cartResult = new CartResult();
User user = regLogServiceImp.getUserByUserName(username);
String userid = user.getUserid();
cartProductVoList cartProductVoList = cartServiceImp.getCartProduct(userid);
if(cartProductVoList==null){
cartResult.setStatus(0);
cartResult.setCartTotalQuantity(0);
cartResult.setSelectedAll(false);
cartResult.setImageHost("mi.com");
cartResult.setCartTotalQuantity(0);
}
else {
int carttotalPrice=0;
boolean selectedall=true;
int carttotalQuantity=0;
for(cartProduct cartProduct:cartProductVoList.getList()){
if(cartProduct.isProductSelected())
carttotalPrice+=cartProduct.getProductTotalPrice();
if(!cartProduct.isProductSelected()){
selectedall=false;
}
carttotalQuantity+=cartProduct.getCount();
}
cartResult.setStatus(0);
cartResult.setCartTotalPrice(carttotalPrice);
cartResult.setCartTotalQuantity(carttotalQuantity);
cartResult.setData(cartProductVoList);
cartResult.setImageHost("mi.com");
cartResult.setSelectedAll(selectedall);
}
return cartResult;
}
@GetMapping("/updatecart")
public CartResult getCarts (
@RequestParam("goodid") int goodid,
@RequestParam("username") String username,
@RequestParam("quantity") int count,
@RequestParam("selected") boolean selected
) throws IOException {
User user = regLogServiceImp.getUserByUserName(username);
String userid = user.getUserid();
CartGood goodByid = goodServiceImp.getGoodByid(userid, goodid);
goodServiceImp.updaCartNumSel(userid,goodid,count,selected,goodByid.getPrice()*count);
CartResult carts = getCarts(username);
return carts;
}
@GetMapping("/selectAll")
public CartResult SelectAllCart(@RequestParam("username") String username) throws IOException {
User user = regLogServiceImp.getUserByUserName(username);
String userid = user.getUserid();
goodServiceImp.updaCartSelect(userid);
CartResult cartResult = getCarts(username);
return cartResult;
}
@GetMapping("/unSelectAll")
public CartResult unSelectAllCart(@RequestParam("username") String username) throws IOException {
User user = regLogServiceImp.getUserByUserName(username);
String userid = user.getUserid();
goodServiceImp.updaCartunSelect(userid);
CartResult cartResult = getCarts(username);
cartResult.setCartTotalQuantity(0);
cartResult.setCartTotalPrice(0);
return cartResult;
}
}
后台登录控制层
@CrossOrigin
@RestController
@RequestMapping("Login")
public class LoginController {
@Autowired
private LoginServiceImp loginServiceImp;
Map
// String publicKey = keyMap.get("publicKey");
// String privateKey = keyMap.get("privateKey");
// private String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIS-9P6aiWK-mNIJxz0R_XXIbHM21pS_ZncpQcE8T5Mu6P6sJfoi5Cw6S4jcSqxNbRs5FSUc59GAkU1Ek9onrz8CAwEAAQ";
private String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAhL70_pqJYr6Y0gnHPRH9dchsczbWlL9mdylBwTxPky7o_qwl-iLkLDpLiNxKrE1tGzkVJRzn0YCRTUST2ievPwIDAQABAkAviOHaOiRwQ6gdrnq2F7cXcknDiCOW6GMc0J1HQP2Nyn52HKtc9E9Ailm-eQesTw6P-qTsvHMyfuVYz8fLhfkhAiEAvhvoG2Dz6qSN6IHeuPboJP7YHK9zTt6M_NGCJKAenasCIQCywU7eStITcHFTRDjjShH0CfO-vR6q-zgWQPjtNI_YvQIgY8UNiODW4bu-ZdlSY8vghRnhnRxO7YNKhpwFKueFacUCIQCZ-P65ZVNs4ScMZTBnP48VWVk2S-rVto0KRi7lC6h4NQIgEa_GhBO6U2p6G5cGAR25ZiQVH9dFnrMVyPxY7P7-WsA";
@PostMapping("/login")
public LoginResult Login(@RequestBody User adminUser) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
User user = loginServiceImp.getUserByUserName(adminUser.getUsername());
LoginResult result = new LoginResult();
LoginData loginData = new LoginData();
if (user == null) {
result.setMsg("无此用户,请重新输入正确用户名");
result.setStatus(2008);
} else {
if (RSAUtils.privateDecrypt(user.getPwd(), RSAUtils.getPrivateKey(privateKey)).equals(adminUser.getPwd())){
result.setStatus(0);
String token = TokenUtil.token(user.getId().trim());
loginData.setToken(token);
result.setMsg("登录成功");
result.setStatus(1010);
result.setData(loginData);
}else{
result.setStatus(2007);
result.setMsg("密码错误");
}
}
return result;
}
}
后台商品管理控制层
@CrossOrigin
@RestController
@RequestMapping("Good")
public class GoodController {
@Autowired
private GoodServiceImp goodServiceImp;
@GetMapping("/getGood")
private GoodResult getGood(@RequestParam("pageIndex") int pageIndex,
@RequestParam("pageSize") int pageSize,
@RequestParam("name") String goodname
) throws IOException {
GoodResult goodResult = goodServiceImp.getGood(pageIndex,pageSize,goodname);
return goodResult;
}
@GetMapping("/getGoodById")
private GoodResult getGoodById(@RequestParam("category") int pageIndex,
@RequestParam("pageSize") int pageSize
) throws IOException {
GoodResult goodResult = goodServiceImp.getGoodById(pageIndex,pageSize);
return goodResult;
}
@RequestMapping("/getGoodByName")
private GoodResult getGoodByName(@RequestParam("name") String goodname) throws IOException {
GoodResult goodResult = goodServiceImp.getGoodByName(goodname);
return goodResult;
}
@RequestMapping("/updGood")
private GoodResult getGoodByName(@RequestParam("good_id") int goodid,
@RequestParam("name") String goodname,
@RequestParam("newhot") int newhot) throws IOException {
GoodResult goodResult = goodServiceImp.updGood(goodid,goodname,newhot);
return goodResult;
}
@GetMapping("/getGoodPicById")
private GoodPicResult getGoodPicById(@RequestParam("category") int pageIndex,
@RequestParam("pageSize") int pageSize
) throws IOException {
GoodPicResult goodpicResult = goodServiceImp.getGoodPic(pageIndex,pageSize);
return goodpicResult;
}
@RequestMapping("/getGoodPicByName")
private GoodPicResult getGoodPicByName(@RequestParam("name") String goodname) throws IOException {
// int categor = Integer.parseInt(category);
GoodPicResult goodResult = goodServiceImp.getGoodPicByName(goodname);
return goodResult;
}
@PostMapping("/pushGood")
private GoodPushResult pushGood(@RequestBody good good) throws IOException {
GoodPushResult goodPushResult = goodServiceImp.getGoodPush(good);
return goodPushResult;
}
@PostMapping("/delGood")
private void delGood(@RequestBody good good) throws IOException {
goodServiceImp.delGood(good);
}
@PostMapping("/delMulGood")
private void delMulGood(@RequestBody List
goodServiceImp.delMulGood(goods);
}
@GetMapping("/getGoodPic")
private GoodPicResult getGoodPic(@RequestParam("pageIndex") int pageIndex,
@RequestParam("pageSize") int pageSize) throws IOException {
GoodPicResult goodPicResult = goodServiceImp.getGoodPic(pageIndex,pageSize);
return goodPicResult;
}
@PostMapping("/updGoodPic")
private void upGoodPic(@RequestBody GoodPic goodPic) throws IOException {
int goodid= goodPic.getGood_id();
String icon= goodPic.getIcon();
String imageHead = goodPic.getImageHead();
String imageFoot = goodPic.getImageFoot();
goodServiceImp.updGoodPic(goodid,icon,1);
goodServiceImp.updGoodPic(goodid,imageHead,2);
goodServiceImp.updGoodPic(goodid,imageFoot,3);
}
}
后台品类控制层
@CrossOrigin
@RestController
@RequestMapping("category")
public class CategoryController {
@Autowired
private CategServiceImp categServiceImp;
@GetMapping("/getCategory")
private CateResult getCategory(@RequestParam("pageIndex") int pageIndex,
@RequestParam("pageSize") int pageSize) throws IOException {
CateResult cateResult = categServiceImp.getCategory(pageIndex, pageSize);
return cateResult;
}
@PostMapping("/updateCategory")
private void updateCategory(@RequestBody Category category) throws IOException {
categServiceImp.updCategory(category.getName(),category.getCategoryId());
}
}
以上就是java实战之小米交易商城系统的实现的详细内容,更多关于Java交易商城系统的资料请关注我们其它相关文章!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~