【Spring Boot】Controller类--UserController

package com.ssm.web.controller;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beasn.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.srpingframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ssm.po.Role;
import com.ssm.po.User;
import com.ssm.service.RoleService;
import com.ssm.service.UserService;

/*
 *用户控制类
 */
@Controller
public class UserController{
    //依赖注入
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    //查询所有用户状态的用户集合(用户列表)
    @RequestMapping(value="/findUserList.action")
    public String findUserList(String keywords,Integer userListRoleId,Model model){
        //获取角色列表
        List<Role> roleList = roleService.findRoleList();
        model.addAttribute("userList",userList);
        model.addAttribute("keywords",keywords);
        model.addAttribute("userListRoleId",userListRoleId);
        return "user/user_list";
    }
    //转向添加用户
    @RequestMapping(vaule="/toAddUser.action")
    public String toAddUser(Model model){
        //获取角色列表,用于添加用户页面中的用户角色下拉列表
        List<Role> roleList = roleService.findRoleList();
        model.addAttribute("roleList",roleList);
        return "user/add_user";
    }
 //判断登录账号是否已存在
    @RequestMapping(vulue="/checkLoginName.action")
    @ResponseBody
    public User checkLoginName(@RequestBody User user,Model modle){
        User checkUSer = userService.getUserByLoginName(user.getLoginName());
        
    }
}

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是示例代码: 首先,需要配置pom.xml文件,引入相应的依赖: ```xml <!-- Spring Boot 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis-Plus 依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <!-- MySQL 驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> ``` 接着,配置application.properties文件,配置数据库相关信息: ```properties # 数据库配置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root # MyBatis-Plus 配置 mybatis-plus.mapper-locations=classpath:mapper/*.xml mybatis-plus.type-aliases-package=com.example.demo.entity mybatis-plus.global-config.db-config.id-type=auto ``` 其中,mapper/*.xml是指mapper目录下的所有xml文件都会被MyBatis-Plus自动扫描,并注册到Spring容器中。 下面,创建实体和Mapper接口: ```java // User实体 @Data @AllArgsConstructor @NoArgsConstructor public class User { private Long id; private String name; private Integer age; private String email; } // UserMapper接口 @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 其中,@Mapper注解表示该接口是一个Mapper接口,并由MyBatis-Plus自动实现其方法。 接下来,编写Controller层的增删改查方法: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserMapper userMapper; // 查询所有用户 @GetMapping("/list") public List<User> userList() { return userMapper.selectList(null); } // 根据id查询用户 @GetMapping("/{id}") public User getUserById(@PathVariable("id") Long id) { return userMapper.selectById(id); } // 添加用户 @PostMapping("/add") public String addUser(@RequestBody User user) { int rows = userMapper.insert(user); return rows > 0 ? "success" : "fail"; } // 修改用户 @PutMapping("/update") public String updateUser(@RequestBody User user) { int rows = userMapper.updateById(user); return rows > 0 ? "success" : "fail"; } // 根据id删除用户 @DeleteMapping("/{id}") public String deleteUserById(@PathVariable("id") Long id) { int rows = userMapper.deleteById(id); return rows > 0 ? "success" : "fail"; } } ``` 其中,@GetMapping、@PostMapping、@PutMapping、@DeleteMapping注解分别表示对应的HTTP请求方法。 最后,启动Spring Boot应用,访问http://localhost:8080/user/list即可查询所有用户,访问http://localhost:8080/user/1即可根据id查询用户。其他增删改的操作也似。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张天龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值