12.6 服务层功能实现
BookService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package org.fkit.service;
import java.util.List; import org.fkit.domain.Book;
public interface BookService {
List<Book> getAll(); }
|
BookServiceImpl.java
1 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 31 32 33 34 35 36 37 38
| package org.fkit.service.impl;
import java.util.List; import org.fkit.domain.Book; import org.fkit.mapper.BookMapper; import org.fkit.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;
@Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT) @Service("bookService") public class BookServiceImpl implements BookService {
@Autowired private BookMapper bookMapper;
@Transactional(readOnly = true) @Override public List<Book> getAll() { return bookMapper.findAll(); } }
|
UserService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package org.fkit.service;
import org.fkit.domain.User;
public interface UserService {
User login(String loginname, String password);
}
|
UserServiceImpl.java
1 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 31 32 33 34 35 36 37 38
| package org.fkit.service.impl;
import org.fkit.domain.User; import org.fkit.mapper.UserMapper; import org.fkit.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;
@Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT) @Service("userService") public class UserServiceImpl implements UserService {
@Autowired private UserMapper userMapper;
@Transactional(readOnly = true) @Override public User login(String loginname, String password) { return userMapper.findWithLoginnameAndPassword(loginname, password); } }
|
服务层使用了Spring
的@Autowired
注解自动注入持久层的Mapper
对象,并且使用了@Service
注解将类注释成为Spring
的Bean
。
原文链接: 12.6 服务层功能实现