001 | package com.hp.school.controller; |
002 |
003 | import java.util.HashMap; |
004 | import java.util.Map; |
005 |
006 | import org.springframework.beans.factory.annotation.Autowired; |
007 | import org.springframework.stereotype.Controller; |
008 | import org.springframework.util.StringUtils; |
009 | import org.springframework.web.bind.annotation.RequestMapping; |
010 | import org.springframework.web.bind.annotation.RequestMethod; |
011 | import org.springframework.web.bind.annotation.RequestParam; |
012 | import org.springframework.web.bind.annotation.ResponseBody; |
013 | import org.springframework.web.servlet.ModelAndView; |
014 |
015 | import com.hp.school.entity.User; |
016 | import com.hp.school.page.Page; |
017 | import com.hp.school.service.UserService; |
018 |
019 | @Controller |
020 | @RequestMapping ( "/user" ) |
021 | /** |
022 | * 完成用户的增删改查 ,分页 |
023 | * @author yuan |
024 | * |
025 | */ |
026 | public class UserController { |
027 | @Autowired |
028 | private UserService userService; |
029 | |
030 | /** |
031 | * 跳转到用户 列表jsp页面 |
032 | * @param model |
033 | * @return |
034 | */ |
035 | @RequestMapping ( "/list" ) |
036 | public ModelAndView list(ModelAndView model){ |
037 | model.setViewName( "user/user_list" ); |
038 | return model; |
039 | } |
040 | |
041 | /** |
042 | * 添加用户 |
043 | * @param user |
044 | * @return |
045 | */ |
046 | @RequestMapping (value= "/add" ,method=RequestMethod.POST) |
047 | @ResponseBody |
048 | public Map<String,String> add(User user){ |
049 | |
050 | Map<String,String> map = new HashMap<String, String>(); |
051 | if (StringUtils.isEmpty(user.getUsername())){ |
052 | map.put( "type" , "error" ); |
053 | map.put( "msg" , "用户名不能为空!" ); |
054 | return map; |
055 | } |
056 | if (StringUtils.isEmpty(user.getPassword())){ |
057 | map.put( "type" , "error" ); |
058 | map.put( "msg" , "密码不能为空!" ); |
059 | return map; |
060 | } |
061 | //TODO 访问业务层 |
062 | User existsUser = userService.findUserByUserName(user.getUsername()); |
063 | if (existsUser!= null ){ |
064 | map.put( "type" , "error" ); |
065 | map.put( "msg" , "该用户已存在!" ); |
066 | return map; |
067 | } |
068 | // 更新 /删除 /添加操作 最终 返回的是影响的行数 |
069 | int result = userService.add(user); |
070 | |
071 | if (result<= 0 ){ |
072 | map.put( "type" , "error" ); |
073 | map.put( "msg" , "用户保存失败!" ); |
074 | return map; |
075 | } |
076 | map.put( "type" , "success" ); |
077 | map.put( "msg" , "添加用户成功!" ); |
078 | return map; |
079 | } |
080 | |
081 | /** |
082 | * 获取用户列表数据 -- 包含 条件查询 分页 |
083 | * @return |
084 | */ |
085 | @RequestMapping (value= "/get_list" ,method=RequestMethod.POST) |
086 | @ResponseBody |
087 | /** |
088 | * @param username 模糊查询条件 |
089 | * @param page 分页类 |
090 | * @return |
091 | */ |
092 | public Map<String,Object> getList( |
093 | @RequestParam (name= "username" ,required= false ,defaultValue= "" ) String username, |
094 | Page page |
095 | ){ |
096 | Map<String,Object> map = new HashMap<>(); // 最终数据在这里 |
097 | // 这个map 等同于 QueryBean |
098 | Map<String,Object> queryMap = new HashMap<>(); // 是一个查询条件类 |
099 | //拼装 limit ?,? |
100 | queryMap.put( "offset" , page.getOffset()); |
101 | queryMap.put( "pageSize" , page.getRows()); |
102 | queryMap.put( "username" , "%" +username+ "%" ); |
103 | |
104 | map.put( "rows" , userService.getList(queryMap)); //比如查询的第2页显示的一个集合数据 |
105 | map.put( "total" , userService.getTotal(queryMap)); //接收总数量 |
106 | |
107 | return map; |
108 | } |
109 | |
110 | |
111 | /** |
112 | * 编辑用户 |
113 | * @param user |
114 | * @return |
115 | */ |
116 | @RequestMapping (value= "/edit" ,method=RequestMethod.POST) |
117 | @ResponseBody |
118 | public Map<String,String> editUser(User user){ |
119 | |
120 | Map<String,String> map = new HashMap<String, String>(); |
121 | if (StringUtils.isEmpty(user.getUsername())){ |
122 | map.put( "type" , "error" ); |
123 | map.put( "msg" , "用户名不能为空!" ); |
124 | return map; |
125 | } |
126 | if (StringUtils.isEmpty(user.getPassword())){ |
127 | map.put( "type" , "error" ); |
128 | map.put( "msg" , "密码不能为空!" ); |
129 | return map; |
130 | } |
131 | //TODO 访问业务层 |
132 | User existsUser = userService.findUserByUserName(user.getUsername()); |
133 | if (existsUser!= null ){ |
134 | if (user.getId()!=existsUser.getId()){ |
135 | map.put( "type" , "error" ); |
136 | map.put( "msg" , "该用户已存在!" ); |
137 | return map; |
138 | } |
139 | } |
140 | // 更新 /删除 /添加操作 最终 返回的是影响的行数 |
141 | int result = userService.edit(user); |
142 | |
143 | if (result<= 0 ){ |
144 | map.put( "type" , "error" ); |
145 | map.put( "msg" , "用户编辑失败!" ); |
146 | return map; |
147 | } |
148 | map.put( "type" , "success" ); |
149 | map.put( "msg" , "编辑用户成功!" ); |
150 | return map; |
151 | } |
152 | |
153 | |
154 | |
155 | /** |
156 | * 删除用户 |
157 | * @param user |
158 | * @return |
159 | * |
160 | * delete from user where id in (23,24,17) |
161 | */ |
162 | @RequestMapping (value= "/delete" ,method=RequestMethod.POST) |
163 | @ResponseBody |
164 | public Map<String,String> delete( |
165 | @RequestParam (name= "ids[]" ,required= true )Integer[] ids){ |
166 | Map<String,String> map = new HashMap<>(); |
167 | // ids 非空判断 可以不写 |
168 | //需将 数组id转成 23,24,17 |
169 | String idsParam= "" ; |
170 | for (Integer id : ids) { |
171 | idsParam += id+ "," ; // 23,24,17, |
172 | } |
173 | idsParam = idsParam.substring( 0 , idsParam.length()- 1 ); |
174 | // 通过业务层 调用删除方法 , 根据返回值判断 |
175 | int result = userService.delete(idsParam); |
176 | if (result<= 0 ){ |
177 | map.put( "type" , "error" ); |
178 | map.put( "msg" , "用户删除失败!" ); |
179 | return map; |
180 | } |
181 | map.put( "type" , "success" ); |
182 | map.put( "msg" , "删除用户成功!" ); |
183 | return map; |
184 | } |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | |
198 | |
199 | |
200 | |
201 | |
202 | |
203 | |
204 | |
205 | /** |
206 | * 修改用户 |
207 | * @param user |
208 | * @return |
209 | */ /* |
210 | @RequestMapping(value="/edit",method=RequestMethod.POST) |
211 | @ResponseBody |
212 | public Map<String,String> edit(User user){ |
213 | |
214 | Map<String,String> map = new HashMap<String, String>(); |
215 | if(StringUtils.isEmpty(user.getUsername())){ |
216 | map.put("type", "error"); |
217 | map.put("msg", "用户名不能为空!"); |
218 | return map; |
219 | } |
220 | if(StringUtils.isEmpty(user.getPassword())){ |
221 | map.put("type", "error"); |
222 | map.put("msg", "密码不能为空!"); |
223 | return map; |
224 | } |
225 | //TODO 访问业务层 |
226 | User existsUser = userService.findUserByUserName(user.getUsername()); |
227 | if(existsUser!=null){ |
228 | if(user.getId()!=existsUser.getId()){ // |
229 | map.put("type", "error"); |
230 | map.put("msg", "该用户已存在!"); |
231 | return map; |
232 | } |
233 | } |
234 | // 更新 /删除 /添加操作 最终 返回的是影响的行数 |
235 | int result = userService.edit(user); |
236 | |
237 | if(result<=0){ |
238 | map.put("type", "error"); |
239 | map.put("msg", "用户修改失败!"); |
240 | return map; |
241 | } |
242 | |
243 | map.put("type", "success"); |
244 | map.put("msg", "修改用户成功!"); |
245 | return map; |
246 | } |
247 | |
248 | *//** |
249 | * 删除用户 |
250 | * @param user |
251 | * @return |
252 | *//* |
253 | @RequestMapping(value="/delete",method=RequestMethod.POST) |
254 | @ResponseBody |
255 | public Map<String,String> delete(@RequestParam(name="ids[]",required=true)Long[] ids){ |
256 | |
257 | Map<String,String> map = new HashMap<String, String>(); |
258 | if(ids==null){ |
259 | map.put("type", "error"); |
260 | map.put("msg", "请选择要删除的数据!"); |
261 | return map; |
262 | } |
263 | |
264 | String idsParam = ""; |
265 | for (Long id : ids) { |
266 | idsParam += id+","; //(2,7,8,9,) |
267 | } |
268 | System.out.println("idsParam==="+idsParam); |
269 | //去掉最后一个逗号 |
270 | idsParam = idsParam.substring(0, idsParam.length()-1); |
271 | |
272 | int result = userService.delete(idsParam); |
273 | if(result<=0){ |
274 | map.put("type", "error"); |
275 | map.put("msg", "删除失败!"); |
276 | return map; |
277 | } |
278 | |
279 | map.put("type", "success"); |
280 | map.put("msg", "删除成功!"); |
281 | return map; |
282 | }*/ |
283 | } |

9632148963 LV1
2024年12月10日
skook7 LV2
2024年10月30日
hongdongdong LV14
2024年6月18日
潘潘123456 LV2
2023年12月30日
uni-code_0123 LV1
2023年8月4日
douhongwen LV1
2023年7月21日
ice_candy LV1
2023年6月19日
493240689 LV3
2023年6月3日
微信网友_6469820124057600 LV6
2023年5月30日
liuchang183 LV5
2023年4月22日

甜心冰淇淋 LV4
6月15日
微信网友_7520905278033920 LV1
5月22日
xianyu091012 LV5
2024年12月26日
571818771 LV3
2024年12月16日
84126415 LV2
2024年12月10日
565236523
2024年12月10日
暂无贡献等级
9632148963 LV1
2024年12月10日
moxiao
2024年12月3日
暂无贡献等级
asdfgh112
2024年7月4日
暂无贡献等级
时光海 LV2
2024年6月30日