> 2.实现增删查改 - Yuyy
Yuyy
Yuyy
2.实现增删查改

1. 目录

http://yuyy.info/image/Snipaste_2020-01-02_00-04-25.png

2. mapper

public interface UsersMapper {
    void insertUser(Users uses);
    int deleateUser(int id);
    List<Users> selectAllUser();
    Users findUser(int id);
    int updateUser(Users user);
}
<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

public interface UsersService {
    void addUser(Users users);
    int delUser(int id);
    List<Users> selAllUser();
    Users findUser(int id);
    int updUser(Users user);
}
@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

@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

    <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

<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

    <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>

发表评论

textsms
account_circle
email

Yuyy

2.实现增删查改
1. 目录 2. mapper public interface UsersMapper { void insertUser(Users uses); int deleateUser(int id); List<Users> selectAllUser(); Users findUser(i…
扫描二维码继续阅读
2020-01-02
友情链接
标签
归档
近期文章
分类
近期文章