SpringBoot基于Mybatis实现增删改查

一、使用 generator 插件生成 pojo 和 mapper

参考:Mybatis配置generator插件

二、Controller编写

1、页面跳转方法

@Controller
public class PageController {
    //页面跳转方法
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page){
        return page;
    }
}

2、增删改查功能

@Controller
@RequestMapping("/user")
public class UsersController {
    @Autowired
    private UsersService usersService;
	
    //添加用户信息
    @PostMapping("/addUser")
    public String addUsers(User user){
        try{
            this.usersService.addUsers(user);
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        return "redirect:/ok";
    }
	
    //获取所有用户信息
    @GetMapping("/findUserAll")
    public String findUserAll(Model model){
        try{
            List<User> list = this.usersService.findUserAll();
            model.addAttribute("list",list);
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        return "showUsers";
    }
	
    //根据id查找用户
    @GetMapping("/preUpdateUser")
    public String preUpdateUser(Integer id,Model model){
        try{
            User user = this.usersService.preUpdateUser(id);
            model.addAttribute("user",user);
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        return "updateUser";
    }
	
    //更新用户信息
    @PostMapping("/updateUser")
    public String updateUser(User user){
        try{
            this.usersService.modifyUsers(user);
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        return "redirect:/ok";
    }

    //根据id删除用户
    @GetMapping("/deleteUser")
    public String deleteUser(Integer id){
        try{
            this.usersService.dropUsersById(id);
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        return "redirect:/ok";
    }
}

二、业务层编写

UserServiceImpl

@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UserMapper userMapper;
	
    //添加用户信息
    @Override
    @Transactional
    public void addUsers(User user) {
        this.userMapper.insert(user);
    }

    //获取所有用户信息
    @Override
    public List<User> findUserAll() {
        UserExample example = new UserExample();
        return this.userMapper.selectByExample(example);
    }

    //根据id查找用户
    @Override
    public User preUpdateUser(Integer id) {
        return this.userMapper.selectByPrimaryKey(id);
    }

    //更新用户信息
    @Override
    @Transactional
    public void modifyUsers(User user) {
        this.userMapper.updateByPrimaryKey(user);
    }

    //根据id删除用户
    @Override
    @Transactional
    public void dropUsersById(Integer id) {
        this.userMapper.deleteByPrimaryKey(id);
    }


}

四、简单前端页面部分代码

所有页面开头需加入 Thymeleaf 支持

<html lang="en" xmlns:th="http://www.thymeleaf.org">

还需编写成功提示页和失败页面,随需求添加即可。

1、用户信息展示页

<table border="1" align="center">
        <tr>
            <th>用户ID</th>
            <th>用户姓名</th>
            <th>用户性别</th>
            <th>操作</th>
        </tr>
        <tr th:each="u : ${list}">
            <td th:text="${u.id}"></td>
            <td th:text="${u.name}"></td>
            <td th:text="${u.gender}"></td>
            <td>
                <a th:href="@{/user/preUpdateUser(id=${u.id})}">修改</a>
                <a th:href="@{/user/deleteUser(id=${u.id})}">删除</a>
            </td>
        </tr>
    </table>

2、添加用户信息页

<form th:action="@{/user/addUser}" method="post">
        <input type="text" name="name"><br/>
        <input type="text" name="gender"><br/>
        <input type="submit" value="添加">
    </form>

3、更改用户信息页

<form th:action="@{/user/updateUser}" method="post">
    <input type="hidden" name="id" th:value="${user.id}">
    <input type="text" name="name" th:value="${user.name}"><br/>
    <input type="text" name="gender" th:value="${user.gender}"><br/>
    <input type="submit" value="添加">
</form>
发布了28 篇原创文章 · 获赞 0 · 访问量 722

猜你喜欢

转载自blog.csdn.net/William_GJIN/article/details/105430927