深入学习java笔记-9.SpringBoot2.1之Mybatis注解与事务

pom.xml
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        
        
            mysql
            mysql-connector-java
            5.1.47
        
application.yml
datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/spring_boot_mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: *********
User.java
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User {

    private Integer id;

    private String username;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer age;

    @JsonProperty("account")
    private String phone;

    @JsonIgnore
    private String email;

}
UserDao.java
import com.ctgu.springstart.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserDao {

    /**
     * 根据用户名称,查询用户信息
     * @param username 用户名
     */
    @Select("select * from user where username = #{username}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "age", column = "age"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "email", column = "email"),
    })
    User findByName(String username);

    @Select("select * from user where id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "age", column = "age"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "email", column = "email"),
    })
    User findById(Integer id);

    @Select("select * from user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "age", column = "age"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "email", column = "email")
    })
    List getAllUser();

    @Update("update user set username=#{username} where id=#{id}")
    void updateUser(User user);

    @Delete("delete from user where id =#{id}")
    void deleteUser(User user);
}
UserService.java
import com.ctgu.springstart.domain.User;

import java.util.List;
import java.util.Map;

public interface UserService {

    User findUserByName(String username);

    User findUserById(Integer id);

    List getAllUser();

    Map updateUser(String name, Integer id);

    void deleteUser(User user);
}
UserServiceImpl.java
import com.ctgu.springstart.dao.UserDao;
import com.ctgu.springstart.domain.User;
import com.ctgu.springstart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public User findUserByName(String username) {
        return userDao.findByName(username);
    }

    @Override
    public User findUserById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public List getAllUser() {
        return userDao.getAllUser();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map updateUser(String name, Integer id) {
        Map map = new HashMap<>();
        User user = findUserById(id);
        if(null == user){
            map.put("isUpdate", false);
            map.put("result", null);
        }else{
            user.setUsername(name);
            userDao.updateUser(user);
//            int b = 1/0;                //开启异常事务会回滚
            map.put("isUpdate", true);
            map.put("result", findUserById(id));
        }
        return map;
    }

    @Override
    public void deleteUser(User user) {
        userDao.deleteUser(user);
    }
}

你可能感兴趣的:(深入学习java笔记-9.SpringBoot2.1之Mybatis注解与事务)