文章目录
- 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>
<!-- <!–MYSQL 驱动–>-->
<!-- <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.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.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;
}
@Override
public 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 的容器,
@Autowired
private UserMapper userMapper;
@Override
public User findById(Integer id) {
return userMapper.findById(id);
}
}
8. 创建 controller 包
创建 UserController 类
package 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")
设置的路径传进去