未分类
2020-12-15 11:32:31
1822677238@qq.com
手机扫码查看
2020java框架教程之spring的RESTful的CRUD实现
1.创建实体类
2.创建dao
@Repository
public class UsersDao {
private static Map<Integer, User> map=null;
static{
map=new HashMap<Integer, User>();
map.put(1,new User(1,"admin","admin",25,"admin@qq.com",
new Address(1,"ah","000")));
map.put(2,new User(2,"admin2","admin2",11,"admin2@qq.com",
new Address(2,"ah2","002")));
map.put(3,new User(3,"admin3","admin3",11,"admin3@qq.com",
new Address(3,"ah3","003")));
map.put(4,new User(4,"admin4","admin4",11,"admin4@qq.com",
new Address(4,"ah4","004")));
}
private static Integer initid=5;
public void save(User user){
if (user.getId() == null) {
user.setId(initid++);
}
map.put(user.getId(),user);
}
//查询所有
public Collection<User> getAll(){
return map.values();
}
//查询单个
public User getId(Integer id){
return map.get(id);
}
//移除
public void delete(Integer id){
map.remove(id);
}
}
3.控制层
@RequestMapping("/admin")
@Controller
@SessionAttributes(value = {"user"},types = {String.class})
public class UserController {
@Autowired
private UsersDao usersDao;
/////////////RESTful风格///////////////
//查询所有
@RequestMapping(value = "/list",method = RequestMethod.GET)
public String list(Model model){
Collection<User> list = usersDao.getAll();
model.addAttribute("list",list);
return "admin/list";
}
//删除
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
public String delete(@PathVariable(value = "id") Integer id){
System.out.println("执行删除,已删除"+id);
usersDao.delete(id);
return "redirect:/admin/list";
}
//添加表单
@RequestMapping(value = "/add",method = RequestMethod.GET)
public String add(){
System.out.println("add");
return "admin/add";
}
//添加
@RequestMapping(value = "/",method = RequestMethod.POST)
public String save(User user){
System.out.println("save");
usersDao.save(user);
return "redirect:/admin/list";
}
//修改表单
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public String updateForm(@PathVariable(value = "id") Integer id,Model model){
System.out.println("edit");
User user = usersDao.getId(id);
model.addAttribute("user",user);
return "admin/updateForm";
}
//修改
@RequestMapping(value = "/",method = RequestMethod.PUT)
public String update(User user){
System.out.println("执行修改,已修改");
usersDao.save(user);
return "redirect:/admin/list";
}
}
4.查询所有表单页
<h1>用户列表</h1>
<a href="add">添加用户</a>
<table border="1">
<tr>
<th>id</th>
<th>username</th>
<th>password</th>
<th>age</th>
<th>email</th>
<th>address</th>
<th>操作</th>
</tr>
<c:forEach var="users" items="${list}">
<tr>
<td>${users.id}</td>
<td>${users.username}</td>
<td>${users.password}</td>
<td>${users.age}</td>
<td>${users.email}</td>
<td>${users.address}</td>
<td>
<a href="javascript:void(0)" onclick="deleteById(${users.id})">删除</a>
<a href="${pageContext.request.contextPath}/admin/${users.id}" onclick="updateById(${users.id})">修改</a>
</td>
</tr>
</c:forEach>
</table>
<form method="post" id="deleteForm">
<input type="text" name="_method" value="DELETE">
<button>删除</button>
</form>
<script>
function deleteById(id){
var form=document.getElementById('deleteForm');
form.action=id;
document.getElementById('deleteForm').submit();
}
function updateById(id){
document.getElementById('updateForm').submit();
}
</script>
5.添加表单页面
<form action="${pageContext.request.contextPath}/admin/" method="post">
<label>id : <input type="text" id="id" name="id"></label><br>
<label for="username">
username : <input type="text" id="username" name="username"><br>
</label>
<label for="password">
password : <input type="password" name="password" id="password"><br>
</label>
<label for="age">
age : <input type="text" name="age" id="age"><br>
</label>
<label for="email">
email : <input type="email" name="email" id="email"><br>
</label>
<label for="address.addr">
addr : <input type="text" name="address.addr" id="address.addr"><br>
</label>
<label for="address.zipCode">
zipCode : <input type="text" name="address.zipCode" id="address.zipCode"><br>
</label>
<button>submit</button>
</form>
6.修改表单页面
<form action="${pageContext.request.contextPath}/admin/" method="post">
<input type="hidden" name="_method" value="PUT">
<label for="id">id : <input type="text" id="id" name="id" value="${user.id}"></label><br>
<label for="username">
username : <input type="text" id="username" name="username" value="${user.username}"><br>
</label>
<label for="password">
password : <input type="password" name="password" id="password" value="${user.password}"><br>
</label>
<label for="age">
age : <input type="text" name="age" id="age" value="${user.age}"><br>
</label>
<label for="email">
email : <input type="email" name="email" id="email" value="${user.email}"><br>
</label>
<label for="address.addr">
addr : <input type="text" name="address.addr" id="address.addr" value="${sessionScope.user.address.addr}"><br>
</label>
<label for="address.zipCode">
zipCode : <input type="text" name="address.zipCode" id="address.zipCode" value="${user.address.zipCode}"><br>
</label>
<button>submit</button>
</form>



发表回复