关于java:复杂项目的创建

我的项目的创立

首创立一个父我的项目,用来治理所有子项目中要用到的公共依赖,打包形式必须为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();
    }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理