4-6作业,做一遍,有助于深刻理解
来源:4-6 本阶段练习题
starxigua
2020-11-03
目录
├── datamodels
│ ├── movie.go
│ └── student.go
├── go.mod
├── go.sum
├── main.go
├── repositories
│ ├── movie_repository.go
│ └── student_repository.go
├── services
│ ├── movie_service.go
│ └── student_service.go
└── web
├── controllers
│ ├── movie_controller.go
│ └── student_controller.go
└── views
├── movie
│ ├── index.html
│ └── test.html
└── student
└── index.html
创建学生模型,其中包括的属性有 ,学生ID,名称,年龄,性别
datamodels/student.go
package datamodels
type Student struct {
Id int
Name string
Age int
Gender string
}
创建学生数据映射关系结构体,接口中定义查询学生的方法
实现映射关系结构体,查询方法实现逻辑分为两个步骤:第一步:创建学生模式,并且赋值;第二步:返回已经创建的学生模型
repositories/student_repository.go
package repositories
import "imoocLearn/immoc-iris/datamodels"
type StudentRepository interface {
GetStudentName() string
}
type StudentManager struct {
Id int
Name string
Age int
Gender string
}
func NewStudentManager() StudentRepository {
return &StudentManager{}
}
func (receiver StudentManager) GetStudentName() string {
//模拟赋值给模型
student := &datamodels.Student{
Id: 1,
Name: "iMooc",
Age: 30,
Gender: "male",
}
return student.Name
}
创建学生的对应service,返回学生数据映射关系中查询到的学生数据
services/student_service.go
package services
import (
"fmt"
"imoocLearn/immoc-iris/repositories"
)
type StudentService interface {
ShowStudentName() string
}
type StudentServiceManager struct {
repo repositories.StudentRepository
}
//init()
func NewStudentServiceManager(repo repositories.StudentRepository) StudentService{
return &StudentServiceManager{repo: repo}
}
func (receiver StudentServiceManager) ShowStudentName() string {
fmt.Println("我们获取到的学生名称为:" + receiver.repo.GetStudentName())
return "我们获取到的学生名称为:" + receiver.repo.GetStudentName()
}
新建学生了控制器,完成页面信息模版嵌套
web/controllers/student_controller.go
package controllers
import (
"github.com/kataras/iris/v12/mvc"
"imoocLearn/immoc-iris/repositories"
"imoocLearn/immoc-iris/services"
)
type StudentController struct {
}
func (receiver StudentController) Get() mvc.View {
studentRepository := repositories.NewStudentManager()
studentService := services.NewStudentServiceManager(studentRepository)
studentResult:= studentService.ShowStudentName()
return mvc.View{
Name: "student/index.html",
Data: studentResult,
}
}
注册/student路由,能够通过浏览器访问展示信息
main.go
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
"imoocLearn/immoc-iris/web/controllers"
)
func main() {
app := iris.New()
app.Logger().SetLevel("debug")
app.RegisterView(iris.HTML("./web/views", ".html"))
//注册控制器
mvc.New(app.Party("/hello")).Handle(new(controllers.MovieController))
mvc.New(app.Party("/student")).Handle(new(controllers.StudentController))
app.Run(
// 启动服务在8080端口
iris.Addr("localhost:8080"),
// 启动时禁止检测框架版本差异
//iris.WithoutVersionChecker,
//忽略服务器错误
iris.WithoutServerError(iris.ErrServerClosed),
//让程序自身尽可能的优化
iris.WithOptimizations,
)
}
web/views/student/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Info</title>
</head>
<body>
<h2>{{.}}</h2>
</body>
</html>
写回答
1回答
-
Cap
2020-11-03
nice
00
相似问题