SpringMVC文件结构

一、RequestMapping注解的多值映射

@RequestMapping(value = {"/album"})在类上添加Mapping,在mapping中可以添加多数组,当用户请求不同的地址时指向同一路径。

BeanUtils.copyProperties(brandDTO,brand);赋值,将变量名相同的赋值,

BeanUtils.copyProperties(获取的值,需要赋值的值);

@RestControllerAdvice:全局

二、SpringMVC结构

​​​​​​​

 

2.1

        2.1.1 config:配置文件-MybatisConfiguration,用于表示mybatis的xml文件位置

@Configuration
@MapperScan("cn.tude.csmall.product.mapper")
public class MybatisConfiguration {
}

       2.1.2 controller:控制层-用于接受客户端的请求,并响应回去(只做响应和接受不做逻辑功能),调用实现类的接口

@Slf4j
@RestControllerAdvice
@RequestMapping(value = {"/album"})
public class AlbumController {
    @Autowired
    IAlbumService albumService;
    @RequestMapping(value = "/add-new")
    public String addNew(AlbumAddNewDTO albumAddNewDTO){
            albumService.addNew(albumAddNewDTO);
            return "添加成功";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        albumService.delete(id);
        return "删除成功";
    }
}

       2.1.3 ex:异常类-发生异常时自己写的异常存在在这里(自定义不做异常功能)

public class ServiceException extends RuntimeException{
    public ServiceException(String message) {
        super(message);
    }
}

                2.1.3.1ex-handler:定义全局异常-为ex子包

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public String handleServiceException(ServiceException e) {
        log.debug("捕获到ServiceException:{}", e.getMessage());
        return e.getMessage();
    }

    @ExceptionHandler
    public String handleServiceException(Throwable e) {
        log.debug("捕获到ServiceException:{}", e.getMessage());
        e.printStackTrace();
        return e.getMessage();
    }
}

       2.1.4 mapper:对数据最基础功能的描述,没有直接对数据库的增删改查

@Repository
public interface AlbumMapper {
    /**
     * 插入相册数据
     *
     * @param album
     * @return
     */
    int insert(Album album);

    /**
     * 批量插入
     *
     * @param albumList
     * @return
     */
    int insertBatch(List<Album> albumList);

    /**
     * 删除
     */
    int delete(Long id);

    /**
     * 批量删除
     */
    int deleteIds(Long... id);

    /**
     * 选择性更新字段
     *
     * @return
     */
    int updateById(Album album);

    /**
     * 查找列表数量
     */
    int count();

    /**
     * 通过名字查找列表数量
     */
    int countByName(String name);

    /**
     * 根据id查询相册信息
     *
     * @param id
     * @return 查询不到返回null
     */
    AlbumStandardVO getStandardById(Long id);

    /**
     * 查询相册列表
     *
     * @return list集合
     */
    List<AlbumListVO> list();

}

        2.1.5 pojo:实体类的存放-各种实体类都放在这

                2.1.5.1 pojo-dto:根据实际业务创建的实体类

                2.1.5.2 pojo-entity:最基本的实体类

                2.1.5.3 pojo-vo:一个页面展示所需所有数据的集合

       2.1.6  service:对应用户所需业务(实际的功能),全都是接口,实际的操作的由实现类操作

public interface IAlbumService {
    /**
     * 添加
     * @param albumAddNewDTO
     */
    void addNew(AlbumAddNewDTO albumAddNewDTO);

    /**
     * 删除
     * @param id
     */
    void delete(Long id);
}

                2.1.6.1 service-iml:业务的实现类-接受Controller的用户请求,做实际的处理,并由Controller调用处理,调用时调用他实现的接口.

@Service
public class AlbumServiceImpl implements IAlbumService {
    @Autowired
    AlbumMapper albumMapper;

    @Override
    public void addNew(AlbumAddNewDTO albumAddNewDTO) {
        int i = albumMapper.countByName(albumAddNewDTO.getName());
        if (i != 0) {
            throw new ServiceException("添加失败,名称重复");
        }
        //创建Album 类对象
        Album album = new Album();
        //insert方法只能传Album对象,所以将AlbumAddNewDTO赋值给Album
        BeanUtils.copyProperties(albumAddNewDTO, album);
        //执行插入方法
        albumMapper.insert(album);
    }

    @Override
    public void delete(Long id) {
        AlbumStandardVO standardById = albumMapper.getStandardById(id);
        if (standardById == null) {
            log.trace("删除失败{}",id);
            throw new ServiceException("删除id不存在");
        }
        albumMapper.delete(id);
    }
}

        2.1.7 web:发生异常时,返回的对象(携带着state和message),配合ex中GlobalExce ptionHandler类使用

@Data
public class JSONResult {
    private Integer state;
    private String message;
}
 @ExceptionHandler
    public JSONResult handleServiceException(ServiceException e) {
        log.debug("捕获到ServiceException:{}", e.getMessage());
        JSONResult jsonResult=new JSONResult();
        jsonResult.setState(1);
        jsonResult.setMessage(e.getMessage());
        return jsonResult;
    }

2.2

resources

       2.2.1 mapper:对数据库直接操作,对数据库的操作所配置的XML文件.与上面的mapper名字一一对应.

<?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.tude.csmall.product.mapper.AlbumMapper">
    <!--int insert(Album album);-->
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into pms_album(name)
        values (#{name})
    </insert>

    <!-- int insertBatch(List<Album> albumList);-->
    <insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
        insert into pms_album(name,description,sort)
        values
        <foreach collection="list" item="albumList" separator=",">
            (#{albumList.name},#{albumList.description},#{albumList.sort})
        </foreach>
    </insert>

    <!--int delete(Long id);-->
    <delete id="delete">
        delete
        from pms_album
        where id = #{id}
    </delete>

    <!-- int deleteIds(Long...id);-->
    <delete id="deleteIds">
        delete from pms_album where id in (
        <foreach collection="array" separator="," item="id">
            #{id}
        </foreach>
        )
    </delete>

    <!--int updateById(Album album);-->
    <update id="updateById">
        update pms_album
        <set>
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="description!=null">
                description=#{description},
            </if>
            <if test="sort!=null">
                sort=#{sort}
            </if>
        </set>
        where id=#{id}
    </update>

    <!--int count();-->
    <select id="count" resultType="int">
        select count(*)
        from pms_album
    </select>

    <!--int countByName();-->
    <select id="countByName" resultType="int">
        select count(*)
        from pms_album
        where name = #{name}
    </select>

    <!--AttributeTemplateStandardVO getStandardById(Long id)-->
    <select id="getStandardById" resultType="cn.tude.csmall.product.pojo.vo.AlbumStandardVO">
        select id, name, description, sort
        from pms_album
        where id = #{id}
    </select>

    <!--AlbumListVO List<AlbumListVO>-->
    <select id="list" resultMap="ListResultMap">
        select
        <include refid="ListResultMap"/>
        from pms_album
        order by id
    </select>
    <sql id="ListResultMap">
        <if test="true"></if>
        id, name, description, sort
    </sql>
    <resultMap id="ListResultMap" type="cn.tude.csmall.product.pojo.vo.AlbumListVO">
    </resultMap>

</mapper>

三、yml文件书写规范

        yml比较xml书写更加规划,容易阅读,遇到 . tab,赋值时=变为

 

 四、选择配置文件

#调用权限不同的配置文件 spring.profiles.active=test test 为其他配置文件的名字。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值