一、新建项目
1.1 新建springboot项目
新建项目
选择SpringBoot,填写基本信息,主要是JDK版本和项目构建方式,此处以JDK17和Maven举例。
1.2 引入依赖
选择SpringBoot版本,勾选Lombok,Spring Web,MyBatis,MySQL依赖。
1.3 创建完成
二、基础配置
2.1 配置application.properties
配置启动端口,数据库信息,MyBatis的基本配置。
2.2 配置mapper接口的bean扫描
此处以@MapperScan注解扫描整个包的mapper接口,也可以在每个mapper接口上添加@Mapper注解。
三、功能演示
3.1 数据库准备
CREATE TABLE `sys_user`
(`id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id',`username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户名',`password` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码',`nick_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '昵称',`gender` int NULL DEFAULT NULL COMMENT '性别:1-男 2-女',`avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '头像',`remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',`del_flag` tinyint(1) NULL DEFAULT NULL COMMENT '逻辑删除',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户' ROW_FORMAT = Dynamic;
3.2 实体类
package com.qiangesoft.demo.entity;import lombok.Data;/*** 用户** @author qiangesoft* @since 2024-10-14*/
@Data
public class SysUser {/*** id*/private Long id;/*** 用户名*/private String username;/*** 密码*/private String password;/*** 昵称*/private String nickName;/*** 性别:1-男 2-女*/private Integer gender;/*** 头像*/private String avatar;/*** 备注*/private String remark;/*** 逻辑删除*/private Boolean delFlag;}
3.3 Mapper接口及xml
package com.qiangesoft.demo.mapper;import com.qiangesoft.demo.entity.SysUser;/*** 用户** @author qiangesoft* @since 2024-10-14*/
public interface SysUserMapper {/*** 通过主键查询用户* @param id* @return*/SysUser selectByPrimaryKey(Long id);}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qiangesoft.demo.mapper.SysUserMapper"><resultMap id="resultMap" type="com.qiangesoft.demo.entity.SysUser"><result column="id" property="id"/><result column="username" property="username"/><result column="password" property="password"/><result column="nick_name" property="nickName"/><result column="gender" property="gender"/><result column="avatar" property="avatar"/><result column="remark" property="remark"/><result column="del_flag" property="delFlag"/></resultMap><select id="selectByPrimaryKey" resultMap="resultMap">select id,username,password,nick_name,gender,avatar,remark,del_flagfrom sys_userwhere id = #{id}</select></mapper>
3.4 Service接口及实现
package com.qiangesoft.demo.service;import com.qiangesoft.demo.entity.SysUser;/*** 用户** @author qiangesoft* @since 2024-10-14*/
public interface SysUserService {/*** 通过id查询用户* @param id* @return*/SysUser getSysUserById(Long id);}
package com.qiangesoft.demo.service.impl;import com.qiangesoft.demo.entity.SysUser;
import com.qiangesoft.demo.mapper.SysUserMapper;
import com.qiangesoft.demo.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** 用户** @author qiangesoft* @since 2024-10-14*/
@Service
public class SysUserServiceImpl implements SysUserService {@Autowiredprivate SysUserMapper sysUserMapper;@Overridepublic SysUser getSysUserById(Long id) {return sysUserMapper.selectByPrimaryKey(id);}}
3.5 Controller
package com.qiangesoft.demo.controller;import com.qiangesoft.demo.entity.SysUser;
import com.qiangesoft.demo.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 用户** @author qiangesoft* @since 2024-10-14*/
@RequestMapping("/sysUser")
@RestController
public class SysUserController {@Autowiredprivate SysUserService sysUserService;@GetMapping("/{id}")public SysUser findById(@PathVariable Long id) {return sysUserService.getSysUserById(id);}}
3.6 代码结构
3.7 测试
数据库插入一条数据,然后调用接口查询。
四、源码下载
百度网盘:https://pan.baidu.com/s/1zRoU4e7FNpTbzdmbX6lf0w
提取码:1234