美文网首页工作生活
01.Springboot中使用mybatis-plus

01.Springboot中使用mybatis-plus

作者: LewisZhu | 来源:发表于2020-04-01 09:40 被阅读0次

1.导入依赖

        <!-- mybatis plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>latest-version</version>
        </dependency>

2.操作表
1>.需要将要操作的实体类用@TableName("tb_user")标注,以便MP可以知道要操作的表,即就是实体和表进行了映射。
2>.将UserMapper继承BaseMapper,将拥有了BaseMapper中的所有方法.

3.数据插入:int insert(T entity);
1>.注意主键生成的策略

@TableId(type = IdType.AUTO)

2>.insert返回值是受影响的行数;entity生成之后,会回填,比如可以获得自增长的id

entity.getUid()

4.TableField注解
在MP中通过@TableField注解可以指定字段的一些属性,常常解决的问题有2个:
1、对象中的属性名和字段名不一致的问题(非驼峰)
2、对象中的属性字段在表中不存在的问题

5.根据条件进行更新

1>.User user = new User(); user.setAge(22); //更新的字段
//更新的条件
QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("id", 6);
//执行更新操作
int result = this.userMapper.update(user, wrapper);

2>.//更新的条件以及字段
UpdateWrapper<User> wrapper = new UpdateWrapper<>(); wrapper.eq("id", 6).set("age", 23);
//执行更新操作
int result = this.userMapper.update(null, wrapper); System.out.println("result = " + result);

相关文章

网友评论

    本文标题:01.Springboot中使用mybatis-plus

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