SpringBootはMybatisに基づいており、追加、削除、変更、チェックを行います

まず、ジェネレータプラグインを使用してpojoとマッパーを生成します

リファレンス:Mybatis構成ジェネレータープラグイン

2、コントローラーの準備

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";
    }
}

2、ビジネス層の準備

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);
    }


}

4番目に、コードの単純なフロントエンドページ部分

すべてのページの最初に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