一、配置MySQL数据库和MyBatis-Plus依赖
mysql
mysql-connector-java
8.0.28
com.baomidou
mybatis-plus-boot-starter
3.4.0
org.projectlombok
lombok
provided
org.springframework.boot
spring-boot-maven-plugin
src/main/java
**/*.xml
true
src/main/resources
**/*.*
二、配置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%2B8
username: 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、创建实体类
package com.example.springbootdemo.model;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
/**
* @ClassName: User
* @Description:
* @Author: WangBin
* @Date: 2023/6/17 20:48
*/
@Data
@TableName("user")
public class User {
/** id */
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/** 名字 */
private String name;
/** 账号 */
private String account;
/** 密码 */
private String password;
/** 创建者 */
private String createBy;
/** 创建时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") // 规定时间数据格式
private Date createTime;
/** 更新者 */
private String updateBy;
/** 更新时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") // 规定时间数据格式
private Date updateTime;
/** 备注 */
private String remark;
}
2、创建Controller
package com.example.springbootdemo.controller;
import com.example.springbootdemo.model.User;
import com.example.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @ClassName: UserController
* @Description:
* @Author: WangBin
* @Date: 2023/6/17 21:07
*/
@RestController
public class UserController {
@Autowired
private UserService userService;
/**
* 根据ID查询用户(使用MyBatis-Plus方法查询)
* @param userId
* @return
*/
@RequestMapping(value = "/queryUserById/{userId}", method = RequestMethod.GET)
public User queryUserById(@PathVariable("userId") Integer userId){
User user = userService.getById(userId);
return user;
}
/**
* 条件查询(使用MyBatis-Plus方法查询)
* @param user
* @return
*/
@RequestMapping(value = "/getList", method = RequestMethod.POST)
public List getList(@RequestBody User user){
List list = userService.getList(user);
return list;
}
/**
* 根据姓名查询用户
* @param name
* @return
*/
@RequestMapping(value = "/getUserByName",method = RequestMethod.GET)
public List getUserByName(@RequestParam("name") String name){
List users = userService.getUserByName(name);
return users;
}
/**
* 根据账号查询用户
* @param account
* @return
*/
@RequestMapping(value = "/getUserByAccount",method = RequestMethod.GET)
public User getUserByAccount(@RequestParam("account") String account){
User user = userService.getUserByAccount(account);
return user;
}
}
3、创建service接口
package com.example.springbootdemo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.springbootdemo.model.User;
import java.util.List;
/**
* @ClassName: UserService
* @Description:
* @Author: WangBin
* @Date: 2023/6/17 21:09
*/
public interface UserService extends IService {
/**
* 条件查询
* @param user
* @return
*/
List getList(User user);
/**
* 根据姓名查询用户
* @param name
* @return
*/
List getUserByName(String name);
/**
* 根据账号查询用户
* @param account
* @return
*/
User getUserByAccount(String account);
}
4、创建Service实现类
package com.example.springbootdemo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springbootdemo.mapper.UserMapper;
import com.example.springbootdemo.model.User;
import com.example.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @ClassName: UserServiceImpl
* @Description:
* @Author: WangBin
* @Date: 2023/6/17 21:10
*/
@Service
public class UserServiceImpl extends ServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
/**
* 条件查询
* @param user
* @return
*/
@Override
public List getList(User user) {
QueryWrapper wrapper = new QueryWrapper<>();
if (user.getId() != null && user.getId() > 0){
wrapper.eq("id",user.getId());
}
if (user.getName() != null && user.getName() != ""){
wrapper.like("name",user.getName());
}
// .....
List list = userMapper.selectList(wrapper);
return list;
}
/**
* 根据姓名查询用户
* @param name
* @return
*/
@Override
public List getUserByName(String name) {
List list = userMapper.getUserByName(name);
return list;
}
@Override
public User getUserByAccount(String account) {
User user = userMapper.getUserByAccount(account);
return user;
}
}
5、创建Mapper
package com.example.springbootdemo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springbootdemo.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @ClassName: UserMapper
* @Description:
* @Author: WangBin
* @Date: 2023/6/17 20:27
*/
@Mapper
public interface UserMapper extends BaseMapper {
@Select("select * from `user` where name=#{name}")
List getUserByName(@Param("name") String name);
User getUserByAccount(@Param("account") String account);
}
6、创建Mapper配置类
五、接口测试
1、URL携带参数测试(通过\绑定)
2、POST请求体测试
3、URL携带参数测试(通过?绑定)