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):

相关推荐
AIgorithmGEEK10 小时前
MySQL 索引全解(下):建、查、删实战与知识速查表
数据库·mysql·b+树·索引
Lost_the_wind10 小时前
MySQL学习笔记四
mysql
paopaokaka_luck14 小时前
滑雪场管理系统(AI智能助手流式对话,ECharts 运营数据分析、教练、雪具与场地一体化预约,钱包支付退款、订单核销与服务评价)
java·人工智能·spring boot·网络协议·数据分析·echarts
SL_staff14 小时前
JVS混合架构实践:能力引擎+动态模型如何支撑企业级可持续交付
spring boot·低代码·全栈
迪飞特科技14 小时前
智慧物业平台实战:Spring Boot 整合小程序实现报修巡检费用全流程
spring boot·后端·小程序
考虑考虑14 小时前
@JsonInclude注解使用
spring boot·后端·spring
geovindu14 小时前
sql:Search-Optimized SQL Queries using mysql 9.0
android·数据库·sql·mysql·数据库开发·数据库架构
for_ever_love__14 小时前
MySQL SELECT 与 WHERE 优化实战:可搜索条件、联合索引与执行计划
数据库·mysql·oracle·select·where·条件优化·查询优化技巧
2501_9318037515 小时前
MySQL 锁机制详解
数据库·mysql
.Hypocritical.17 小时前
MySQL |常用 SQL 语句与 pymysql 函数
mysql