本帖最后由 我是楠楠 于 2019-12-5 11:22 编辑
【郑州校区】学成在线 第1天 讲义-项目概述 CMS接口开发 五
6 页面查询服务端开发
6.1 创建CMS服务工程
6.1.1 创建CMS工程结构
创建maven工程, CMS工程的名称为 xc-service-manage-cms,父工程为xc-framework-parent。
pom.xml如下:
[AppleScript] 纯文本查看 复制代码 <?xml version="1.0"encoding="UTF‐8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="
[url=http://www.w3.org/2001/XMLSchema]http://www.w3.org/2001/XMLSchema[/url]‐instance"xsi:schemaLocation="
[url=http://maven.apache.org/POM/4.0.0]http://maven.apache.org/POM/4.0.0[/url]
[url=http://maven.apache.org/xsd/maven]http://maven.apache.org/xsd/maven[/url]‐4.0.0.xsd">
<parent>
<artifactId>xc‐framework‐
parent</artifactId>
<groupId>com.xuecheng</groupId>
<version>1.0‐SNAPSHOT</version>
<relativePath>../xc‐framework‐
parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>xc‐service‐manage‐
cms</artifactId>
<dependencies>
<dependency>
<groupId>com.xuecheng</groupId>
<artifactId>xc‐service‐
api</artifactId>
<version>1.0‐
SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.xuecheng</groupId>
<artifactId>xc
‐
framework
‐
model</artifactId>
<version>1.0
‐
SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.xuecheng</groupId>
<artifactId>xc
‐
framework
‐
utils</artifactId>
<version>1.0
‐
SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.xuecheng</groupId>
<artifactId>xc
‐
framework
‐
common</artifactId>
<version>1.0
‐
SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org
.springframework.boot</groupId>
<artifactId>spring
‐
boot
‐
starter
‐
web</artifactId>
</dependency>
<dependency>
<groupId>org
.springframework.boot</groupId>
<artifactId>spring
‐
boot
‐
starter
‐
freemarker</artifactId>
</dependency>
<dependency>
<groupId>org
.springframework.boot</groupId>
<artifactId>spring
‐
boot
‐
starter
‐
data
‐
mongodb</artifactId>
</dependency>
<dependency>
<groupId>org
.springframework.boot</groupId>
<artifactId>spring
‐
boot
‐
starter
‐
amqp</artifactId>
</dependency>
<dependency>
<groupId>com.squareup
.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>org
.springframework.boot</groupId>
<artifactId>spring
‐
boot
‐
starter
‐
test</artifactId>
</dependency>
</dependencies>
</project>
由于cms工程要连接mongodb所以需要在在cms服务端工程添加如下依赖:
项目使用spring data mongodb操作mongodb数据库
[AppleScript] 纯文本查看 复制代码 <dependency>[/size][/font]
[font=微软雅黑][size=3]<groupId>org
.springframework.boot</groupId>
<artifactId>spring
‐
boot
‐
starter
‐
data
‐
mongodb</artifactId>
</dependency>
2、创建基本的包结构:
com.xuecheng
.manage_cms.confifig:配置类目录,数据库配置、MQ配置等
com.xuecheng
.manage_cms.dao:dao接口目录
com.xuecheng
.manage_cms.service:service类目录
com.xuecheng
.manage_cms.web.controller:controller类目录
工程结构如下
3、配置文件
在classpath下配置application.yml
[AppleScript] 纯文本查看 复制代码 server:
port: 31001
spring:
application:
name: xc‐service‐manage‐cms
data:mongodb:
uri: mongodb://root:123@localhost:27017
database: xc_cms
另外从课程资料下“cms工程配置文件”中拷贝logback-spring.xml,此文件为工程的日志配置文件。
4、SpringBoot 启动类
Spring Boot应用需要创建一个应用启动类,启动过程中会扫描Bean并注入spring 容器
注意:此类创建在本工程com.xuecheng.manage_cms包下
[AppleScript] 纯文本查看 复制代码 @SpringBootApplication
@EntityScan(
"
com.xuecheng
.framework.domain.cms
"
)//扫描实体类
@ComponentScan(basePackages
=
{
"
com.xuecheng
.api
"
})//扫描接口
@ComponentScan(basePackages
=
{
"
com.xuecheng
.manage_cms
"
})//扫描本项目下的所有类
public class ManageCmsApplication {
public static void main(String[] args) {
SpringApplication.run(ManageCmsApplication.class,args);
}
}
6.1.2 测试Controller
使用springMVC完成接口实现开发,这里暂时使用测试数据,稍后会让controller调用service来查询数据。
[AppleScript] 纯文本查看 复制代码 package com.xuecheng
.manage_cms.web.controller;
import com.xuecheng
.api.cms.CmsPageControllerApi;
import com.xuecheng
.framework.domain.cms.request.
QueryPageRequest;
import com.xuecheng
.framework.model.response.
QueryResponseResult;
import com.xuecheng
.manage_cms.service.PageService;
import org
.springframework.beans.factory
.annotation.Autowired;
import org
.springframework.web.bind.annotation.PathVariable;
import org
.springframework.web.bind.annotation.RestController;
@RestController
public class CmsPageController implements CmsPageControllerApi {
@Override
@GetMapping(
"
/list/{page}/{size}
"
)
public QueryResponseResult findList(@PathVariable(
"
page
"
) int page,@PathVariable(
"
size
"
) int
size,QueryPageRequest queryPageRequest) {
//暂时采用测试数据,测试接口是否可以正常运行
QueryResult queryResult
= new QueryResult();
queryResult.setTotal(2);
//静态数据列表
List list
= new ArrayList();
CmsPage cmsPage
= new CmsPage();
cmsPage.setPageName(
"
测试页面
"
);
list.add(cmsPage)
queryResult.setList(list);
QueryResponseResult queryResponseResult
= new
QueryResponseResult(CommonCode.SUCCESS,queryResult);
return queryResponseResult;
}
}
使用浏览器测试
输入:http://localhost:31001/cms/page/list/1/10 查询第1页,每页显示10条记录
|