第四次作业后端代码

This commit is contained in:
Ylx472 2024-11-10 23:35:51 +08:00
parent a0d9d1a71d
commit df21d01487
11 changed files with 272 additions and 8 deletions

25
pom.xml
View File

@ -60,16 +60,33 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mavenrepository.org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.3.1.Final</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
</dependencies>

View File

@ -0,0 +1,34 @@
package com.c202201020140.tasks;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.nio.file.Paths;
public class Test {
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:mysql://106.53.194.250:63306/***?serverTimezone=UTC",
"**", "@hnucm1254")
.globalConfig(builder -> builder
.author("Baomidou")
.outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
.commentDate("yyyy-MM-dd")
)
.packageConfig(builder -> builder
.parent("com.baomidou.mybatisplus")
.entity("entity")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
.xml("mapper.xml")
)
.strategyConfig(builder -> builder
.entityBuilder()
.enableLombok()
)
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
}

View File

@ -0,0 +1,80 @@
package com.c202201020140.tasks.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.c202201020140.tasks.model.Result;
import com.c202201020140.tasks.model.BookList;
import com.c202201020140.tasks.service.IBookListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@CrossOrigin
@Controller //控制器负责处理来自客户端的 HTTP 请求并返回相应的响应
@RequestMapping("/bookList")
public class BookListController {
//JSON方法返回数据
@Autowired//它可以用于字段构造器setter 方法等地方Spring 容器会自动将匹配的 Bean 注入
IBookListService bookListService;
@ResponseBody //返回 JSONXML 等格式的数据
@RequestMapping("/BookList1")
public Result getBookList1(){
QueryWrapper<BookList> queryWrapper=new QueryWrapper<>();
queryWrapper.like("name", "test").or().eq("age",19);
List<BookList> list = bookListService.list(queryWrapper);
return Result.ok().put("data",list);
}
@ResponseBody //返回 JSONXML 等格式的数据
@RequestMapping("/login")
public Result login(String username,String password){
//todo=>查询数据库
if(username.equals("admin")&&password.equals("123456")){
return Result.ok("登录成功");
}
return Result.error("用户名或密码错误");
}
@ResponseBody //返回 JSONXML 等格式的数据
@RequestMapping("/BookList11")
public Result getStudentList(){
List<BookList> list = bookListService.list();
return Result.ok().put("data",list);
}
@ResponseBody
@RequestMapping("/addBookList")
public Result addBookList(BookList bookList){
boolean result = bookListService.save(bookList);
if(!result){
return Result.error("添加失败");
}
return Result.ok();
}
@ResponseBody
@RequestMapping("/updateBookList")
public Result updateBookList(BookList bookList){
boolean result = bookListService.saveOrUpdate(bookList);
if(!result){
return Result.error("修改失败");
}
return Result.ok();
}
@ResponseBody
@RequestMapping("/deleteBookList")
public Result deleteBookList(Integer id){
boolean result = bookListService.removeById(id);
if(!result){
return Result.error("删除失败");
}
return Result.ok();
}
}

View File

@ -0,0 +1,12 @@
package com.c202201020140.tasks.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.c202201020140.tasks.model.BookList;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BookListMapper extends BaseMapper<BookList> {
}

View File

@ -0,0 +1,26 @@
package com.c202201020140.tasks.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
@Getter
@Setter
public class BookList implements Serializable{
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String title;
private String author;
private String isbn;
private String publisher;
private String published_date;
}

View File

@ -0,0 +1,63 @@
package com.c202201020140.tasks.model;
import java.util.HashMap;
import java.util.Map;
/**
* 返回数据封装类
*/
public class Result extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
public Result() {
put("code", 0);
put("msg", "success");
}
public static Result error() {
return error(500, "未知异常,请联系管理员");
}
public static Result error(String msg) {
return error(500, msg);
}
public static Result error(int code, String msg) {
Result r = new Result();
r.put("code", code);
r.put("msg", msg);
return r;
}
public static Result ok(String msg) {
Result r = new Result();
r.put("msg", msg);
return r;
}
public static Result ok(Object obj) {
Result r = new Result();
r.put("data", obj);
return r;
}
public static Result ok(Map<String, Object> map) {
Result r = new Result();
r.putAll(map);
return r;
}
public static Result ok() {
return new Result();
}
public Result put(String key, Object value) {
super.put(key, value);
return this;
}
}

View File

@ -0,0 +1,11 @@
package com.c202201020140.tasks.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.c202201020140.tasks.model.BookList;
public interface IBookListService extends IService<BookList> {
}

View File

@ -0,0 +1,14 @@
package com.c202201020140.tasks.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c202201020140.tasks.dao.BookListMapper;
import com.c202201020140.tasks.model.BookList;
import com.c202201020140.tasks.service.IBookListService;
import org.springframework.stereotype.Service;
@Service
public class BookListServiceImpl extends ServiceImpl<BookListMapper, BookList> implements IBookListService {
}

View File

@ -9,8 +9,8 @@ spring.datasource.username=202201020140
spring.datasource.password=@hnucm1254
#??????????MyBatis??
#??Mybatis?Mapper??
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis-plus.mapper-locations=classpath:mapper/*.xml
#??Mybatis?????
mybatis.type-aliases-package=com.c202201020140.tasks.model
mybatis-plus.type-aliases-package=com.c202201020140.tasks.model
#????
logging.level.com.yilinxuan.springboot.springboot = debug

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.c202201020140.tasks.dao.BookListMapper">
</mapper>