完成学习事务
This commit is contained in:
@@ -4,6 +4,7 @@ package cn.mayiming;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
@@ -12,6 +13,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@MapperScan("cn.mayiming.Mapper")
|
||||
@EnableTransactionManagement
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
|
||||
@@ -4,18 +4,26 @@ import cn.mayiming.Mapper.UserMapper;
|
||||
import cn.mayiming.Service.UserService;
|
||||
import cn.mayiming.entity.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping("/user")
|
||||
public User getUser(@RequestBody User user) {
|
||||
return userMapper.selectByUsername(user.getUsername());
|
||||
return userService.getUserByUsername(user.getUsername());
|
||||
}
|
||||
|
||||
@PutMapping("/user")
|
||||
public int updateUser(@RequestBody User user) {
|
||||
return userService.updateUser(user);
|
||||
}
|
||||
|
||||
@GetMapping("/user")
|
||||
public User GetUserByid(@RequestParam Integer id) {
|
||||
return userService.getUserById(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,11 +40,17 @@ public interface UserMapper {
|
||||
* @param user 用户对象(含要更新的字段)
|
||||
* @return 影响行数
|
||||
*/
|
||||
@Update("UPDATE user SET " +
|
||||
"username = #{username}, " +
|
||||
"password = #{password}, " +
|
||||
"nickname = #{nickname} " +
|
||||
"WHERE id = #{id}")
|
||||
@Update({
|
||||
"<script>",
|
||||
"UPDATE user",
|
||||
"<set>",
|
||||
" <if test='username != null'>username = #{username},</if>",
|
||||
" <if test='password != null'>password = #{password},</if>",
|
||||
" <if test='nickname != null'>nickname = #{nickname}</if>",
|
||||
"</set>",
|
||||
"WHERE id = #{id}",
|
||||
"</script>"
|
||||
})
|
||||
int updateById(User user);
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,8 +2,10 @@ package cn.mayiming.Service;
|
||||
|
||||
import cn.mayiming.Mapper.UserMapper;
|
||||
import cn.mayiming.entity.User;
|
||||
import org.apache.ibatis.jdbc.Null;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -21,6 +23,7 @@ public class UserService {
|
||||
return rows;
|
||||
}
|
||||
|
||||
|
||||
// 根据用户名查询
|
||||
public User getUserByUsername(String username) {
|
||||
return userMapper.selectByUsername(username);
|
||||
@@ -30,4 +33,16 @@ public class UserService {
|
||||
public List<User> getAllUsers() {
|
||||
return userMapper.selectAll();
|
||||
}
|
||||
|
||||
public User getUserById(Integer id) {
|
||||
return userMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int updateUser(User user) {
|
||||
if (user.getId() == null){
|
||||
throw new IllegalArgumentException("更新失败:用户ID不能为空");
|
||||
}
|
||||
return userMapper.updateById(user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,4 +19,24 @@ public class User {
|
||||
|
||||
String username;
|
||||
String password;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
Integer id;
|
||||
|
||||
public String getNickname() {
|
||||
return nickname;
|
||||
}
|
||||
|
||||
public void setNickname(String nickname) {
|
||||
this.nickname = nickname;
|
||||
}
|
||||
|
||||
String nickname;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user