12.6 服务层功能实现
BookService.java
/MyBookApp/src/com/service/BookService.java1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.service;
import java.util.List; import com.domain.Book;
public interface BookService {
List<Book> getAllBooks(); }
|
BookServiceImpl.java
/MyBookApp/src/com/service/BookServiceImpl.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.service;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.domain.Book; import com.mapper.BookMapper;
@Service("bookService") public class BookServiceImpl implements BookService {
@Autowired private BookMapper bookMapper;
@Override public List<Book> getAllBooks() { return bookMapper.selectAllBooks(); } }
|
UserService.java
/MyBookApp/src/com/service/UserService.java1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.service;
import com.domain.User;
public interface UserService {
User login(String loginname, String password); }
|
UserServiceImpl.java
/MyBookApp/src/com/service/UserServiceImpl.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.domain.User;
import com.mapper.UserMapper;
@Service("userService") public class UserServiceImpl implements UserService {
@Autowired private UserMapper userMapper;
@Override public User login(String loginname, String password) { return userMapper.selectUserByLoginnamePassword(loginname, password); } }
|
服务层使用了Spring
的@Autowired
注解自动注入持久层的Mapper
对象,并且使用了@Service
注解将类注释成为Spring
的Bean
。