最近新项目,架构师又换框架了,整成batis plus了,在这里做个简单的demo记录一下吧
话说mybatis plus好像是国人搞,壮哉我大中华!
先上pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--启用数据库时开启-->
<!--只需要引入这2个就可以了-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
mapper类
如果调用plus的方法,需要需要继承 BaseMapper这个类
WaybillOwner这个参数是自己的实体类,不是plus的参数,各位主要注意一下
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.model.WaybillOwner;
/**
* <p>
* Mapper 接口
* </p>
*
* @author
* @since 2020-01-02
*/
public interface WaybillOwnerMapper extends BaseMapper<WaybillOwner> {
}
service层
package com.example.demo.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.model.WaybillOwner;
import com.example.demo.vo.OwnerRegisterVO;
import java.util.List;
/**
* <p>
* 服务类
* </p>
*
* @author haibo
* @since 2020-01-02
*/
public interface WaybillOwnerService extends IService<WaybillOwner> {
/**
* 新增
* @param WaybillOwner
* @return
*/
boolean register(WaybillOwner WaybillOwner);
/**
* 列表查询 分页 条件
* @return
*/
Page<WaybillOwner> getWaybillOwners(Integer page, Integer pageSize, String name);
/**
* 根据id查询详情
* @return
*/
WaybillOwner getWaybillOwner(Integer id);
/**
* 根据id删除
* @return
*/
boolean DeleteWaybillOwner(Integer id);
/**
* 更新
* @param WaybillOwner
* @return
*/
boolean UpdataWaybillOwner(WaybillOwner WaybillOwner);
}
serviceImpl实现类
这是需要先继承maybtis的serviceimpl,再实现自己接口的方法
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.WaybillOwnerMapper;
import com.example.demo.model.WaybillOwner;
import com.example.demo.service.WaybillOwnerService;
import com.example.demo.vo.OwnerRegisterVO;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Classname WaybillOwnerServiceImpl
* @Description TODO
* @Date 2020/1/6 16:54
* @Created wrc
*/
@Service
public class WaybillOwnerServiceImpl extends ServiceImpl<WaybillOwnerMapper, WaybillOwner> implements WaybillOwnerService {
/**
* 新增
*
* @param waybillOwner
* @return
*/
@Override
public boolean register(WaybillOwner waybillOwner) {
return this.save(waybillOwner);
}
/**
* 列表查询 分页 条件
*
* @param name
* @return
*/
@Override
public Page<WaybillOwner> getWaybillOwners(Integer page,Integer pageSize,String name) {
return this.page(new Page<>(page,pageSize),
new QueryWrapper<WaybillOwner>().like("owner_name",name));
}
/**
* 根据id查询详情
*
* @param id
* @return
*/
@Override
public WaybillOwner getWaybillOwner(Integer id) {
return this.getById(id);
}
/**
* 根据id删除
*
* @param id
* @return
*/
@Override
public boolean DeleteWaybillOwner(Integer id) {
return this.removeById(id);
}
/**
* 更新
* @param WaybillOwner
* @return
*/
@Override
public boolean UpdataWaybillOwner(WaybillOwner WaybillOwner){
return this.updateById(WaybillOwner);
}
}
分页的话需要一个分页类
package com.example.demo.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @Classname MybatisPlusConfig
* @Description TODO
* @Date 2020/1/7 10:21
* @Created wrc
*/
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
编写测试类,测试通过
package com.example.demo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.model.WaybillOwner;
import com.example.demo.service.WaybillOwnerService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
/**
* @Classname WaybillOwnerTest
* @Description TODO
* @Date 2020/1/6 17:45
* @Created wrc
*/
@SpringBootTest
public class WaybillOwnerTest {
@Autowired
private WaybillOwnerService waybillOwnerService;
@Test
public void register() {
WaybillOwner waybillOwner = new WaybillOwner().setOwnerName("王大锤");
waybillOwnerService.register(waybillOwner);
}
@Test
public void getWaybillOwners() {
waybillOwnerService.getWaybillOwners(1,5,"山东");
}
@Test
public void getWaybillOwner() {
waybillOwnerService.getWaybillOwner(1577);
}
@Test
public void DeleteWaybillOwner() {
waybillOwnerService.DeleteWaybillOwner(1577);
}
//更新需要设置一下id,这是根据id来更新
@Test
public void UpdataWaybillOwner(){
WaybillOwner WaybillOwner = new WaybillOwner().setOwnerName("2222").setOwnerCode("111").setId(1578);
waybillOwnerService.UpdataWaybillOwner(WaybillOwner);
}
}
mybatis plus 还是很强大的,有构造器之类,有兴趣的小伙伴可以去官网去看看
官网链接 https://mp.baomidou.com/guide/wrapper.html#abstractwrapper