spring boot MybatisPlus

MybatisPlus 是Mybatis的增强
所以在引入MybatisPLus的依赖时要将Mybatis的依赖删掉(防止紊乱)

引入依赖

<!--spring整合mybatis-plus 只导入MP包,删除mybatis包 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

MybatisPuls将数据库的表与对象关连,字段与属性关联
@TableName("表名")
@TableId(type=IdType.AUTO)
@TableField(value="字段名")

@Data
@Accessors(chain = true)//链式规则
@TableName("user")    //实现表与对象的关联  如果名称一致(忽略大小写)可以省略表名
public class User implements Serializable {

    //但凡定义pojo实体对象中的属性类型必须使用包装类型.
    @TableId(type = IdType.AUTO)
    private Integer id;  //主键,并且主键自增
    //@TableField(value = "name")  当字段名和属性名一致时可以省略这个注解
    private String name;
    //@TableField(value = "age")
    private Integer age;
    private String sex;

    //动态生成get和set方法及构造方法 快捷键 alt+insert

}

mapper接口(dao)继承BaseMapper注意泛型是我们要操作的对象

//@Mapper //将Mapper接口交给Spring容器管理
//注意泛型引入.如果不添加则数据库操作没办法完成.
public interface UserMapper extends BaseMapper<User> {
}

修改配置文件

#Mybatisplus整合
mybatis-plus:
  #定义别名包 将实体对象的包路径进行封装.
  type-aliases-package: com.jt.pojo
  #添加xml文件的依赖
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

测试MP方法

    @Test
    public void test02(){
        //查询user表的全部数据
        List<User> userList = userMapper.selectList(null);
        System.out.println(userList);
    }
posted @ 2020-09-02 13:55  岁月染过的梦  阅读(140)  评论(0)    收藏  举报