SpringBoot + MySQL + MyBatis 实操示例教学

一、准备工作

1.导入相关依赖

复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

2.配置文件application.yml

复制代码
server:
  port: 8088
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 1234

mybatis:
  mapper-locations: classpath:mapper/*xml
  type-aliases-package: com.example.domain.po

这里的mapper-locations设置的是mapper映射的xml文件位置:

如果不设置的话,调用数据库的数据就会报错:

二、示例代码

controller:

EmployeeController示例:

复制代码
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/getData")
    public List<Employee> getEmployeeList(){
        List<Employee> employeeList = employeeService.getData();
        System.out.println(employeeList);
       return employeeList;
    }

}

service:

IEmployeeServcie接口:

复制代码
public interface IEmployeeService {

    List<Employee> getData();
}

impl:

EmployeeServiceImpl实现IEmployeeServcie接口:

复制代码
@Service
public class EmplyeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public List<Employee> getData() {
        return employeeDao.getData();
    }
}

dao:

复制代码
@Mapper
public interface EmployeeDao {

    List<Employee> getData();
}

resources/mapper:

EmployeeMapper.xml:

复制代码
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.EmployeeDao">

    <resultMap id="employeeResultMap" type="com.example.domain.po.Employee">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <result property="salary" column="salary"/>
        <result property="entryTime" column="entry_time"/>
    </resultMap>

    <select id="getData" resultMap="employeeResultMap">
        select id,name,age,salary,entry_time
        from employee
    </select>

</mapper>

mapper:

namespace:对应的是dao中的EmployeeDao文件

select:

id :对应的的是EmployeeDao文件方法名

resultMap:是返回的结果是列表

type:是列表的类型数据,案例中列表的类型数据是Employee

下图有更完整的解读:

进行测试:

测试方式一(直接访问浏览器):

http://localhost:8088/employee/getData

测试方式二(使用测试工具Postman):

相关推荐
Mico181 小时前
MySQL 8.0.35 GTID主从复制常见管理操作
mysql
网安墨雨2 小时前
MySQL数据库 SQL语句详解
自动化测试·软件测试·数据库·python·sql·mysql
hexu_blog2 小时前
springboot3集成shardingsphere4.0 分表分库
java·spring boot·mybatis
乐观的Terry2 小时前
9、发布系统-Webhook自动发布
java·spring boot·spring·spring cloud·mybatis
muddjsv2 小时前
SQLite与MySQL/PostgreSQL本质区别:运行模型、并发、部署、权限全方位对比
mysql·postgresql·sqlite
PH = 72 小时前
SpringBoot使用自动装配编写Start包
java·spring boot·后端
Q渡劫3 小时前
MinIO 从下载到集成 Spring Boot 完整教程
spring boot·spring·maven
RuoyiOffice5 小时前
超级个体接私活必看:后端+前端+移动端三端一体企业管理系统怎么选(2026)
java·spring boot·vue·uniapp·全栈·企业管理·接私活
Mico185 小时前
MySQL 8.0.35 GTID + 延迟复制 + 过滤复制搭建
mysql·dba
wuqingshun3141595 小时前
springBoot是如何通过main方法启动web项目的?
前端·spring boot·后端