关于java:复杂项目的创建

9次阅读

共计 3913 个字符,预计需要花费 10 分钟才能阅读完成。

我的项目的创立

首创立一个父我的项目,用来治理所有子项目中要用到的公共依赖,打包形式必须为 pom, 这样能力被其余我的项目依赖。

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

另一个为咱们的 manage 我的项目,用来解决业务。(war 打包打包形式)

因为业务中会用到咱们的工具类(common 包中的),所以会将 common 中的以依赖的形式增加进来。

jar war pom 之间的区别

pom:打进去能够作为其余我的项目的 maven 依赖,在工程 A 中增加工程 B 的 pom,A 就能够应用 B 中的类。用在父级工程或聚合工程中。用来做 jar 包的版本控制。

jar 包:通常是开发时要援用通用类,打成 jar 包便于寄存治理。当你应用某些性能时就须要这些 jar 包的反对,须要导入 jar 包。

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

配置 YML 文件

批改启动项

当咱们在 YML 中配置的 mvc 不是 idea 默认形式,有可能拜访不到,须要批改启动项。


JSON 阐明

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

什么是 json

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

json 的模式 l

{“string”:value,”string”:value….}
string– 名称(sring 类型)value– 具体值
相似 List<Map<String,Object>> 构造。其中 value 为任意类型,也可为 json,相当于嵌套 json。

编辑 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<Item> 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);
    }
}

创立业务层和接口


接口采纳 Mybatis-Plus 实现

package com.jt.mapper;

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

public interface ItemMapper extends BaseMapper<Item>{}

业务层编写

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<Item> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("updated");// 依据更新工夫排序
        mpPage = itemMapper.selectPage((mpPage), queryWrapper);
        long total = mpPage.getTotal();// 获取记录总数
        List<Item> 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();
    }
}
正文完
 0