service接口,继承MP的IService
泛型为 实体类
package com.example.demo07.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo07.dto.Book;
public interface IBookService extends IService<Book> {
}
service实现类
extends ServiceImpl<BookDao, Book>
ServiceImpl 实现了 IService 接口,
不必自己去实现 IService 接口 中的方法
package com.example.demo07.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo07.dao.BookDao;
import com.example.demo07.dto.Book;
import com.example.demo07.service.IBookService;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
}
测试类
package com.example.demo07.service;
import com.example.demo07.dto.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class BookServiceTestCase {
@Autowired
private IBookService bookService;
@Test
void testGetById(){
//getById继承自IService
Book book = bookService.getById(3);
System.out.println( book );
}
}
结果
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@66a5755] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7980cf2c] will not be managed by Spring
==> Preparing: SELECT id,type,name,description FROM book WHERE id=?
==> Parameters: 3(Integer)
<== Columns: id, type, name, description
<== Row: 3, 文学, 葡萄牙的高山, 苦难
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@66a5755]
Book(id=3, type=文学, name=葡萄牙的高山, description=苦难)