美文网首页Java编程思想Spring-Boot
Spring Boot整合Mybatis-plus

Spring Boot整合Mybatis-plus

作者: 第八号灬当铺 | 来源:发表于2017-08-11 00:22 被阅读1163次

    将上一篇Spring Boot整合Mybatis 修改为Mybatis-plus

    Mybatis-plus我就不多做介绍了 直接百度搜索了解一下

    首先将pom.xml 中的 mybatis-spring-boot-starter 换成 mybatisplus-spring-boot-starter

    <!--<dependency>-->
        <!--<groupId>org.mybatis.spring.boot</groupId>-->
        <!--<artifactId>mybatis-spring-boot-starter</artifactId>-->
        <!--<version>1.3.0</version>-->
    <!--</dependency>-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatisplus-spring-boot-starter</artifactId>
        <version>1.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus</artifactId>
        <version>2.1-gamma</version>
    </dependency>
    

    然后将Mybatis配置 换成 Mybatis-plus配置
    完全兼容Mybatis的所有配置

    # Mybatis配置
    #mybatis:
    #    mapperLocations: classpath:mapper/**/*.xml
    #    typeAliasesPackage: com.xiaohan.bootdemo.entity
    #    #config-location: classpath:mybatis.xml
    #    configuration:
    #      map-underscore-to-camel-case: true  #使用驼峰法映射属性
    
    # Mybatis-plus配置
    mybatis-plus:
      mapper-locations: classpath:mapper/**/*.xml
      typeAliasesPackage: com.xiaohan.bootdemo.entity
      configuration:
        map-underscore-to-camel-case: true  #使用驼峰法映射属性
      global-config:
        #主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
        id-type: 0
        #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
        field-strategy: 2
        #驼峰下划线转换
        db-column-underline: true
        #刷新mapper 调试神器
        refresh-mapper: true
        #数据库大写下划线转换
        #capital-mode: true
        #序列接口实现类配置
        #key-generator: com.baomidou.springboot.xxx
        #逻辑删除配置
        #logic-delete-value: 0
        #logic-not-delete-value: 1
        #自定义填充策略接口实现
        #meta-object-handler: com.baomidou.springboot.xxx
        #自定义SQL注入器
        #sql-injector: com.baomidou.springboot.xxx
    

    可以完全兼容mybatis的

    现在来试一下plus的api

    将dao接口 里面的方法全部删除 并继承BaseMapper指定实体泛型

    package com.xiaohan.bootdemo.dao;
    
    import com.baomidou.mybatisplus.mapper.BaseMapper;
    import com.xiaohan.bootdemo.entity.UserEntity;
    import org.apache.ibatis.annotations.Mapper;
    import org.springframework.stereotype.Repository;
    
    @Repository
    @Mapper
    public interface UserDao extends BaseMapper<UserEntity> {
    }
    

    修改实体类如下
    继承Model指定泛型为本类
    在类上使用@TableName指定映射的表
    在主键id上使用@TableId主键 指定主键策略 此处优先级 比 全局配置要高
    主键必须存在 且 不支持复合主键
    实现pkVal方法返回主键

    package com.xiaohan.bootdemo.entity;
    
    import com.baomidou.mybatisplus.activerecord.Model;
    import com.baomidou.mybatisplus.annotations.TableId;
    import com.baomidou.mybatisplus.annotations.TableName;
    import com.baomidou.mybatisplus.enums.IdType;
    
    import java.io.Serializable;
    import java.util.Date;
    
    @TableName("t_user")
    public class UserEntity extends Model<UserEntity> implements Serializable {
    
        @TableId(value = "id", type = IdType.AUTO)
        private Integer id;
        private String name;
        private Date createTime;
    
        //省略set get方法
    
        @Override
        protected Serializable pkVal() {
            return this.id;
        }
    
        @Override
        public String toString() {
            return "UserEntity{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", createTime=" + createTime +
                    '}';
        }
    }
    

    测试类如下 更多API请访问mybatis-plus github仓库查看

    package com.xiaohan.bootdemo;
    
    import com.xiaohan.bootdemo.dao.UserDao;
    import com.xiaohan.bootdemo.entity.UserEntity;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.Date;
    import java.util.List;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class BootdemoApplicationTests {
    
        @Autowired
        private UserDao userDao;
    
        @Test
        public void insertTest() {
            UserEntity userEntity = new UserEntity();
            userEntity.setName("李四");
            Integer i = userDao.insert(userEntity);
            Integer id = userEntity.getId();
            System.err.println("影响行数==>" + i);
            System.err.println("id==>" + id);
        }
    
        @Test
        public void selectAllTest() {
            List<UserEntity> list = userDao.selectList(null);
            for (UserEntity userEntity:
                 list) {
                System.err.println(userEntity);
            }
        }
    
    
        @Test
        public void selectByIdTest() {
            UserEntity userEntity = userDao.selectById(1);
            System.err.println(userEntity);
        }
    
        @Test
        public void deleteTest() {
            int i = userDao.deleteById(1);
            System.err.println("影响行数==>" + i);
            selectAllTest();
        }
    
        @Test
        public void updateByIdTest() {
            UserEntity userEntity = new UserEntity();
            userEntity.setId(2);
            userEntity.setName("王五");
            userEntity.setCreateTime(new Date());
            int i = userDao.updateById(userEntity);
            System.err.println("影响行数==>" + i);
            selectAllTest();
        }
    }
    

    相关文章

      网友评论

        本文标题:Spring Boot整合Mybatis-plus

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