简单搭建SpringBoot连接数据库(测试通过)


1、首先我们建立一个SpringBoot项目(前提是环境和依赖地址等都已经搭建好) 2、然后建立我们的父工程,名字自己可以随便起,选择Java8,一直下一步就行 3、然后找到我们父工程的pom文件,里面加入web依赖

<modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example1</groupId>
    <artifactId>example1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>example1</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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--web 没有这个项目启动不了-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

4、然后我们右键项目,建立模块,改成Maven,然后点击下一步,写好模块名字,一直点击下一步即可 5、建立好以后我们首先在父工程的pom文件中加以下依赖创建启动类,创建配置文件,连接好数据库

5、1父工程依赖

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

5、2启动类,加pringBootApplication注解

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

5、3配置文件,如果复制到自己项目上启动有问题,建议手敲,然后启动项目即可

server:
  port: 9090

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test1?characterEncoding=utf-8&useSSL=false
    username: root
    password: 123

6、我们在父工程下建立一个实体类模块,首先我们在父工程下加MybatisPlus依赖和json依赖

<!--        mybatis-plus 持久层-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
        </dependency>

6、1当前项目格式,在实体类模块中,建立实体类,mapper,mapping,在user模块中引入dao的依赖,便于访问,dao的依赖地址,按自己创建的写

<dependencies>
        <dependency>
            <artifactId>dao</artifactId>
            <groupId>com.example1</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

6、2实体类模块,使用Data注解,在dao模块下加以下依赖

<dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
package com.parent.dao;
import lombok.Data;
import java.io.Serializable;
@Data
public class score implements Serializable {
          
   

    private Integer sid;
    private Integer studentId;
    private Integer courseId;
    private Integer number;
}

6、3mapper文件和mapping文件

package com.parent.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.parent.dao.score;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface scoreMapper extends BaseMapper<score> {
          
   
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.parent.mapper.scoreMapper">

</mapper>

6、4在user模块下建立controlelr,service,impl包,并且放入类 6、5controller

package com.parent.controller;

import com.parent.dao.score;
import com.parent.service.scoreService;
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("/com/parent")
public class userController {
          
   
    @Autowired
    private scoreService scoreService;

    @GetMapping("getAll")
    public List getAll(){
          
   
        List<score> list = scoreService.list(null);
        return list;
    }
}

6、6coreService

package com.parent.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.parent.dao.score;

public interface scoreService extends IService<score> {
          
   
}

6、7scoreServiceImpl

package com.parent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parent.dao.score;
import com.parent.mapper.scoreMapper;
import com.parent.service.scoreService;
import org.springframework.stereotype.Service;

@Service
public class scoreServiceImpl extends ServiceImpl<scoreMapper, score> implements scoreService {
          
   
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/database/290600.html

(0)
上一篇 2022年10月4日 18:53
下一篇 2022年10月4日 18:53

相关推荐

发表回复

登录后才能评论