UserServiceImpl.java 1.6 KB
Newer Older
Q
qinyingjie 已提交
1 2
package com.kwan.springbootkwan.service.impl;

3
import com.baomidou.dynamic.datasource.annotation.DS;
Q
qinyingjie 已提交
4
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
5
import com.kwan.springbootkwan.entity.Person;
Q
qinyingjie 已提交
6 7
import com.kwan.springbootkwan.entity.User;
import com.kwan.springbootkwan.mapper.UserMapper;
8
import com.kwan.springbootkwan.service.IPersonService;
Q
qinyingjie 已提交
9
import com.kwan.springbootkwan.service.IUserService;
10
import io.seata.spring.annotation.GlobalTransactional;
Q
qinyingjie 已提交
11 12 13 14
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
15
import java.util.Objects;
Q
qinyingjie 已提交
16 17 18 19 20 21

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
22 23
    @Autowired
    private IPersonService personService;
Q
qinyingjie 已提交
24 25 26 27 28 29 30 31 32 33

    @Override
    public List<User> getUsers() {
        return userMapper.selectList(null);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectById(id);
    }
34 35 36

    @Override
    public User getUserByName(String sex) {
37
        User userByName = userMapper.getUserByName(sex);
38 39 40 41 42
        if (Objects.isNull(userByName)) {
            userByName = new User();
        }
        return userByName;
    }
43

44
    @DS(value = "kwan-ds")
45
    @GlobalTransactional
46 47 48 49 50 51 52 53 54 55
    @Override
    public User saveUser(User user) {
        userMapper.insert(user);
        Person person = new Person();
        person.setUsername(user.getName());
        person.setAddress("当铺");
        person.setGender(user.getSex());
        personService.savePerson(person);
        return user;
    }
Q
qinyingjie 已提交
56
}