OA项目第一天

发布时间:2024年01月11日

1.在ldea中先创建一个maven项目,虽然这是一个springboot项目,但是对于项目的搭建还是建议使用maven创建,然后添加springboot依赖

项目的结构为

在这里插入图片描述

parent项目中添加依赖

<groupId>com.zssj</groupId>
<artifactId>parent</artifactId>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.6.RELEASE</version>
</parent>

添加版本锁定和插件

<properties>
    <java.version>12</java.version>
    <mybatis-plus.version>3.4.1</mybatis-plus.version>
    <mysql.version>8.0.30</mysql.version>
    <knife4j.version>3.0.3</knife4j.version>
    <jwt.version>0.9.1</jwt.version>
    <fastjson.version>2.0.21</fastjson.version>
</properties>

<!--配置dependencyManagement锁定依赖的版本-->
<dependencyManagement>
    <dependencies>
        <!--mybatis-plus 持久层-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!--knife4j-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>${knife4j.version}</version>
        </dependency>
        <!--jjwt-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>${jwt.version}</version>
        </dependency>
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

对于common-util中添加的依赖为

 <packaging>jar</packaging>
 
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
    </dependencies>

对于service-util项目中添加的依赖

<packaging>jar</packaging>
<dependencies>
     <dependency>
            <groupId>com.zssj</groupId>
            <artifactId>common-util</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
        <version>${knife4j.version}</version>
    </dependency>
    <!--mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

对于model项目中添加的依赖为

<dependencies>
    <!--lombok用来简化实体类-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
        <scope>provided </scope>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <scope>provided </scope>
    </dependency>
</dependencies>

对于service项目中添加的依赖为

<packaging>jar</packaging>

<dependencies>
    <dependency>
        <groupId>com.zssj</groupId>
        <artifactId>model</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.zssj</groupId>
        <artifactId>service-util</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2.开始写代码,先在service中创建包和springboot启动类

package com.zssj.auth;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan ("com.zssj.*.mapper")
public class ServiceAuthApplication {

 public static void main(String[] args) {
        SpringApplication.run(ServiceAuthApplication.class,args);
    }
}

增加mapper包扫描,

然后将给的数据库文件加载,将给定的数据库实体类和前端传参的接收参数类复制进去model层中,以供调用

创建spring配置文件和spring开发环境的配置文件分别为

spring:
  application:
    name: service
  profiles:
    active: dev
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

在spring-dev配置文件中进行数据源配置和环境配置

server:
  port: 8800
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 查看日志
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/oa?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8
    username: root
    password: 123456

写service中的service层和mapper层

SysRoleService

import com.baomidou.mybatisplus.extension.service.IService;
import com.zssj.model.system.SysRole;

public interface SysRoleService extends IService<SysRole> {
}

实现类

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zssj.auth.mapper.SysRoleMapper;
import com.zssj.auth.service.SysRoleService;
import com.zssj.model.system.SysRole;
import org.springframework.stereotype.Service;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper,SysRole> implements SysRoleService  {

}

mapper层

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zssj.model.system.SysRole;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface SysRoleMapper extends BaseMapper<SysRole> {
}

写完大概的目录结构为

在这里插入图片描述

然后现在test目录进行测试

测试时有一个小问题,如果使用的时springboot快速创建那么他会生成测试类,如果使用的是常规maven项目,需要把类似上面这个要测试的springboot程序目录结构来创建测试类

在这里插入图片描述

先测试mybatisplus内置的简单mapper层能不能用,写测试类试一下

import com.zssj.auth.mapper.SysRoleMapper;
import com.zssj.model.system.SysRole;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
@MapperScan("com.zssj.mapper")

public class test {

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Test
    public void test(){
        List<SysRole> sysRoles = sysRoleMapper.selectList(null);

    }
}

看控制台输出内容

在这里插入图片描述

如果出现这个内容大概就没有什么问题,

3.开始写第一个controller层

import com.zssj.auth.service.SysRoleService;
import com.zssj.model.system.SysRole;
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;

@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;


    @GetMapping( "/findAll")
    public List<SysRole> findAll (){
      return   sysRoleService.list();
    }

}

运行底层的springboot运行类。然后在浏览器访问http://localhost:8800//admin/system/sysRole/findAll

如果浏览器显示json数据

在这里插入图片描述

那么首先这个springboot后端程序就已经构建成功,并且可以运行,后续在接下来几天。

文章来源:https://blog.csdn.net/qq_64258695/article/details/135510998
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。