SpringBoot数据访问

本文阅读 1 分钟
首页 代码,Java 正文

与springmvc整合myabtis的步骤基本一致,主要分为以下几步

导入pom文件

主要是导入对应的mysql驱动以及对应的mybaits启动器

<!-- properties标签可以自定义版本-->
    <properties>
        <java.version>1.8</java.version>
        <repackage.classifier/>
        <spring-native.version>0.10.4</spring-native.version>
<!-- 我自己的mysql是8.0.22-->
        <mysql.version>8.0.22</mysql.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
<!-- <dependency>-->
<!-- <groupId>org.xerial</groupId>-->
<!-- <artifactId>sqlite-jdbc</artifactId>-->
<!-- </dependency>-->
        <dependency>
            <groupId>org.springframework.experimental</groupId>
            <artifactId>spring-native</artifactId>
            <version>${spring-native.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
<!-- 导入mybatis启动器场景-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

编写mybatis的配置类

在resource文件夹下,新建一个文件夹mybatis,用来存放mapper.xml以及mybatis的主要配置类

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

</configuration>

UserMapper.xml

<?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.spring.mapper.UserMapper">
    <select id="getUser" resultType="com.spring.pojo.User">
        select * from user
    </select>
</mapper>

项目目录如下

img

springboot配置

配置myabtis的主配置类位置

类路径下mybatis文件下的mybatis-config.xml文件

mybatis.config-location=classpath:mybatis/mybatis-config.xml

配置mapper位置

mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

配置数据源

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456

新建mapper

package com.spring.mapper;

import com.spring.pojo.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;


@Mapper
public interface UserMapper { 
    public List<User> getUser();
}

查询数据

新建一个controller和service

访问 /getuser 时查询数据库中所有的user

package com.spring.controller;


import com.spring.pojo.User;
import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MyController { 
    @Autowired
    UserService userService;

    @RequestMapping("/getuser")
    public List<User> getU(){ 
        List<User> user = userService.getUser();
        return user;
    }
}

UserService

package com.spring.service;

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

import java.util.List;

@Service
public class UserService { 

    @Autowired
    UserMapper userMapper;

    public List<User> getUser()
    { 
        List<User> user = userMapper.getUser();
        return user;
    }
}
本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/m0_46435741/article/details/120656600
-- 展开阅读全文 --
Redis底层数据结构--跳跃表
« 上一篇 04-28
BUUCTF Web [强网杯 2019]随便注
下一篇 » 06-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复