一、配置MySQL数据库和MyBatis-Plus依赖
<dependencies><!-- 导入MySQL依赖 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><!-- 导入Mybatis-Plus依赖 --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.0</version></dependency><!-- 导入lombok依赖 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins><!-- 将java目录下的xml文件、resources目录中的静态文件进行编译 --><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>true</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.*</include></includes></resource></resources></build>
二、配置yml文件中MySQL的连接和Mybatis-Plus配置
spring:# mysql配置信息datasource:# 数据库地址 端口 数据库名url: jdbc:mysql://127.0.0.1:3306/springboot_demo?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8username: root # 账号password: root # 密码driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:mapper-locations: mapper/*.xml#这个配置是用来连接mapper接口和mapper配置类的configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #运行日志map-underscore-to-camel-case:true#驼峰转换type-aliases-package: com.example.springbootdemo.model #实体类扫描包
三、创建一个测试表并添加数据
四、测试使用MyBatis-Plus 查询数据
1、创建实体类
packagecom.example.springbootdemo.model;importcom.baomidou.mybatisplus.annotation.*;importcom.fasterxml.jackson.annotation.JsonFormat;importlombok.Data;importjava.util.Date;/**
* @ClassName: User
* @Description:
* @Author: WangBin
* @Date: 2023/6/17 20:48
*/@Data@TableName("user")publicclassUser{/** id */@TableId(value ="id", type =IdType.AUTO)privateInteger id;/** 名字 */privateString name;/** 账号 */privateString account;/** 密码 */privateString password;/** 创建者 */privateString createBy;/** 创建时间 */@JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss")// 规定时间数据格式privateDate createTime;/** 更新者 */privateString updateBy;/** 更新时间 */@JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss")// 规定时间数据格式privateDate updateTime;/** 备注 */privateString remark;}
2、创建Controller
packagecom.example.springbootdemo.controller;importcom.example.springbootdemo.model.User;importcom.example.springbootdemo.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;importjava.util.List;/**
* @ClassName: UserController
* @Description:
* @Author: WangBin
* @Date: 2023/6/17 21:07
*/@RestControllerpublicclassUserController{@AutowiredprivateUserService userService;/**
* 根据ID查询用户(使用MyBatis-Plus方法查询)
* @param userId
* @return
*/@RequestMapping(value ="/queryUserById/{userId}", method =RequestMethod.GET)publicUserqueryUserById(@PathVariable("userId")Integer userId){User user = userService.getById(userId);return user;}/**
* 条件查询(使用MyBatis-Plus方法查询)
* @param user
* @return
*/@RequestMapping(value ="/getList", method =RequestMethod.POST)publicList<User>getList(@RequestBodyUser user){List<User> list = userService.getList(user);return list;}/**
* 根据姓名查询用户
* @param name
* @return
*/@RequestMapping(value ="/getUserByName",method =RequestMethod.GET)publicList<User>getUserByName(@RequestParam("name")String name){List<User> users = userService.getUserByName(name);return users;}/**
* 根据账号查询用户
* @param account
* @return
*/@RequestMapping(value ="/getUserByAccount",method =RequestMethod.GET)publicUsergetUserByAccount(@RequestParam("account")String account){User user = userService.getUserByAccount(account);return user;}}
3、创建service接口
packagecom.example.springbootdemo.service;importcom.baomidou.mybatisplus.extension.service.IService;importcom.example.springbootdemo.model.User;importjava.util.List;/**
* @ClassName: UserService
* @Description:
* @Author: WangBin
* @Date: 2023/6/17 21:09
*/publicinterfaceUserServiceextendsIService<User>{/**
* 条件查询
* @param user
* @return
*/List<User>getList(User user);/**
* 根据姓名查询用户
* @param name
* @return
*/List<User>getUserByName(String name);/**
* 根据账号查询用户
* @param account
* @return
*/UsergetUserByAccount(String account);}
4、创建Service实现类
packagecom.example.springbootdemo.service.impl;importcom.baomidou.mybatisplus.core.conditions.query.QueryWrapper;importcom.baomidou.mybatisplus.extension.service.impl.ServiceImpl;importcom.example.springbootdemo.mapper.UserMapper;importcom.example.springbootdemo.model.User;importcom.example.springbootdemo.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;/**
* @ClassName: UserServiceImpl
* @Description:
* @Author: WangBin
* @Date: 2023/6/17 21:10
*/@ServicepublicclassUserServiceImplextendsServiceImpl<UserMapper,User>implementsUserService{@AutowiredprivateUserMapper userMapper;/**
* 条件查询
* @param user
* @return
*/@OverridepublicList<User>getList(User user){QueryWrapper<User> wrapper =newQueryWrapper<>();if(user.getId()!=null&& user.getId()>0){
wrapper.eq("id",user.getId());}if(user.getName()!=null&& user.getName()!=""){
wrapper.like("name",user.getName());}// .....List<User> list = userMapper.selectList(wrapper);return list;}/**
* 根据姓名查询用户
* @param name
* @return
*/@OverridepublicList<User>getUserByName(String name){List<User> list = userMapper.getUserByName(name);return list;}@OverridepublicUsergetUserByAccount(String account){User user = userMapper.getUserByAccount(account);return user;}}
5、创建Mapper
packagecom.example.springbootdemo.mapper;importcom.baomidou.mybatisplus.core.mapper.BaseMapper;importcom.example.springbootdemo.model.User;importorg.apache.ibatis.annotations.Mapper;importorg.apache.ibatis.annotations.Param;importorg.apache.ibatis.annotations.Select;importjava.util.List;/**
* @ClassName: UserMapper
* @Description:
* @Author: WangBin
* @Date: 2023/6/17 20:27
*/@MapperpublicinterfaceUserMapperextendsBaseMapper<User>{@Select("select * from `user` where name=#{name}")List<User>getUserByName(@Param("name")String name);UsergetUserByAccount(@Param("account")String account);}
6、创建Mapper配置类
<?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.example.springbootdemo.mapper.UserMapper"><select id="getUserByAccount" parameterType="java.lang.String" resultType="com.example.springbootdemo.model.User">
select * from `user` where account=#{account}</select></mapper>
五、接口测试
1、URL携带参数测试(通过\绑定)
2、POST请求体测试
3、URL携带参数测试(通过?绑定)
本文转载自: https://blog.csdn.net/wb980520/article/details/131263743
版权归原作者 Java的继父 所有, 如有侵权,请联系我们删除。
版权归原作者 Java的继父 所有, 如有侵权,请联系我们删除。