MyBatis 读取MySQL 数据简单示例

已有 SpringBoot 程序能够响应前端浏览器HTTP

请求 SQL 能够读取MySQL 数据库数据

缺少 SQL 读取出来的数据转为SpringBoot 程序能够使用的Java 类对象

MyBatis 读取MySQL 数据库数据,将数据转为Java 类对象,供SpringBoot 程序使用

1.确定需求

前端访问后端URL,显示后端从数据库读取的数据

  输入:前端访问URLhttp://localhost:8080/helloFromDB

  输出:前端显示后端从数据库读取的数据


postman中的显示

Mysql数据库表

 

 

2.新建项目的子模块

这边正常next

 web中勾选Spring Web

SQL中勾选MyBatis Framework和MySQL Driver

create

 

3.MySQL 数据库配置、添加数据

打开NaviCat新建数据库编写表单

 

4.编写代码 编译代码、构建程序

1.新建controllerservicemapperdomain

 

2. 新建mapper 资源目录

 

3. 新建controllerservicemapperdomain 对应的Java 文件

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();
    }
}

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();
    }
}

Mapper:

package com.example.mybatis.mapper;

import com.example.mybatis.domain.User;

import java.util.List;

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

Domain:

package com.example.mybatis.domain;

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

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

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

    public int getId() {
        return id;
    }

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

    public int getSex() {
        return sex;
    }

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

    public String getCreateTime() {
        return createTime;
    }

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

 4.新建mapper 资源目录Java mapper 对应的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.example.mybatis.mapper.UserMapper">
    <resultMap id="UserResult" type="User">
        <id     property="id"   column="id"     />
        <result property="name" column="name"   />
        <result property="age"  column="age"   />
        <result property="sex"  column="sex"   />
        <result property="createTime"  column="create_time"   />
    </resultMap>

    <select id="selectAllUser" resultMap="UserResult">
        select * from user
    </select>

</mapper>

5. 配置数据库连接和MyBatis

新建yml文件

  

 6.添加必要的注解

7.run就完了

5.测试SpringBoot 后端接口

测试就和上面的效果一样了~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值