Spring Boot入门之获取MySQL数据

Posted Mr.zhou_Zxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot入门之获取MySQL数据相关的知识,希望对你有一定的参考价值。

Spring Boot入门之获取mysql数据

项目整体结构

需要导入依赖

配置类文件

application.yml

server:
  port: 8081
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/zxy?characterEncoding=utf-8&userSSL=false&serverTimezone=UTC
    username: root
    password: root
mybatis:
  mapper-locations: classpath:mapper/*.xml

Pojo层

  • User
package com.zxy.webcollectdata.pojo;

public class User {
    private int id;
    private String name;
    private String sex;

    public User() {
    }

    public User(int id, String name, String sex) {
        this.id = id;
        this.name = name;
        this.sex = sex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

Controller层

  • UserController
package com.zxy.webcollectdata.controller;

import com.zxy.webcollectdata.pojo.User;
import com.zxy.webcollectdata.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 UserController {

    @Autowired
    private UserService userService;
	//测试
    @RequestMapping("/user")
    public User getName(){
        return new User(1,"zxy","man");
    }
	// 查询指定数据库的表中的所有数据
    @RequestMapping("/findAll")
    public List<User> FindAll(){
        return userService.findAll();
    }
}

Service层

  • UserService
package com.zxy.webcollectdata.service;

import com.zxy.webcollectdata.mapper.UserMapper;
import com.zxy.webcollectdata.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> findAll(){
        return userMapper.findAll();
    }
}

Mapper层

  • UserMapper
package com.zxy.webcollectdata.mapper;

import com.zxy.webcollectdata.pojo.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
    List<User> findAll();
}
  • 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">
<!--
    从现在开始不能随便
    namespace : mapper配置文件对应的mapper接口的包名.类名
    id : mapper接口中对应的方法名称
-->
<mapper namespace="com.zxy.webcollectdata.mapper.UserMapper">
    <!-- 映射配置文件用于专门写sql,查询所有的员工信息 -->
    <select id="findAll" resultType="com.zxy.webcollectdata.pojo.User">
        select * from user
    </select>

</mapper>

以上是关于Spring Boot入门之获取MySQL数据的主要内容,如果未能解决你的问题,请参考以下文章

spring boot之入门配置

Spring Boot入门使用MySQL数据库

Spring Boot 2从入门到入坟 | 请求参数处理篇:常用参数注解之@CookieValue

Spring Boot 2从入门到入坟 | 请求参数处理篇:常用参数注解之@RequestBody

Spring Boot 2从入门到入坟 | 请求参数处理篇:常用参数注解之@RequestBody

Spring Boot入门系列七(SpringBoot 使用JDBC连接Mysql数据库)