springboot整合mybatis-plus快速开发
1.pom.xml引入mybatis-plus依赖
<!--Mybatis--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency>
2.实体类
@Data public class Book { private Integer id; private String name; private String type; private String description; }
3.mapper(dao层)
@Mapper public interface BookDao extends BaseMapper<Book> { }
接口需要继承 BaseMapper
4.service
public interface IBookService extends IService<Book>{ }
需要继承 IService<实体类> 里面的类型填写实体类即可自动生成该实体类的基础sql操作
5.实现类
@Service public class BookServiceimpl2 extends ServiceImpl<BookDao, Book> implements IBookService { }
需要继承 ServiceImpl 类型第一个填dao层,第二个是实体类 ,实现IBookService类
6.使用
@SpringBootTest public class serviceTest { @Autowired private IBookService bookService; @Test void test1(){ Book book = bookService.getById(2); System.out.println(book); } @Test void testSave(){ Book book = new Book(); book.setType("1"); book.setName("测试数据55A4"); book.setDescription("测试数据5AAAA"); bookService.save(book); } @Test void testUpdate(){ Book book = new Book(); book.setId(19); book.setType("3"); book.setName("测试数据aa"); book.setDescription("测试数据bb"); bookService.updateById(book); } @Test void testDelete(){ Boolean delete = bookService.removeById(19); System.out.println(delete); } @Test void testGetAll(){ List<Book> books = bookService.list(null); System.out.println(books); } @Test void testGetPage(){ IPage<Book> ipage = new Page<Book>(1,5); IPage<Book> page = bookService.page(ipage, null); List<Book> records = page.getRecords(); System.out.println(records); } }
仅生成基础mysql操作,需要其它功能要自己添加接口实现。