几分钟了解 MybatisPlus

4.MybatisPlus

1.官网



MyBatis-Plus

MyBatis-Plus 官方文档

https://baomidou.com/

2.加入依赖包

Spring Boot项目如下:


<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.2</version>
</dependency>

在配置文件加如下配置mybatis-plus.type-aliases-package=com.beiyou.entitymybatis.mapper-locations = classpath:mapper/*Mapper.xml

Spring MVC 项目如下

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


   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.baomidou.mybatisplus.samples.quickstart.mapper"/>
    </bean>
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>

3.配置(必须)



4.Model

@TableName("account")
public class Account {

    private Integer id;
    @TableField("name")
    private String name;
    private BigDecimal money;
}



5.使用

@Mapper
public interface AccountMapper extends BaseMapper<Account> {

}

Java

6.使用单元测试测试

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.3.2</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.6.0</version>
      <scope>test</scope>
    </dependency>


XML

   @Autowired
    private UserMapper userMapper;
   
    @Autowired
    private TukuMapper tukuMapper;

    @Test
    void test4() {
        Tuku tuku1 = new Tuku();
        tuku1.setId(3);
        tukuMapper.deleteById(tuku1);
        tukuMapper.deleteById(2);
        tukuMapper.deleteBatchIds(CollUtil.newArrayList(1,2,3));
        UpdateWrapper<Tuku> wapper = Wrappers.update();
        wapper.eq("id",3);
        tukuMapper.delete(wapper);

    }
    @Test
    void test3() {
        Tuku tuku1 = new Tuku();
        tuku1.setId(3);
        tuku1.setImg("艳杰2.gif");
        //int i = tukuMapper.updateById(tuku1);

        //UpdateWrapper<Tuku> wapper = new UpdateWrapper<>();
        UpdateWrapper<Tuku> wapper = Wrappers.update();
        wapper.set("img","艳杰300.gif");
        wapper.eq("id",3);
        //int update = tukuMapper.update(null, wapper);

        LambdaUpdateWrapper<Tuku> wapper3 = new LambdaUpdateWrapper<>();
        wapper3.set(Tuku::getImg,"艳杰400.gif");
        wapper3.eq(Tuku::getId,3);
        int update3 = tukuMapper.update(null, wapper3);
    }

    @Test
    void test2() {
        QueryWrapper wapper = new QueryWrapper<Tuku>();
        //wapper.eq("img","张北.gif");
        Integer minId = null;
        //where 后面 加这个 id > xx
        wapper.ge(minId != null, "id", minId);
        wapper.like("img", "强");

        LambdaQueryWrapper<Tuku> wapper2 = new LambdaQueryWrapper();
        wapper2.like(Tuku::getImg, "强");

        List<Tuku> userList = tukuMapper.selectList(wapper2);
        userList.forEach(System.out::println);
    }


    @Test
    void test1() {
        List<UserEntity> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
    }


结尾



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值