MyBatis 读取MySQL 数据

目录

1.确定需求

2.新建项目的子模块

3.MySQL数据库配置

4.编写代码

5.编译代码、构建代码

7.测试SpringBoot后端接口

1.确定需求

        前端访问后端URL,显示后端从数据库中获取的信息。

        输入:前端访问为:http://localhost:8080/hello

        输出:前端显示后端从数据库中获得的数据。

        Postman中显示的结果如下:

        

2.新建项目的子模块

        首先新建项目,如下

 

         完成了项目新建以后,首先要做的是,在新建的项目里面,再新建一个模块mybatis

 

        需要注意的是 ,新模块的依赖选择有所不同,需要以下三个依赖同时勾选

         然后,将新模块mybatis 添加为Maven 项目, 等待Maven 解析依赖完成,不过有些版本的idea可以自动添加,我也只是有所耳闻,没见识过。

3.MySQL数据库配置

        完成了新建项目以后,对数据库操作一番,此处利用Navicat来操作数据库,首先在Navicat中新建一个数据库

        

   

         如此,数据库就建立完成,在新建的数据库中,添加一个表

        新建表完成后,对表的字段进行编辑,同时设置主键。

         还有一点值得在意,最好将id设置为

         字段的名字与类型,需要和程序中对应。

        用下述方法在表格中添加数据

        嗯,这样就行

4.编写代码

        在新建的模块下,添加一系列包

        同时也添加一个资源目录

        新建controller、service、mapper、domain 对应的Java 文件     

        新建mapper 资源目录Java mapper 对应的XML 文件

        将上述文件添加到对应位置

代码如下:

        controller文件中代码为:

package com.example.mybatis.controller;

import com.example.mybatis.domain.User;
import com.example.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HelloController {
    @Autowired
    private UserService userService;

    @GetMapping("/hello")
    public List<User> hello()
    {
        return userService.selectAllUser();
    }
}

        domain中的文件为:

package com.example.mybatis.domain;

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

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", createTime='" + createTime + '\'' +
                '}';
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

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

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String createTime;

}

        mapper中的代码为:

package com.example.mybatis.mapper;

import com.example.mybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

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

        service中的代码为:

package com.example.mybatis.service;

import com.example.mybatis.domain.User;
import com.example.mybatis.mapper.UserMapper;
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> selectAllUser()
    {
        return userMapper.selectAllUser();
    }
}

        资源目录中的xml文件代码为:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybaits?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
    username: 
    password: 

mybatis:
  typeAliasesPackage: com.example.mybatis.domain
  mapperLocations: classpath:mapper/*.xml

        需要注意一点,username以及password使用自己的用户名和密码。

5.编译代码、构建代码

        将完成的代码进行构建与编译,如下图就算成功

7.测试SpringBoot后端接口

        建议使用postman测试接口,当get出了下图结果就算成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值