复杂项目的创建

项目的创建

首创建一个父项目,用来管理所有子项目中要用到的公共依赖,打包方式必须为pom,这样才能被其他项目依赖。
image.png

在父项目里面再创建子项目,通常会有一个common项目,用来管理公共的API,便于调用。(默认打包方式为jar)。

另一个为我们的manage项目,用来处理业务。(war打包打包方式)
image.png
因为业务中会用到我们的工具类(common包中的),所以会将common中的以依赖的方式添加进来。

jar war pom 之间的区别

pom:打出来可以作为其他项目的maven依赖,在工程A中添加工程B的pom,A就可以使用B中的类。用在父级工程或聚合工程中。用来做jar包的版本控制。

jar包:通常是开发时要引用通用类,打成jar包便于存放管理。当你使用某些功能时就需要这些jar包的支持,需要导入jar包。

war包:是做好一个web网站后,打成war包部署到服务器。目的是节省资源,提供效率。

配置YML文件

image.png

修改启动项

当我们在YML中配置的mvc不是idea默认方式,有可能访问不到,需要修改启动项。
image.png


JSON说明

在项目中,后端程序通常会返回一个pojo对象,但有些数据页面处理非常麻烦,通常会将pojo对象转化为vo(json)对象,再交给客户端处理。

什么是json

JSON(JavaScript Object Notation)是一种轻量级数据交换格式。它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。json本质是String(字符串)

json 的形式l

{"string":value,"string":value....}
string--名称(sring类型) value--具体值
类似List>结构。其中value为任意类型,也可为json,相当于嵌套json。
image.png

编辑EasyUITable VO对象

package com.jt.vo;

import com.jt.pojo.Item;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class EasyUITable {
    private Long total;//总记录数
    private List rows;//每页显示的记录
}

IndexController实现页面分页

package com.jt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
    /**
     * IndexController控制层实现分页
     *     在我们的项目中,每一个页面请求会对应一个处理请求,如果请求页面过多,需要不断处理
     *    @RequestMapping("IndexUI")
     *    public String IndexUI(){
     *         return "index";
     *    }....
     *
     * 通常会采用rest风格来处理来解决。
     *    1 动态获取url中的地址当作参数,并作为返回静态页面的名字。
     *    @RequestMapping("/page/{moduleName}")
     *    public String module(@PathVariable String moduleName) {
     *         return moduleName;
     *    }
     *  2 按照不同的业务逻辑,采用不同的请求方式,但不是该请求时会被自动过滤
     *       //@RequestMapping(value = "/page/{moduleName}",method= RequestMethod.GET)
     *          @GetMapping("/page/{moduleName}")
     *          public String itemAdd(@PathVariable String moduleName){
     *              return moduleName;
     *         }
     *
     *
     */
    @RequestMapping("/page/{moduleName}")
    public String module(@PathVariable String moduleName) {
        
        return moduleName;
    }
}

创建controller层来处理业务

package com.jt.controller;

import com.jt.service.ItemService;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/item/")
public class ItemController {
    
    @Autowired
    private ItemService itemService;

    /**
     * 业务:分页查询商品信息
     * url: http://localhost:8091/item/query?page=1&rows=20
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("query")
    public EasyUITable findItemByPage(int page,int rows){
        return itemService.findItemByPage(page,rows);
    }
}

创建业务层和接口

image.png

接口采用Mybatis-Plus实现

package com.jt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.Item;

public interface ItemMapper extends BaseMapper{
    
}

业务层编写

import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import com.jt.service.ItemService;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    
    @Autowired
    private ItemMapper itemMapper;//这里报红设置为警告即可

    @Override
    public EasyUITable findItemByPage(int page, int rows) {
        /**
         * 利用Mybatis-Plus MP方式实现分页,不用写Sql语句
         * 业务不太复杂时也可自己手写
         * 这里还需要配置类
         */
        IPage mpPage = new Page(page,rows);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("updated");//根据更新时间排序
        mpPage = itemMapper.selectPage((mpPage), queryWrapper);
        long total = mpPage.getTotal();//获取记录总数
        List itemList = mpPage.getRecords();//获取查询当前页
        return new EasyUITable(total,itemList);
    }
}

添加配置类

package com.jt.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {
    //将分页拦截器交给spring 容器管理,MP是Mybatis增强工具
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }
}

你可能感兴趣的:(java,spring)