在實作登入功能之前我們來實作下使用者管理。
初學者應該對這張圖檔銘記于心~
使用者管理
1.查詢使用者資訊
将使用者表中的資料展示在頁面中
1.1 使用者業務邏輯層
接口定義
package com.bobo.service;
import java.util.List;
import com.bobo.pojo.User;
/**
* 使用者資訊
* @author 波波烤鴨
*
* [email protected]
*/
public interface IUserService {
/**
* 根據條件查詢使用者資訊
* @param user
* @return
*/
public List<User> query(User user);
/**
* 添加使用者
* @param user
* @throws Exception
*/
public void addUser(User user) throws Exception;
/**
* 修改使用者
* @param user
* @throws Exception
*/
public void updateUser(User user)throws Exception;
/**
* 删除使用者
* @param id
* @throws Exception
*/
public void deleteUser(int id)throws Exception;
}
接口實作
package com.bobo.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.bobo.mapper.UserMapper;
import com.bobo.pojo.User;
import com.bobo.pojo.UserExample;
import com.bobo.service.IUserService;
@Service
public class UserServiceImpl implements IUserService {
@Resource
private UserMapper userMapper;
@Override
public List<User> query(User user) {
UserExample example = new UserExample();
// ---
return userMapper.selectByExample(example);
}
@Override
public void addUser(User user) throws Exception {
userMapper.insertSelective(user);
}
@Override
public void updateUser(User user) throws Exception {
// TODO Auto-generated method stub
userMapper.updateByPrimaryKey(user);
}
@Override
public void deleteUser(int id) throws Exception {
// TODO Auto-generated method stub
userMapper.deleteByPrimaryKey(id);
}
}
1.2 使用者控制層
/**
* 角色 控制層
* @author 波波烤鴨
*
* [email protected]
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private IUserService userService;
@RequestMapping("/query")
public String query(User user,Model model){
List<User> list = userService.query(user);
model.addAttribute("list", list);
return "user/user";
}
}
1.3 使用者管理界面
修改left.jsp中的代碼
因為jsp檔案下檔案越來越多,是以分下目錄
RoleController跳轉頁調整下
1.4 展示資料
準備一條測試資料
因為logistics-manager-service的代碼修改了是以需要install下,别忘了,如果提示報錯就在來一次,如果還報錯請檢查。
啟動通路
通路成功!
2.添加使用者資訊
2.1控制層添加跳轉的方法
更新和修改我們放在一個jsp頁面進行,添加和修改都需要配置設定角色。
service層
/**
* 擷取添加或者修改使用者需要的資料
* 添加
* 擷取所有的角色資訊
* 修改
* 擷取所有的角色資訊及根據id查詢的使用者資訊
* @param id
* @return
*/
public void getUpdateUserInfo(Integer id,Model model);
@Resource
private RoleMapper roleMapper;
@Override
public void getUpdateUserInfo(Integer id,Model model) {
RoleExample roleExample = new RoleExample();
List<Role> roles = roleMapper.selectByExample(roleExample );
model.addAttribute("roles", roles);
}
控制層
/**
* 進入添加或者修改頁面
* @param id
* @return
*/
@RequestMapping("/userUpdate")
public String userUpdatePage(Integer id,Model model){
// 查詢添加或者更新需要的資料
userService.getUpdateUserInfo(id,model);
return "/user/userUpdate";
}
2.2點選添加完成跳轉
userUpdate.jsp使用form.html的代碼
通路後樣式失效:
2.3修改添加頁面
效果
代碼
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無标題文檔</title>
<link href="/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="place">
<span>位置:</span>
<ul class="placeul">
<li><a href="/">首頁</a></li>
<li><a href="/user/query">使用者管理</a></li>
</ul>
</div>
<div class="formbody">
<div class="formtitle">
<span>基本資訊</span>
</div>
<ul class="forminfo">
<li><label>賬号</label>
<input name="userName" type="text"
class="dfinput" />
<i>賬号不能超過30個字元</i>
</li>
<li><label>姓名</label>
<input name="realName" type="text"
class="dfinput" />
</li>
<li><label>密碼</label>
<input name="password" type="password"
class="dfinput" />
</li>
<li><label>确認密碼</label>
<input name="confirmPassword"
type="password" class="dfinput" />
</li>
<li><label>電話</label>
<input name="phone" type="text"
class="dfinput" /><i></i>
</li>
<li><label>郵箱</label>
<input name="email" type="text"
class="dfinput" /><i></i>
</li>
<li><label>配置設定角色</label>
<div style="height: 32px;line-height: 32px;">
<c:forEach items="${roles }" var="role">
<input type="checkbox" value="${role.roleId }" name="roleIds"> ${role.roleName }
</c:forEach>
</div>
</li>
<li><label> </label>
<input name="" type="submit"
class="btn" value="确認儲存" /></li>
</ul>
</div>
<div style="display: none">
<script src='http://v7.cnzz.com/stat.php?id=155540&web_id=155540'
language='JavaScript' charset='gb2312'></script>
</div>
</body>
</html>
驗證此處沒有加,可自行加上~
2.4送出資料
因為表單送出的資料有配置設定的角色資訊,單獨一個User對象并不能接收所有的資料。是以我們需要建立一個Dto對象,來儲存資料。
資料傳輸對象,即Data Transfer Object,簡稱DTO。
一個為了減少方法調用次數而在程序間傳輸資料的對象
表單域name值設定,及送出位址:
<form action="/user/saveOrUpdate">
<ul class="forminfo">
<li><label>賬号</label>
<input name="user.userName" type="text"
class="dfinput" />
<i>賬号不能超過30個字元</i>
</li>
<li><label>姓名</label>
<input name="user.realName" type="text"
class="dfinput" />
</li>
<li><label>密碼</label>
<input name="user.password" type="password"
class="dfinput" />
</li>
<li><label>确認密碼</label>
<input name="confirmPassword"
type="password" class="dfinput" />
</li>
<li><label>電話</label>
<input name="user.phone" type="text"
class="dfinput" /><i></i>
</li>
<li><label>郵箱</label>
<input name="user.email" type="text"
class="dfinput" /><i></i>
</li>
<li><label>配置設定角色</label>
<div style="height: 32px;line-height: 32px;">
<c:forEach items="${roles }" var="role">
<input type="checkbox" value="${role.roleId }" name="roleIds"> ${role.roleName }
</c:forEach>
</div>
</li>
<li><label> </label>
<input name="" type="submit"
class="btn" value="确認儲存" /></li>
</ul>
</form>
控制層接收資料:
/**
* 進入添加或者修改頁面
* @param id
* @return
* @throws Exception
*/
@RequestMapping("/saveOrUpdate")
public String saveOrUpdate(UserDto userDto) throws Exception{
userService.saveOrUpdate(userDto);
return "redirect:/user/query";
}
service接口
/**
* 儲存或修改使用者資料
* @param userDto
*/
public void saveOrUpdate(UserDto userDto)throws Exception;
@Override
public void saveOrUpdate(UserDto userDto) throws Exception {
// 擷取User對象
User user = userDto.getUser();
// 擷取關聯的角色資訊
List<Integer> roles = userDto.getRoles();
// 判斷是添加還是修改資料
if(user.getUserId()!=null && user.getUserId() > 0){
// 表示userId存在,說明是更新
}else{
// 不存在id說明是添加資料
// 先添加使用者資料 擷取生成的userId
userMapper.insert(user);
// 再儲存使用者和角色的對應關系,在一個事務中處理
if(roles!=null && roles.size() > 0){
for (Integer roleId : roles) {
userMapper.inserUserIdAndRoleId(user.getUserId(),roleId);
}
}
}
}
自動生成key
設定insert對應的插入語句。
添加inserUserIdAndRoleId實作
void inserUserIdAndRoleId(Integer userId, Integer roleId);
添加該sql語句在UserMapper.xml檔案中
<insert id="inserUserIdAndRoleId">
insert into t_user_role(user_id,role_id)values(#{param1},#{param2})
</insert>
測試
使用者和角色的關聯關系也添加成功
篇幅原因修改和删除下篇文章介紹~