文章目录
- Spring boot 整合Mybatis将数据返回到浏览器
- 1、准备数据
- 2. 创建一个 pojo 包,创建User实体类
- 3. 创建一个mapper包,写一个UserMapper接口
- 4. 创建一个service包,写一个UserService接口。
- 5. 在 Service 包下创建一个子包,impl 包,然后 implements UserService 接口,同时将 UserService 接口和 userMapper 类的抽象方法 findById 实现。
- 6. 创建 controller 包
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 tinyint unsigned comment '年龄',gender tinyint 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. 创建一个 pojo 包,创建User实体类
里面四个属性,id,姓名,年龄,性别,手机号 跟数据库表里的字段一一对应。
package com.ithiema.springbootdemo2.pojo;public class User {private Integer id;private String name;private Short age;private Short gender;private String phone;public User() {}public User(Integer id, String name, Short age, Short 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 Short getAge() {return age;}public void setAge(Short age) {this.age = age;}public Short getGender() {return gender;}public void setGender(Short 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 + '\'' +'}';}
}
3. 创建一个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);
}
4. 创建一个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);
}
5. 在 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);}
}
6. 创建 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);}}