后端:增加 Banner 增删改查接口

This commit is contained in:
YunaiV
2019-03-30 21:57:01 +08:00
parent 0256a4da17
commit 4595db1d4c
41 changed files with 1543 additions and 19 deletions

View File

@@ -0,0 +1,14 @@
package cn.iocoder.mall.promotion.biz.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@MapperScan("cn.iocoder.mall.promotion.biz.dao") // 扫描对应的 Mapper 接口
@EnableTransactionManagement(proxyTargetClass = true) // 启动事务管理。为什么使用 proxyTargetClass 参数,参见 https://blog.csdn.net/huang_550/article/details/76492600
public class DatabaseConfiguration {
// 数据源,使用 HikariCP
}

View File

@@ -0,0 +1,26 @@
package cn.iocoder.mall.promotion.biz.config;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
@Configuration
public class ServiceExceptionConfiguration {
@EventListener(ApplicationReadyEvent.class) // 可参考 https://www.cnblogs.com/ssslinppp/p/7607509.html
public void initMessages() {
// 从 service_exception_message.properties 加载错误码的方案
// Properties properties;
// try {
// properties = PropertiesLoaderUtils.loadAllProperties("classpath:service_exception_message.properties");
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
for (UserErrorCodeEnum item : UserErrorCodeEnum.values()) {
ServiceExceptionUtil.put(item.getCode(), item.getMessage());
}
}
}

View File

@@ -0,0 +1,30 @@
package cn.iocoder.mall.promotion.biz.convert;
import cn.iocoder.mall.promotion.api.bo.BannerBO;
import cn.iocoder.mall.promotion.api.dto.BannerAddDTO;
import cn.iocoder.mall.promotion.api.dto.BannerUpdateDTO;
import cn.iocoder.mall.promotion.biz.dataobject.BannerDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface BannerConvert {
BannerConvert INSTANCE = Mappers.getMapper(BannerConvert.class);
@Mappings({})
BannerBO convertToBO(BannerDO banner);
@Mappings({})
List<BannerBO> convertToBO(List<BannerDO> bannerList);
@Mappings({})
BannerDO convert(BannerAddDTO bannerAddDTO);
@Mappings({})
BannerDO convert(BannerUpdateDTO bannerUpdateDTO);
}

View File

@@ -0,0 +1,24 @@
package cn.iocoder.mall.promotion.biz.dao;
import cn.iocoder.mall.promotion.biz.dataobject.BannerDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BannerMapper {
BannerDO selectById(@Param("id") Integer id);
List<BannerDO> selectListByTitleLike(@Param("title") String title,
@Param("offset") Integer offset,
@Param("limit") Integer limit);
Integer selectCountByTitleLike(@Param("title") String title);
void insert(BannerDO bannerDO);
int update(BannerDO bannerDO);
}

View File

@@ -0,0 +1,106 @@
package cn.iocoder.mall.promotion.biz.dataobject;
import cn.iocoder.common.framework.dataobject.DeletableDO;
/**
* Banner 广告页
*/
public class BannerDO extends DeletableDO {
/**
* 编号
*/
private Integer id;
/**
* 标题
*/
private String title;
/**
* 跳转链接
*/
private String url;
/**
* 图片链接
*/
private String picUrl;
/**
* 排序
*/
private Integer sort;
/**
* 状态
*
* {@link cn.iocoder.common.framework.constant.CommonStatusEnum}
*/
private Integer status;
/**
* 备注
*/
private String memo;
// TODO 芋艿 点击次数。&& 其他数据相关
public Integer getId() {
return id;
}
public BannerDO setId(Integer id) {
this.id = id;
return this;
}
public String getTitle() {
return title;
}
public BannerDO setTitle(String title) {
this.title = title;
return this;
}
public String getUrl() {
return url;
}
public BannerDO setUrl(String url) {
this.url = url;
return this;
}
public Integer getStatus() {
return status;
}
public BannerDO setStatus(Integer status) {
this.status = status;
return this;
}
public String getMemo() {
return memo;
}
public BannerDO setMemo(String memo) {
this.memo = memo;
return this;
}
public Integer getSort() {
return sort;
}
public BannerDO setSort(Integer sort) {
this.sort = sort;
return this;
}
public String getPicUrl() {
return picUrl;
}
public BannerDO setPicUrl(String picUrl) {
this.picUrl = picUrl;
return this;
}
}

View File

@@ -0,0 +1 @@
package cn.iocoder.mall.promotion.biz;

View File

@@ -0,0 +1,92 @@
package cn.iocoder.mall.promotion.biz.service;
import cn.iocoder.common.framework.constant.CommonStatusEnum;
import cn.iocoder.common.framework.constant.DeletedStatusEnum;
import cn.iocoder.common.framework.constant.SysErrorCodeEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.promotion.api.BannerService;
import cn.iocoder.mall.promotion.api.bo.BannerBO;
import cn.iocoder.mall.promotion.api.bo.BannerPageBO;
import cn.iocoder.mall.promotion.api.constant.PromotionErrorCodeEnum;
import cn.iocoder.mall.promotion.api.dto.BannerAddDTO;
import cn.iocoder.mall.promotion.api.dto.BannerPageDTO;
import cn.iocoder.mall.promotion.api.dto.BannerUpdateDTO;
import cn.iocoder.mall.promotion.biz.convert.BannerConvert;
import cn.iocoder.mall.promotion.biz.dao.BannerMapper;
import cn.iocoder.mall.promotion.biz.dataobject.BannerDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service // 实际上不用添加。添加的原因是,必须 Spring 报错提示
@com.alibaba.dubbo.config.annotation.Service(validation = "true")
public class BannerServiceImpl implements BannerService {
@Autowired
private BannerMapper bannerMapper;
@Override
public CommonResult<BannerPageBO> getBannerPage(BannerPageDTO bannerPageDTO) {
BannerPageBO bannerPageBO = new BannerPageBO();
// 查询分页数据
int offset = (bannerPageDTO.getPageNo() - 1) * bannerPageDTO.getPageSize();
bannerPageBO.setList(BannerConvert.INSTANCE.convertToBO(bannerMapper.selectListByTitleLike(bannerPageDTO.getTitle(),
offset, bannerPageDTO.getPageSize())));
// 查询分页总数
bannerPageBO.setTotal(bannerMapper.selectCountByTitleLike(bannerPageDTO.getTitle()));
return CommonResult.success(bannerPageBO);
}
@Override
public CommonResult<BannerBO> addBanner(Integer adminId, BannerAddDTO bannerAddDTO) {
// 保存到数据库
BannerDO banner = BannerConvert.INSTANCE.convert(bannerAddDTO);
banner.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()).setCreateTime(new Date());
bannerMapper.insert(banner);
// 返回成功
return CommonResult.success(BannerConvert.INSTANCE.convertToBO(banner));
}
@Override
public CommonResult<Boolean> updateBanner(Integer adminId, BannerUpdateDTO bannerUpdateDTO) {
// 校验 Banner 存在
if (bannerMapper.selectById(bannerUpdateDTO.getId()) == null) {
return ServiceExceptionUtil.error(PromotionErrorCodeEnum.BANNER_NOT_EXISTS.getCode());
}
// 更新到数据库
BannerDO updateBanner = BannerConvert.INSTANCE.convert(bannerUpdateDTO);
bannerMapper.update(updateBanner);
// 返回成功
return CommonResult.success(true);
}
@Override
public CommonResult<Boolean> updateBannerStatus(Integer adminId, Integer bannerId, Integer status) {
// 校验参数
if (!CommonStatusEnum.isValid(status)) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "变更状态必须是开启1或关闭2"); // TODO 有点搓
}
// 更新到数据库
BannerDO updateBanner = new BannerDO().setId(bannerId).setStatus(status);
bannerMapper.update(updateBanner);
// 返回成功
return CommonResult.success(true);
}
@Override
public CommonResult<Boolean> deleteBanner(Integer adminId, Integer bannerId) {
// 校验 Banner 存在
if (bannerMapper.selectById(bannerId) == null) {
return ServiceExceptionUtil.error(PromotionErrorCodeEnum.BANNER_NOT_EXISTS.getCode());
}
// 更新到数据库
BannerDO updateBanner = new BannerDO().setId(bannerId);
updateBanner.setDeleted(DeletedStatusEnum.DELETED_YES.getValue());
bannerMapper.update(updateBanner);
// 返回成功
return CommonResult.success(true);
}
}

View File

@@ -0,0 +1,30 @@
spring:
# datasource
datasource:
url: jdbc:mysql://180.167.213.26:13306/mall_promotion?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: ${MALL_MYSQL_PASSWORD}
# mybatis
mybatis:
config-location: classpath:mybatis-config.xml
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.iocoder.mall.promotion.biz.dataobject
# dubbo
dubbo:
application:
name: promotion-service
registry:
address: zookeeper://127.0.0.1:2181
protocol:
port: -1
name: dubbo
scan:
base-packages: cn.iocoder.mall.promotion.biz.service
# logging
logging:
level:
cn.iocoder.mall.promotion.dao: debug

View File

@@ -0,0 +1,98 @@
<?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="cn.iocoder.mall.promotion.biz.dao.BannerMapper">
<sql id="FIELDS">
id, title, url, pic_url, sort,
status, memo, create_time
</sql>
<!-- <select id="selectListByPidAndStatusOrderBySort" resultType="BannerDO">-->
<!-- SELECT-->
<!-- <include refid="FIELDS" />-->
<!-- FROM banner-->
<!-- WHERE pid = #{pid}-->
<!-- AND status = #{status}-->
<!-- AND deleted = 0-->
<!-- ORDER BY sort ASC-->
<!-- </select>-->
<!-- <select id="selectList" resultType="BannerDO">-->
<!-- SELECT-->
<!-- <include refid="FIELDS" />-->
<!-- FROM banner-->
<!-- WHERE deleted = 0-->
<!-- </select>-->
<select id="selectById" parameterType="Integer" resultType="BannerDO">
SELECT
<include refid="FIELDS" />
FROM banner
WHERE id = #{id}
AND deleted = 0
</select>
<select id="selectListByTitleLike" resultType="BannerDO">
SELECT
<include refid="FIELDS" />
FROM banner
<where>
<if test="title != null">
title LIKE "%"#{title}"%"
</if>
AND deleted = 0
</where>
LIMIT #{offset}, #{limit}
</select>
<select id="selectCountByTitleLike" resultType="Integer">
SELECT
COUNT(1)
FROM banner
<where>
<if test="title != null">
title LIKE "%"#{title}"%"
</if>
AND deleted = 0
</where>
</select>
<insert id="insert" parameterType="BannerDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO banner (
title, url, pic_url, sort, status,
memo, create_time, deleted
) VALUES (
#{title}, #{url}, #{picUrl}, #{sort}, #{status},
#{memo}, #{createTime}, #{deleted}
)
</insert>
<update id="update" parameterType="BannerDO">
UPDATE banner
<set>
<if test="title != null">
title = #{title},
</if>
<if test="url != null">
url = #{url},
</if>
<if test="pic_url != null">
pic_url = #{picUrl} ,
</if>
<if test="sort != null">
sort = #{sort},
</if>
<if test="status != null">
status = #{status},
</if>
<if test="VALUES != null">
VALUES = #{VALUES},
</if>
<if test="deleted != null">
deleted = #{deleted}
</if>
</set>
WHERE id = #{id}
</update>
</mapper>

View File

@@ -0,0 +1,19 @@
<?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>
<settings>
<!-- 使用驼峰命名法转换字段。 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer"/>
<typeAlias alias="Long" type="java.lang.Long"/>
<typeAlias alias="HashMap" type="java.util.HashMap"/>
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap"/>
<typeAlias alias="ArrayList" type="java.util.ArrayList"/>
<typeAlias alias="LinkedList" type="java.util.LinkedList"/>
</typeAliases>
</configuration>