0%

12.6 服务层功能实现

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;

/**
* Book服务层接口
*/
public interface BookService
{
/**
* 查找所有图书
* @return Book对象集合
*/
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;

/*
* Book服务层接口实现类 @Service("bookService")
* 用于将当前类注释为一个Spring的bean,名为bookService
*/
@Transactional(
propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT)
@Service("bookService")
public class BookServiceImpl implements BookService
{
/**
* 自动注入BookMapper
*/
@Autowired
private BookMapper bookMapper;
/**
* BookService接口getAll方法实现
* @see { BookService }
*/
@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;

/**
* User服务层接口
*/
public interface UserService
{
/**
* 判断用户登录
* @param String loginname
* @param String password
* @return 找到返回User对象,没有找到返回null
*/
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;

/**
* User服务层接口实现类 @Service("userService")
* 用于将当前类注释为一个Spring的bean,名为userService
*/
@Transactional(
propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT)
@Service("userService")
public class UserServiceImpl implements UserService
{
/**
* 自动注入UserMapper
*/
@Autowired
private UserMapper userMapper;

/**
* UserService接口login方法实现
* @see { UserService }
*/
@Transactional(readOnly = true)
@Override
public User login(String loginname, String password)
{
return userMapper.findWithLoginnameAndPassword(loginname, password);
}
}

服务层使用了Spring@Autowired注解自动注入持久层的Mapper对象,并且使用了@Service注解将类注释成为SpringBean

原文链接: 12.6 服务层功能实现