我准备开一个系列,就是写一些在简要的学习项目中可能会用到的奇奇怪怪的功能,比如线程池或者统一异常处理类
取名为【项目demo】系列
然后将会同步到GitHub中:https://github.com/Livorth/FunctionalLearning
项目初始化
首先简单的构建一个SpringBoot项目
然后构建基础的实验环境,即连接测试数据库
pom.xml要用什么的时候再加什么,但是首先要有数据库,同时要先简单的把MVC几个层写了
<?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>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.livorth</groupId>
<artifactId>FunctionalLearning</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>FunctionalLearning</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 在使用日志的时候会要用到-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Mybatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<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>
-
进行一个数据库的连
# server server.port= 8888 spring.application.name=FunctionalLearning # datasource spring.datasource.url=jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=GMT spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-
并添加些许初始数据
-
entity层、DAO层、service层、controller层
User.java
package cn.livorth.functionallearning.entity; import lombok.Data; /** * @program: FunctionalLearning * @description: 用户 * @author: livorth * @create: 2021-10-02 13:15 **/ @Data public class User { private int userId; private String userName; private String password; }
UserMapper.java,一般操作交给了MybatisPlus,当出现特殊需求的时候再去写Mapper.xml
UserServicepackage cn.livorth.functionallearning.dao; import cn.livorth.functionallearning.entity.User; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; /** * @program: FunctionalLearning * @description: 用户Mapper * @author: livorth * @create: 2021-10-02 13:21 **/ @Mapper public interface UserMapper extends BaseMapper<User> { }
UserService.java
package cn.livorth.functionallearning.service; import cn.livorth.functionallearning.entity.User; import java.util.List; /** * @program: FunctionalLearning * @description: 用户服务 * @author: livorth * @create: 2021-10-02 13:21 **/ public interface UserService { /** * 返回全部用户的信息 * @return */ List<User> getAllUser(); }
UserServiceImpl.java
package cn.livorth.functionallearning.service.impl; import cn.livorth.functionallearning.dao.UserMapper; import cn.livorth.functionallearning.entity.User; import cn.livorth.functionallearning.service.UserService; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @program: FunctionalLearning * @description: 用户服务实现 * @author: livorth * @create: 2021-10-02 13:25 **/ @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> getAllUser() { LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>(); return userMapper.selectList(queryWrapper); } }
UserController.java
package cn.livorth.functionallearning.controller; import cn.livorth.functionallearning.entity.User; import cn.livorth.functionallearning.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @program: FunctionalLearning * @description: 用户Controller * @author: livorth * @create: 2021-10-02 13:27 **/ @RestController @RequestMapping("user") public class UserController { @Autowired private UserService userService; @GetMapping public List<User> getAllUser(){ return userService.getAllUser(); } }
-
进行一个测试,访问http://localhost:8888/user
我的项目demo系列都会同步到GitHub上,欢迎围观
https://github.com/Livorth/FunctionalLearning