学习进度条
今日所花时间:一小时
今日代码量:100行
博客量:两篇
了解到的知识点: 今天主要是进行AI训练 学习如何使用自然语言进行开发 熟悉springboot的书写框架和模式 继续完成项目练习
项目配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>day0225</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>day0225</name>
<description>day0225</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- volidation依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.4.0</version>
</dependency>
<!-- 单元测试的坐标-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
spring.application.name=day0225
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/big_event
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.mapper-locations=classpath:mybatis/mappers/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
#??????????????
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.web.resources.add-mappings=false
logging.level.com.example.elderEvaluate.mapper=debug
spring.session.jdbc.initialize-schema=always
统一结果封装类
package com.example.day0225.pojo;
/**
* 统一响应结果封装类
*/
public class Result<T> {
private Integer code; // 0失败,1成功
private String message; // 提示错误信息
private T data; // 数据
// 方便创建Result对象
// 创建一个表示成功的Result对象,并传入具体数据
public static <T> Result<T> success(T data) {
return new Result<>(1, "success", data);
}
// 创建一个表示成功但没有具体数据的Result对象
public static <T> Result<T> success() {
return new Result<>(1, "success", null);
}
// 创建一个表示失败的Result对象,并传入错误提示信息
public static <T> Result<T> error(String message) {
return new Result<>(0, "操作失败" + message , null);
}
public Result(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
// 重写Object类的toString()方法
// 以自定义的格式返回Result对象的字符串表示
// 方便在调试或输出时查看对象的内容
@Override
public String toString() {
return "Result [code=" + code + ", message=" + message + ", data=" + data + "]";
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
文章类别代码
控制层代码
package com.example.day0225.controller;
import com.example.day0225.pojo.Category;
import com.example.day0225.pojo.Result;
import com.example.day0225.service.CategoryService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.rmi.server.RemoteRef;
import java.util.List;
@RestController
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@PostMapping()
public Result<String> add(@RequestBody @Validated(Category.Add.class) Category category) {
categoryService.add(category);
return Result.success();
}
@GetMapping()
public Result<List<Category>> list() {
List<Category> cs = categoryService.list();
return Result.success(cs);
}
@GetMapping("/detail")
public Result<Category> detail(Integer id) {
Category c = categoryService.findById(id);
return Result.success(c);
}
@PutMapping()
public Result<String> update(@RequestBody @Validated(Category.Update.class) Category category) {
categoryService.update(category);
return Result.success();
}
}
service层代码
package com.example.day0225.service;
import com.example.day0225.pojo.Category;
import java.util.List;
public interface CategoryService {
//新增分类
void add(Category category);
//列表查询
List<Category> list();
//根据id查询分类信息
Category findById(Integer id);
//更新分类
void update(Category category);
}
实现类
package com.example.day0225.service.impl;
import com.example.day0225.mapper.CategoryMapper;
import com.example.day0225.pojo.Category;
import com.example.day0225.service.CategoryService;
import com.example.day0225.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryMapper categoryMapper;
@Override
public void add(Category category) {
//补充属性值
category.setCreateTime(LocalDateTime.now());
category.setUpdateTime(LocalDateTime.now());
Map<String,Object> map = ThreadLocalUtil.get();
Integer userId = (Integer) map.get("id");
category.setCreateUser(userId);
categoryMapper.add(category);
}
@Override
public List<Category> list() {
Map<String,Object>map = ThreadLocalUtil.get();
Integer userId = (Integer) map.get("id");
return categoryMapper.list(userId);
}
@Override
public Category findById(Integer id) {
Category c = categoryMapper.findById(id);
return c;
}
@Override
public void update(Category category) {
category.setUpdateTime(LocalDateTime.now());
categoryMapper.update(category);
}
}
Mapper层代码
package com.example.day0225.mapper;
import com.example.day0225.pojo.Category;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;
import java.util.List;
@Mapper
public interface CategoryMapper {
//新增
@Insert("insert into category (category_name,category_alias,create_user,create_time,update_time)" +
"values(#{categoryName},#{categoryAlias},#{createUser},#{createTime},#{updateTime})")
void add(Category category);
//查询所有
@Select("select * from category where create_User = #{userId}")
List<Category> list(Integer userId);
//根据id查询
@Select("select * from category where id = #{id}")
Category findById(Integer id);
//更新
@Update("update category set category_name =#{categoryName},category_alias = #{categoryAlias},update_time = #{updateTime} where id = #{id}")
void update(Category category);
}

浙公网安备 33010602011771号