欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > spring-boot 整合 mybatis

spring-boot 整合 mybatis

2025/3/16 23:31:07 来源:https://blog.csdn.net/m0_72168501/article/details/142629567  浏览:    关键词:spring-boot 整合 mybatis

文章目录

  • Spring boot 整合Mybatis将数据返回到浏览器
    • 1. 准备数据
    • 2. 导入依赖
    • 3. 配置数据库连接
    • 4. 创建一个 pojo 包,创建User实体类
    • 5. 创建一个mapper包,写一个UserMapper接口
    • 6. 创建一个service包,写一个UserService接口。
    • 7. 在 Service 包下创建一个子包,impl 包,然后 implements UserService 接口,同时将 UserService 接口和 userMapper 类的抽象方法 findById 实现。
    • 8. 创建 controller 包
    • 9. 查看结果

Spring boot 整合Mybatis将数据返回到浏览器

1. 准备数据

将数据放在本地的mysql数据库中

create database if not exists mybatis;use mybatis;create table user(id int unsigned primary key auto_increment comment 'ID',name varchar(100) comment '姓名',age varchar(100) comment '年龄',gender varchar(100) unsigned comment '性别, 1:男, 2:女',phone varchar(11) comment '手机号'
) comment '用户表';insert into user(id, name, age, gender, phone) VALUES (null,'白眉鹰王',55,'1','18800000000');
insert into user(id, name, age, gender, phone) VALUES (null,'金毛狮王',45,'1','18800000001');
insert into user(id, name, age, gender, phone) VALUES (null,'青翼蝠王',38,'1','18800000002');
insert into user(id, name, age, gender, phone) VALUES (null,'紫衫龙王',42,'2','18800000003');
insert into user(id, name, age, gender, phone) VALUES (null,'光明左使',37,'1','18800000004');
insert into user(id, name, age, gender, phone) VALUES (null,'光明右使',48,'1','18800000005');

在这里插入图片描述

2. 导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--boot 工程的父工程,用于管理起步依赖的版本--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.3.4</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.ithiema</groupId><artifactId>springboot-demo2</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-demo2</name><description>springboot-demo2</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version></properties><dependencies><!--mysql驱动依赖--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.0.33</version></dependency><!--        &lt;!&ndash;MYSQL 驱动&ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>mysql</groupId>-->
<!--            <artifactId>mysql-connector-java</artifactId>-->
<!--            <version>8.0.26</version>-->
<!--        </dependency>--><!--mybatis 的起步依赖--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version></dependency><!--web起步依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

3. 配置数据库连接

application.properties 配置文件里改

spring.application.name=springboot-demo2
spring:datasource:spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/mybatisspring.datasource.username=rootspring.datasource.password=p@ssw0rd

在这里插入图片描述

4. 创建一个 pojo 包,创建User实体类

里面四个属性,id,姓名,年龄,性别,手机号 跟数据库表里的字段一一对应。

package com.ithiema.springbootdemo2.pojo;public class User {private Integer id;private String name;private String age;private String gender;private String phone;public User() {}public User(Integer id, String name, String age, String gender, String phone) {this.id = id;this.name = name;this.age = age;this.gender = gender;this.phone = phone;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", gender=" + gender +", phone='" + phone + '\'' +'}';}
}

5. 创建一个mapper包,写一个UserMapper接口

里面定义一个 findByid这个方法,传入一个 id 查询之后返回一个User对象。

package com.ithiema.springbootdemo2.mapper;import com.ithiema.springbootdemo2.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;/*** ClassName: UserMapper* Packge: com.ithiema.springbootdemo2.mapper* Description:** @Author: aex* @Create 2024/9/29 10:32* @Version 1.0*/
@Mapper
public interface UserMapper {@Select("select * from mybatis.user where id = #{id}")public User findById(Integer id);
}

6. 创建一个service包,写一个UserService接口。

跟刚刚Mapper 包里一样的方法。

package com.ithiema.springbootdemo2.service;import com.ithiema.springbootdemo2.pojo.User;/*** ClassName: UserService* Packge: com.ithiema.springbootdemo2.service* Description:** @Author: aex* @Create 2024/9/29 10:36* @Version 1.0*/
public interface UserService{public User findById(Integer id);
}

7. 在 Service 包下创建一个子包,impl 包,然后 implements UserService 接口,同时将 UserService 接口和 userMapper 类的抽象方法 findById 实现。

package com.ithiema.springbootdemo2.service.impl;import com.ithiema.springbootdemo2.mapper.UserMapper;
import com.ithiema.springbootdemo2.pojo.User;
import com.ithiema.springbootdemo2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** ClassName: UserServiceImpl* Packge: com.ithiema.springbootdemo2.service.impl* Description:** @Author: aex* @Create 2024/9/29 10:40* @Version 1.0*/
@Service
public class UserServiceImpl implements UserService {  // 这个类是service 的容器,@Autowiredprivate UserMapper userMapper;@Overridepublic User findById(Integer id) {return userMapper.findById(id);}
}

8. 创建 controller 包

创建 UserControllerpackage com.ithiema.springbootdemo2.controller;import com.ithiema.springbootdemo2.pojo.User;
import com.ithiema.springbootdemo2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** ClassName: UserController* Packge: com.ithiema.springbootdemo2.controller* Description:** @Author: aex* @Create 2024/9/29 10:52* @Version 1.0*/
@RestController
public class UserController {@Autowired //注入属性private UserService userService;@RequestMapping("/findById")public User findById(Integer id){return userService.findById(id);}}

9. 查看结果

在浏览器输入我们的本地的这个网址,将刚刚在UserController 类里 @RequestMapping("/findById") 设置的路径传进去

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词