美文网首页
Spring整合MyBatisPlus

Spring整合MyBatisPlus

作者: 大鱼馆长 | 来源:发表于2022-07-21 10:12 被阅读0次

    MyBatisPlus官网

    1. 引入Maven依赖

    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.4.0</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

    2. 编写Entity和Mapper类并设置扫描范围

    也可以用EasyCode等代码生成工具生成,请参考《EasyCode自动生成代码》

    生成的Entity类:

    package com.spring.accumulator.entity;
    
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableName;
    
    import java.io.Serializable;
    
    import lombok.NoArgsConstructor;
    import lombok.Data;
    
    /**
     * (Person)表实体类
     *
     * @author wangrubin
     * @since 2022-07-15 18:22:45
     */
    @Data
    @NoArgsConstructor
    @TableName("person")
    public class PersonPO implements Serializable {
    
        private String name;
    
        private Integer male;
    
        private Integer age;
    
        @TableId(value = "id", type = IdType.AUTO)
        private Long id;
    }
    

    生成Mapper类

    package com.spring.accumulator.dao;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.spring.accumulator.entity.PersonPO;
    import org.apache.ibatis.annotations.Mapper;
    
    /**
     * (Person)表数据库访问层
     *
     * @author wangrubin
     * @since 2022-07-15 18:22:45
     */
    @Mapper
    public interface PersonMapper extends BaseMapper<PersonPO> {
    }
    

    配置@MapperScan,使Mapper类可被MyBatisPlus识别

    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan("com.spring.accumulator.dao")
    public class AccumulatorApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AccumulatorApplication.class, args);
        }
    
    }
    

    3. 配置文件中增加MySQL数据库的连接配置

    spring.datasource.url=jdbc:mysql://localhost:3306/wangrubin_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&tinyInt1isBit=false&&useSSL=false&&rewriteBatchedStatements=true
    spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
    spring.datasource.username=root
    spring.datasource.password=mysql

    4. 测试MyBatisPlus的功能

    package com.spring.accumulator;
    
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.spring.accumulator.dao.PersonMapper;
    import com.spring.accumulator.entity.PersonPO;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ContextConfiguration(classes = AccumulatorApplication.class)
    public class MyBatisPlusTest {
        @Resource
        private PersonMapper personMapper;
    
        @Test
        public void testQueryWrapper() {
            QueryWrapper<PersonPO> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(PersonPO::getId, 1L);
            PersonPO personPo = personMapper.selectOne(queryWrapper);
            System.out.println(personPo);
        }
    

    5. 总结

    本文整理了Spring Boot整合MyBatisPlus的必要步骤和最基本操作,要想深入探索MyBatisPlus的更多功能,请查阅MyBatisPlus官网文档。

    相关文章

      网友评论

          本文标题:Spring整合MyBatisPlus

          本文链接:https://www.haomeiwen.com/subject/tgeeirtx.html