spring boot 项目连接数据库查询数据过程

本文介绍如何在SpringBoot项目中集成MyBatis,并通过具体步骤实现数据库操作。涵盖pom.xml配置、application.yml设置、mapper.xml定义、Service及Controller层开发等内容。
摘要由CSDN通过智能技术生成

 首先你要有一个spring boot的web项目,只是项目的目录

 一、配置pom.xml文件

<?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 http://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.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.ts</groupId>
    <artifactId>industry</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--   jdbc 的依赖     -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--   spring boot 的启动依赖     -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--   mybatis 的依赖     -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <!--    mysql的依赖    -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <!--这个插件,可以将应用进行打包成一个可执行的jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <!-- maven项目中src源代码下的xml等资源文件编译进classes文件夹,
              注意:如果没有这个,它会自动搜索resources下是否有mapper.xml文件,
              如果没有就会报org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.pet.mapper.PetMapper.selectByPrimaryKey-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>

            <!--将resources目录下的配置文件编译进classes文件  -->
<!--            <resource>-->
<!--                <directory>src/main/resources</directory>-->
<!--            </resource>-->
        </resources>
    </build>
    
</project>

二、配置application文件

这里我的格式为yml ,application.yml

其中 URL 后面跟换成你自己的地址,还有 serverTimezone=GMT 这个一定要配置

不然会报错:

java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.

还有 driverClassName 后面 有点是 com.mysql.jdbc.Driver 这要看你使用的版本。

报错:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. 

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/industry?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8&useSSL=false 
#你学习mybatis时,使用的数据库地址
    driverClassName: com.mysql.cj.jdbc.Driver
    username: root
    password: root
mybatis:
  type-aliases-package: com.ts.pojo
  mapper-locations: com/ts/mapper/*.xml

三、配置mapper.xml 、mapper和有实体类

<?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.ts.mapper.UserMapper">

    <select id="test" resultType="com.ts.pojo.User">
        select * from s_user;
    </select>

</mapper>
package com.ts.mapper;

import com.ts.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component     //加上后再注入时不会爆红
public interface UserMapper {

    List<User> test();

}

实体类我就不写了。》》》》

四、service

package com.ts.serviceimpl;

import com.ts.mapper.UserMapper;
import com.ts.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;


    public List<User> test() {
        List<User> test = userMapper.test();
        return test;
    }

}

五、Controlle

package com.ts.controller;

import com.ts.pojo.User;
import com.ts.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 测试数据
     * @return
     */
    @GetMapping("test")
    @ResponseBody
    public List<User> test(){
        List<User> list = userService.test();
        return list;
    }
}

六、运行测试

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值