零基础搭建spring boot项目

该文详细介绍了如何从零开始构建一个基于SpringBoot的应用,包括引入MyBatis作为ORM框架,配置MySQL数据库,使用Druid数据源,以及创建DAO、Mapper、Service、Controller层来实现用户数据的查询操作。
摘要由CSDN通过智能技术生成
                  *零基础搭建spring boot项目---查看*

导入依赖包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</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>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.4</version>
        </dependency>-->

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.1.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <!--可以把依赖的包都打包到生成的Jar包中-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
架构图
![在这里插入图片描述](https://img-blog.csdnimg.cn/7a1b3b3f3a38493685a6a64cd4498c82.png#pic_center)

dao包中User类

package com.example.zct.dao;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String name;
    private  Integer age;
}

mapper包中UserDaoMapper接口

package com.example.zct.mapper;

import com.example.zct.dao.User;
import org.apache.ibatis.annotations.Select;

import java.util.List;

//@Mapper
public interface UserDaoMapper {
    /**
     * 查询所有
     *
     * @param
     * @return List
     */
    @Select("select *from  user ")
    List<User> QueryAll();

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @Select("select  * from user where id=#{id}")
    User getById(Integer id);
}

service包中的UserService接口

package com.example.zct.service;

import com.example.zct.dao.User;

import java.util.List;

public interface UserService {
    User getById(Integer id);

    List<User> QueryAll();
}

创建一个UserServiceImpl实现UserService接口

package com.example.zct.service.Impl;


import com.example.zct.dao.User;
import com.example.zct.mapper.UserDaoMapper;
import com.example.zct.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
  @Autowired
private UserDaoMapper userDaoMapper;

  @Override
    public User getById(Integer id) {
        return  userDaoMapper.getById(id);
    }

  @Override
  public List<User> QueryAll() {
    return userDaoMapper.QueryAll();
  }
}

controller包创建一个UserQueryController类

package com.example.zct.controller;

import com.example.zct.dao.User;
import com.example.zct.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserQueryController {
    @Autowired
    UserService userService;

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("all")
    public List<User> QueryAll() {
        return userService.QueryAll();
    }

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public User getById(@PathVariable Integer id) {
        return userService.getById(id);
    }
}

启动类

package com.example.zct;

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

@SpringBootApplication
@MapperScan("com.example.zct.mapper")
public class StaffedApplication {
    public static void main(String[] args) {
        SpringApplication.run(StaffedApplication.class, args);
    }
}

``
## rusouces下的application.yml文件
server:
  port: 8089
#druid连接池配置的起步依赖方式
spring:
  datasource:
    druid:
      url: jdbc:mysql:///boot_db?useSSL=false
      username: root
      password: 123456
mybatis:
  configuration:
    # 开启驼峰映射
    map-underscore-to-camel-case: true
    # 标准的日志输出
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

数据库表
在values中根据创建表的字段进行添加名字即可
![在这里插入图片描述](https://img-blog.csdnimg.cn/2d42f94ce3fb4b20bbfa12aa3badad5b.png#pic_center)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@山雨欲来风满楼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值