提交 62d532f6 编写于 作者: 爱吃血肠's avatar 爱吃血肠

删除无用遗留代码以及数据库中对应的表

上级 8f1e40de
package com.yingjun.ssm.dao;
import com.yingjun.ssm.entity.Goods;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface GoodsDao {
/**
* 根据偏移量查询可用商品列表
*
* @param offset
* @param limit
* @return
*/
List<Goods> queryAll(@Param("offset") int offset, @Param("limit") int limit);
/**
* 商品减库存
*
* @param goodsId
* @return 如果更新行数大于1,表示更新的行数
*/
int reduceNumber(long goodsId);
/**
* 使用存储过程执行抢购
*
* 能提升并发性的原因:
* 1、减少多个sql语句执行来回的网络延时。
* 2、通过mysql自身的事物提升效率。
*
* @param paramMap
*/
void bugWithProcedure(Map<String, Object> paramMap);
}
package com.yingjun.ssm.dao;
import com.yingjun.ssm.entity.Order;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface OrderDao {
/**
* 插入订单明细
*
* @param userId
* @param goodsId
* @return
*/
int insertOrder(@Param("userId") long userId,@Param("goodsId") long goodsId, @Param("title")String title);
/**
* 根据用户手机号查询订单
*
* @param userPhone
* @return
*/
List<Order> queryByUserPhone(@Param("userPhone") long userPhone);
/**
* 根据偏移量查询订单列表
* @param offset
* @param limit
* @return
*/
List<Order> queryAll(@Param("offset") int offset, @Param("limit") int limit);
}
package com.yingjun.ssm.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.yingjun.ssm.entity.User;
public interface UserDao {
/**
* 根据手机号查询用户对象
*
* @param userPhone
* @return
*/
User queryByPhone(long userPhone);
/**
* 根据偏移量查询用户列表
*
* @param offset
* @param limit
* @return
*/
List<User> queryAll(@Param("offset") int offset, @Param("limit") int limit);
/**
* 增加积分
*/
void addScore(@Param("add")int add);
}
package com.yingjun.ssm.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.yingjun.ssm.util.CustomDateSerializer;
import com.yingjun.ssm.validator.Not999;
import javax.validation.constraints.Min;
import java.util.Date;
public class Goods {
@Min(900)
@Not999 //这个为自定义的验证标签
private long goodsId;
private String title;
private float price;
private short state;//0表示下架 1表示正常
private int number;
//这里展示了jackson封装好的以及自定义的对时间格式的转换方式
//后续对于一些复杂的转换可以自定义转换方式
@JsonFormat(pattern = "yyyy-MM-dd")
private Date createTime;
@JsonSerialize(using = CustomDateSerializer.class)
private Date updateTime;
public long getGoodsId() {
return goodsId;
}
public void setGoodsId(long goodsId) {
this.goodsId = goodsId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public short getState() {
return state;
}
public void setState(short state) {
this.state = state;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@Override
public String toString() {
return "Goods [goodsId=" + goodsId + ", title=" + title + ", price=" + price + ", state=" + state + ", number=" + number + ", createTime="
+ createTime + ", updateTime=" + updateTime + "]";
}
}
package com.yingjun.ssm.entity;
import java.util.Date;
/**
* 订单
* @author yingjun
*
*/
public class Order {
private long orderId;
private User user;
private long goodsId;
private String title;
private Date createTime;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public long getOrderId() {
return orderId;
}
public void setOrderId(long orderId) {
this.orderId = orderId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public long getGoodsId() {
return goodsId;
}
public void setGoodsId(long goodsId) {
this.goodsId = goodsId;
}
@Override
public String toString() {
return "Order [user=" + user + ", orderId=" + orderId + ", goodsId=" + goodsId + ", title=" + title + ", createTime=" + createTime + "]";
}
}
package com.yingjun.ssm.entity;
import java.util.Date;
/**
* 用户
* @author yingjun
*
*/
public class User {
private long userId;
private String userName;
private long userPhone;
private Date createTime;
private int score;
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public long getUserPhone() {
return userPhone;
}
public void setUserPhone(long userPhone) {
this.userPhone = userPhone;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", userPhone=" + userPhone + ", createTime=" + createTime + ", score=" + score
+ "]";
}
}
package com.yingjun.ssm.service;
import com.yingjun.ssm.entity.Goods;
import java.util.List;
public interface GoodsService {
/**
* 根据偏移量查询可用商品列表
*
* @param offset
* @param limit
* @return
*/
List<Goods> getGoodsList(int offset, int limit);
/**
* 商品购买
*
* @param userPhone
* @param goodsId
* @param useProcedure
* 是否用存储过程提高并发能力
*/
void buyGoods(long userPhone, long goodsId, boolean useProcedure);
}
package com.yingjun.ssm.service;
import java.util.List;
import com.yingjun.ssm.entity.User;
public interface UserService {
List<User> getUserList(int offset, int limit);
}
package com.yingjun.ssm.service.impl;
import com.yingjun.ssm.entity.Goods;
import com.yingjun.ssm.service.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class GoodsServiceImpl implements GoodsService {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public List<Goods> getGoodsList(int offset, int limit) {
return null;
}
@Transactional
@Override
public void buyGoods(long userPhone, long goodsId, boolean useProcedure) {
}
}
package com.yingjun.ssm.service.impl;
import com.yingjun.ssm.entity.User;
import com.yingjun.ssm.service.UserService;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Override
public List<User> getUserList(int offset, int limit) {
return null;
}
}
package com.yingjun.ssm.web;
import com.yingjun.ssm.dto.BaseResult;
import com.yingjun.ssm.entity.Goods;
import com.yingjun.ssm.enums.ResultEnum;
import com.yingjun.ssm.exception.BizException;
import com.yingjun.ssm.service.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.util.List;
@Controller
@RequestMapping("/goods")
public class GoodsController {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Autowired
private GoodsService goodsService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(Model model, Integer offset, Integer limit) {
LOG.info("invoke----------/goods/list");
// offset = offset == null ? 0 : offset;//默认便宜0
// limit = limit == null ? 50 : limit;//默认展示50条
// List<Goods> list = goodsService.getGoodsList(offset, limit);
// model.addAttribute("goodslist", list);
return "goodslist";
}
@RequestMapping(value = "/{goodsId}/buy", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
@ResponseBody
public BaseResult<Object> buy(@CookieValue(value = "userPhone", required = false) Long userPhone,
/*@PathVariable("goodsId") Long goodsId*/ @Valid Goods goods, BindingResult result) {
LOG.info("invoke----------/" + goods.getGoodsId() + "/buy userPhone:" + userPhone);
// if (userPhone == null) {
// return new BaseResult<Object>(false, ResultEnum.INVALID_USER.getMsg());
// }
//Valid 参数验证(这里注释掉,采用AOP的方式验证,见BindingResultAop.java)
//if (result.hasErrors()) {
// String errorInfo = "[" + result.getFieldError().getField() + "]" + result.getFieldError().getDefaultMessage();
// return new BaseResult<Object>(false, errorInfo);
//}
try {
//goodsService.buyGoods(userPhone, goods.getGoodsId(), false);
} catch (BizException e) {
return new BaseResult<Object>(false, e.getMessage());
} catch (Exception e) {
return new BaseResult<Object>(false, ResultEnum.INNER_ERROR.getMsg());
}
return new BaseResult<Object>(true, null);
}
}
<?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="com.yingjun.ssm.dao.GoodsDao">
<select id="queryAll" resultType="Goods">
SELECT *
FROM _goods
ORDER BY create_time DESC
limit #{offset},#{limit}
</select>
<update id="reduceNumber">
UPDATE _goods
SET
number = number -1
WHERE
goods_id = #{goodsId}
AND state = 1
AND number >0;
</update>
<!--mybatis调用存储过程-->
<select id="bugWithProcedure" statementType="CALLABLE">
call execute_buy(
#{userId,jdbcType=BIGINT,mode=IN},
#{goodsId,jdbcType=BIGINT,mode=IN},
#{title,jdbcType=VARCHAR,mode=IN},
#{result,jdbcType=INTEGER,mode=OUT}
)
</select>
</mapper>
\ No newline at end of file
<?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="com.yingjun.ssm.dao.OrderDao">
<insert id="insertOrder">
INSERT INTO
_order(user_id,goods_id,title)
VALUES
(#{userId},#{goodsId},#{title})
</insert>
<select id="queryByUserPhone" resultType="Order">
SELECT *
FROM
_order, _user
WHERE
_order.user_id=_user.user_id
AND
_user.user_phone=#{userPhone}
</select>
<select id="queryAll" resultType="Order">
SELECT *
FROM _order
ORDER BY
create_time DESC
limit #{offset},#{limit}
</select>
</mapper>
\ No newline at end of file
<?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="com.yingjun.ssm.dao.UserDao">
<select id="queryByPhone" resultType="User">
SELECT *
FROM _user
WHERE
user_phone= #{userPhone} limit 1
</select>
<select id="queryAll" resultType="User">
SELECT *
FROM _user
ORDER BY create_time DESC
limit #{offset},#{limit}
</select>
<update id="addScore">
UPDATE _user
SET
score = score + #{add}
</update>
</mapper>
\ No newline at end of file
package com.yingjun.ssm.dao;
import com.yingjun.ssm.entity.Goods;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/spring-dao.xml")
public class GoodsDaoTest {
@Autowired
private GoodsDao goodsDao;
@Test
public void testQueryAll() {
List<Goods> list=goodsDao.queryAll(0, 100);
for (Goods goods : list) {
System.out.println(goods);
}
System.out.println("--------------------------");
}
@Test
public void testReduceNumber() {
int result=goodsDao.reduceNumber(1000);
System.out.println("testReduceNumber result:"+result);
System.out.println("--------------------------");
}
@Test
public void testBugWithProcedure() {
Map<String,Object> map=new HashMap<String,Object>();
map.put("userId", 1000L);
map.put("goodsId", 1000L);
map.put("title", "抢购iPhone7");
map.put("result", null);
goodsDao.bugWithProcedure(map);
//获取result
System.out.println("testBugWithProcedure result:"+map.get("result"));
}
}
package com.yingjun.ssm.dao;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.yingjun.ssm.entity.Goods;
import com.yingjun.ssm.entity.Order;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/spring-dao.xml")
public class OrderDaoTest {
@Autowired
private OrderDao orderDao;
@Autowired
private GoodsDao goodsDao;
@Test
public void testInsertOrder() {
Goods goods=goodsDao.queryAll(0, 1).get(0);
System.out.println(goods);
int result=orderDao.insertOrder(1000,goods.getGoodsId(),goods.getTitle());
System.out.println("testInsertOrder result:"+result);
System.out.println("--------------------------");
}
@Test
public void testQueryByUserPhone() {
List<Order> list=orderDao.queryByUserPhone(18768128888L);
for (Order order : list) {
System.out.println(order);
}
System.out.println("--------------------------");
}
@Test
public void testQueryAll() {
List<Order> list=new ArrayList<Order>();
for (Order order : list) {
System.out.println(order);
}
System.out.println("--------------------------");
}
}
package com.yingjun.ssm.dao;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.yingjun.ssm.entity.User;
/**
*
* @author yingjun
*
* @author liyunfeng
* 普通用户
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/spring-dao.xml")
public class UserDaoTest {
@Autowired
private UserDao userDao;
@Test
public void testQueryById() {
User user=userDao.queryByPhone(18768128888L);
System.out.println(user);
System.out.println("--------------------------");
}
@Test
public void testQueryAll() {
List<User> list=userDao.queryAll(0, 100);
for (User user : list) {
System.out.println(user);
}
}
// @Autowired
// private UserDao userDao;
@Test
public void testAddScore() {
userDao.addScore(10);
List<User> list=userDao.queryAll(0, 100);
for (User user : list) {
System.out.println(user);
}
}
// @Test
// public void testQueryById() {
// User user=userDao.queryByPhone(18768128888L);
// System.out.println(user);
// System.out.println("--------------------------");
// }
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册