【项目demo01】项目初始化

我准备开一个系列,就是写一些在简要的学习项目中可能会用到的奇奇怪怪的功能,比如线程池或者统一异常处理类
取名为【项目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>
  1. 进行一个数据库的连

    # 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
    
  2. 并添加些许初始数据
    在这里插入图片描述

  3. 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();
        }
    }
    
  4. 进行一个测试,访问http://localhost:8888/user
    在这里插入图片描述


我的项目demo系列都会同步到GitHub上,欢迎围观

https://github.com/Livorth/FunctionalLearning

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用maven初始化一个springboot项目,需要遵循以下步骤: 1. 安装maven,确保已经安装好了maven。 2. 在命令行中输入以下命令初始化一个maven项目: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 这个命令将在当前目录下创建一个名为“demo”的项目,其中“com.example”是项目的包名。 3. 在项目的根目录下创建一个名为“src/main/java”的目录,然后在这个目录下创建一个名为“com/example/demo”的目录,这个目录将包含我们的Java代码。 4. 在“com/example/demo”目录下创建一个名为“DemoApplication.java”的文件,这个文件将包含我们的Spring Boot应用程序的入口点。 示例代码: ``` package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 5. 在“src/main/resources”目录下创建一个名为“application.properties”的文件,这个文件将包含我们的应用程序的配置。 示例代码: ``` server.port=8080 ``` 这个配置文件指定了我们应用程序的端口号为8080。 6. 编译并运行应用程序,使用以下命令: ``` mvn spring-boot:run ``` 这个命令将编译并运行我们的应用程序,启动一个内嵌的Tomcat服务器,可以在浏览器中访问“http://localhost:8080”查看应用程序的欢迎页面。 到此,一个简单的Spring Boot项目就创建好了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值