SpringBoot整合mybatis-plus入门

本文详细介绍了如何在SpringBoot项目中集成MyBatisPlus,包括配置pom.xml依赖、application.properties参数设置、创建UserMapper接口及其实现、配置MybatisplusConfig类、实现UserController类的方法等。通过具体代码示例,读者可以快速掌握MyBatisPlus的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pom.xml中加入如下依赖

  <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.3</version>
        </dependency>
复制代码

application.properties

##端口号
server.port=8888
##数据库url
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
##数据库用户名
spring.datasource.username=root
##数据库密码
spring.datasource.password=1234
##数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##日志级别
logging.level.com.dalaoyang.dao.UserMapper=debug
##mybatis-plus mapper xml 文件地址
mybatis-plus.mapper-locations=classpath*:mapper/*Mapper.xml
##mybatis-plus type-aliases 文件地址
mybatis-plus.type-aliases-package=cn.niit.entity
复制代码

UserMapper.java接口加上@Mapper这个注解,@Component不加时自动注入时变量报红但似乎并不影响运行(难道是我idea的问题?)

@Mapper
@Component
public interface UserMapper extends BaseMapper<User> {
     List<User> getUserList();

}
复制代码

UserMapper.xml:接口对应的配置

<?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="cn.niit.dao.UserMapper">
    <resultMap id="user" type="cn.niit.entity.User"/>
    <parameterMap id="user" type="cn.niit.entity.User"/>

    <select id="getUserList" resultMap="user">
        SELECT  * FROM USER
    </select>
</mapper>
复制代码

MybatisplusConfig.java配置类加上@Configuration这个注解

@Configuration
public class MybatisplusConfig {
//    分页拦截器
    @Bean
    public PaginationInterceptor pageInterceptor()
    {
        PaginationInterceptor paginationInterceptor=new PaginationInterceptor();
        paginationInterceptor.setDialectType("mysql");
        return paginationInterceptor;
    }
}
复制代码

UserController.java

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping(value = "getUserList")
    public List<User> getUserList() {
        return userMapper.getUserList();
    }

    //条件查询
    @GetMapping(value = "getUserListByName")
    public List<User> getUserListByName(String userName) {
        Map map = new HashMap();
        map.put("user_name", userName);
        return userMapper.selectByMap(map);
    }

    //根据Id查询User
    @GetMapping("getUserById")
    public User getUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    //http://localhost:8888/getUserListByPage?pageNumber=1&pageSize=2
    //条件分页查询
    @GetMapping(value = "getUserListByPage")
    public List<User> getUserListByPage(Integer pageNum, Integer pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);
        EntityWrapper<User> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("user_name", "xiaoli");
        return userMapper.selectPage(page, entityWrapper);
    }

    //保存用户
    @GetMapping(value = "saveUser")
    public String saveUser(String userName, String userPassword) {
        User user = new User(userName, userPassword);
        Integer insert = userMapper.insert(user);
        if (insert > 0) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    //修改用户
    @GetMapping(value = "updateUser")
    public String updateUser(Integer id,String userName,String userPassword)
    {
        User user = new User(id, userName, userPassword);
        Integer index = userMapper.updateById(user);
        if (index>0)
        {
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
}
复制代码

接口继承BaseMapper

扫描接口
实体类变量要跟表的字段一致,没单独配置一一映射关系
加入的依赖是mybatisplus-spring-boot-starter

个人网站

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值