package com.dxh.pojo; public class Users { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
package com.dxh.mapper; import com.dxh.pojo.Users; public interface UsersMapper { void insertUser(Users users); } <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dxh.mapper.UsersMapper"> <!-- 在properties文件中配置过别名了,所以parameterType不需要写Users的包的名称了 --> <insert id="insertUser" parameterType="Users"> insert into users(name,age) values (#{name},#{age}) </insert> </mapper>
package com.dxh.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.dxh.mapper.UsersMapper; import com.dxh.pojo.Users; import com.dxh.service.UsersService; @Service @Transactional public class UserServiceImpl implements UsersService{ @Autowired private UsersMapper usersMapper; @Override public void addUser(Users users) { this.usersMapper.insertUser(users); } } package com.dxh.service; import com.dxh.pojo.Users; public interface UsersService { void addUser(Users users); }
package com.dxh.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import com.dxh.pojo.Users; import com.dxh.service.UsersService; @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) { this.usersService.addUser(users); return "ok"; } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>AddUser</title> </head> <body> <form th:action="@{/users/addUser}" method="post"> userName: <input type="text" name="name"/></br> userAge: <input type="text" name="age"/></br> <input type="submit" value="SUBMIT"> </form> </body> </html> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>OK</title> </head> <body> addUser Success! </body> </html>
package com.dxh; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.dxh.mapper") //用于扫描mybatis的Mapper接口 public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
List<Users> selectUserAll(); <select id="selectUserAll" resultType="Users"> SELECT id,name,age from users </select>
List<Users> findUserAll(); @Override public List<Users> findUserAll() { return this.usersMapper.selectUserAll(); }
/** * 查询全部用户 */ @RequestMapping("/findUserAll") public String findUserAll(Model model) { List<Users> list = this.usersService.findUserAll(); model.addAttribute("list",list); return "showUsers"; }
在src/main/resources/templates/showUsers.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>展示用户数据</title> </head> <body> <table border="1" style="width:300px"> <tr> <th>ID</th> <th>名称</th> <th>年龄</th> </tr> <tr th:each="user : ${list}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> </tr> </table> </body> </html>
http://localhost:8080/users/findUserAll
分为两部分,一是数据回显,二是提交页面
Users selectUsersById(Integer id); <select id="selectUsersById" resultType="Users" parameterType="int"> SELECT id,name,age from users where id = #{id} </select>
Users findUserById(Integer id); @Override public Users findUserById(Integer id) { return this.usersMapper.selectUsersById(id); }
/** * 根据用户id查询用户 */ @RequestMapping("/findUserById") public String findUserById(Integer id ,Model model) { Users user = this.usersService.findUserById(id); model.addAttribute("user",user); return "updatePage"; }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form th:action="@{/users/editUser}" method="post"> <input type="hidden" name="id" th:field="${user.id}"> userName: <input type="text" name="name" th:field="${user.name}"/></br> userAge: <input type="text" name="age" th:field="${user.age}"/></br> <input type="submit" value="SUBMIT"> </form> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>展示用户数据</title> </head> <body> <table border="1" style="width:300px"> <tr> <th>ID</th> <th>名称</th> <th>年龄</th> <th>操作</th> </tr> <tr th:each="user : ${list}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> <td> <a th:href="@{/users/findUserById(id=${user.id})}">更新用户</a> </td> </tr> </table> </body> </html>
void updateUser(Users users); <update id="updateUser" parameterType="Users"> update users set name=#{name} , age=#{age} where id=#{id} </update>
void updateUser(Users users); @Override public void updateUser(Users users) { this.usersMapper.updateUser(users); }
@RequestMapping("/editUser") public String editUser(Users users,Model model) { this.usersService.updateUser(users); return "ok"; }
void deleteUserByid(Integer id); <delete id="deleteUserByid"> delete from users where id = #{id} </delete>
void deleteUserById(Integer id); @Override public void deleteUserById(Integer id) { this.usersMapper.deleteUserByid(id); }
/** * 删除用户 */ @RequestMapping("/delUser") public String delUser(Integer id) { this.usersService.deleteUserById(id); return "redirect:/users/findUserAll"; }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>展示用户数据</title> </head> <body> <table border="1" style="width:300px"> <tr> <th>ID</th> <th>名称</th> <th>年龄</th> <th>操作</th> </tr> <tr th:each="user : ${list}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> <td> <a th:href="@{/users/findUserById(id=${user.id})}">更新用户</a></br> <a th:href="@{/users/delUser(id=${user.id})}">删除用户</a> </td> </tr> </table> </body> </html>
原文链接:https://www.cnblogs.com/net-safe/p/13529161.html