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

相关推荐
万亿少女的梦1687 分钟前
基于Spring Boot的楚雄旅游景区门票售卖系统设计与实现
java·spring boot·mysql·vue·系统设计
折哥的程序人生 · 物流技术专研1 小时前
Java面试通关⑩:MyBatis核心源码全集
mybatis·校招·java面试·orm框架·源码解析·数据库交互·社招
talenteddriver1 小时前
MySQL的ABC联合索引
mysql
wear工程师1 小时前
可重复读能不能防幻读?MVCC 和 Next-Key Lock 到底谁在起作用
mysql·面试
AOwhisky2 小时前
Kubernetes(K8s)学习笔记(第十四期):集群存储与有状态应用(下篇):StatefulSet 有状态应用管理
redis·笔记·mysql·云原生·kubernetes·云计算·k8s
豆瓣鸡2 小时前
AccessKey 安全配置
java·spring boot
EntyIU2 小时前
CentOS-高可用部署手册-MySQL双主RedisNginx
linux·mysql·centos
漂亮的摩托2 小时前
如何编写一个SpringBoot项目告警推送的Starter
java·spring boot·后端
天丁o2 小时前
Spring Boot + uni-app 智慧考勤闭环 Demo:打卡记录、异常状态和日统计如何复用到企业系统
spring boot·uni-app·mybatis plus·企业管理系统·考勤系统
kuro-shiro2 小时前
SpringBoot 启动流程
java·spring boot·后端