2.实现增删查改

2022-06-28 19:41:15 浏览数 (1)

本文最后更新于 908 天前,其中的信息可能已经有所发展或是发生改变。

1. 目录

2. mapper

代码语言:javascript复制
public interface UsersMapper {
    void insertUser(Users uses);
    int deleateUser(int id);
    List<Users> selectAllUser();
    Users findUser(int id);
    int updateUser(Users user);
}
代码语言:javascript复制
<mapper namespace="com.yuyy.mapper.UsersMapper">
    <insert id="insertUser" parameterType="Users">
        insert into users (name,age) values(#{name},#{age})
    </insert>
    <delete id="deleateUser">
        delete from users where id = #{id} 
    </delete>
    <select id="selectAllUser" resultType="Users">
        select id,name,age from users
    </select>
    <select id="findUser" resultType="Users">
        select id,name,age from users where id = #{id}
    </select>
    <update id="updateUser" parameterType="Users">
        update users set name='${name}',age=#{age} where id = #{id}
    </update>
</mapper>

3. service

代码语言:javascript复制
public interface UsersService {
    void addUser(Users users);
    int delUser(int id);
    List<Users> selAllUser();
    Users findUser(int id);
    int updUser(Users user);
}
代码语言:javascript复制
@Service
public class UsersServiceImpl implements UsersService{

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public void addUser(Users users) {
        usersMapper.insertUser(users);
    }

    @Override
    public int delUser(int id) {
        return usersMapper.deleateUser(id);
    }

    @Override
    public List<Users> selAllUser() {
        return usersMapper.selectAllUser();
    }

    @Override
    public Users findUser(int id) {
        return usersMapper.findUser(id);
    }

    @Override
    public int updUser(Users user) {
        usersMapper.updateUser(user);
        return 0;
    }

}

4. controller

代码语言:javascript复制
@Controller
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page){
        return page;
    }
    @RequestMapping("/addUser")
    public String addUser(Users users){
        usersService.addUser(users);
        return "ok";
    }
    @RequestMapping("/delUser")
    public String delUser(int id){
        if(usersService.delUser(id)==1){
            return "ok";
        }else{
            return "faild";
        }
    }
    @RequestMapping("/selAllUser")
    public String selAllUser(Model model){
        model.addAttribute("userList",usersService.selAllUser());
        for(Users user:usersService.selAllUser()){
            System.out.println(user.getName());
            System.out.println(user.getAge());
        }
        return "showUserPage";
    }
    @RequestMapping("/findUser")
    public String findUser(int id,Model model){
        model.addAttribute("user",usersService.findUser(id));
        return "updateUserPage";
    }
    @RequestMapping("/updateUser")
    public String updateUser(Users user){
        int t=usersService.updUser(user);
        System.out.println("t:" t);
        if(t==1){
            return "ok";
        }else{
            return "faild";
        }
    }
}

5. addUserPage

代码语言:javascript复制
    <form th:action="@{/users/addUser}" method="post">  
        用户姓名<input type="text" name="name"></input>
        用户年龄<input type="text" name="age"></input>
        <input type="submit" th:value="提交"></input>
    </form>

6. showUserPage

代码语言:javascript复制
<table border="1">
        <tr>
            <th th:text="ID"></th>
            <th th:text="姓名"></th>
            <th th:text="年龄"></th>
        </tr>
        <tr th:each="item:${userList}">
            <td th:text="${item.id}"></td>
            <td th:text="${item.name}"></td>
            <td th:text="${item.age}"></td>
            <td>
                <a th:href="@{/users/findUser(id=${item.id})}">更新用户</a>
            </td>
            <td>
                <a th:href="@{/users/delUser(id=${item.id})}">删除用户</a>
            </td>   
        </tr>
    </table>

7. updateUserPage

代码语言:javascript复制
    <table border="1">
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
        <tr>
            <td>
                <span th:text="${user.id}"></span>
            </td>
            <td>
                <input type="text" th:value="${user.name}"></input>
            </td>
            <td>
                <input type="text" th:value="${user.age}"></input>
            </td>
            <td>
                <a th:href="@{/users/updateUser(id=${user.id},name=${user.name},age=${user.age})}">修改</a>   
            </td>
        </tr>
    </table>

Post Views: 311

0 人点赞