环境配置:
Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)
项目技术:
JSP + C3P0+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload等等
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
protected WebResponse webResponse;
@Resource
protected IUserService userService;
@Resource
protected IMyMusicService MyMusicService;
@Autowired
UserService userService2;
String newName = null;
// 登录功能
@RequestMapping(value = "/loginPage", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse loginCon(HttpServletRequest request, HttpSession session) {
// 取参数的方法,对应登录表单中的用户名name="user_name"
String user_name = request.getParameter("user_name");
String user_password = request.getParameter("user_password");
// 调用mapper层的登录的方法,从数据库中匹配用户名和密码,并放回用户名
String tname = userService2.login(user_name, user_password);
String user_Id = userService2.getUserById(user_name, user_password);
// newUserId = user_Id; //修改密码用的
int userId = 0;
try {
// 判断字符串是否是数字,并且抛出异常
// boolean NotisNum
// =(user_Id.equals(null)||user_Id.equals("")||user_Id.equals("null"));
boolean NotisNum = (user_Id.equals("null"));
// System.out.println(NotisNum);
if (!NotisNum) {
userId = Integer.parseInt(user_Id);
}
} catch (Exception e) {
}
//
session.setAttribute("tname", tname);
session.setAttribute("userId", userId);
Object data = null;
String statusMsg = "";
Integer statusCode = 200;
Map paramMap = new HashMap();
paramMap.put("user_name", user_name);
paramMap.put("user_password", user_password);
data = paramMap;
User user = new User();
user.setUser_id(userId);
System.out.println("登录的id:" + user.getUser_id());
System.out.println("登录的用户名:" + session.getAttribute(tname));
System.out.println("前端,用户名:" + user_name + " 密码:" + user_password);
System.out.println("根据前端在数据库中查找到的用户名:" + tname + " 用户的id:" + userId);
// 用户名和密码匹配不成功,则返回的用户名为空
if (tname == null) {
System.out.println("用户不存在");
System.out.println();
statusMsg = "用户或密码错误!";
statusCode = 201;
return webResponse.getWebResponse(statusCode, statusMsg, data);
} else {
statusCode = 200;
webResponse.setStatusCode(statusCode);
System.out.println("状态码:" + webResponse.getStatusCode());
System.out.println("登录成功了");
System.out.println();
return webResponse.getWebResponseUserId(statusCode, statusMsg, data, userId);
}
}
// 更改密码 更改密码 更改密码
@RequestMapping(value = "/resetUserPassword", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse resetUserPassword(HttpServletRequest request, HttpSession session) {
Object data = null;
String statusMsg = "";
Integer statusCode = 200;
int uId = 0;
// 取参数的方法,对应登录表单中的用户名
String user_name = request.getParameter("user_name");
String newUser_password = request.getParameter("newUser_password");
try {
uId = (int) this.userService2.resetPassword(user_name, newUser_password);
System.out.println("修改密码返回的id:" + uId);
} catch (Exception e) {
}
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
// 用户注册
@RequestMapping(value = "/addOrEditUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse addOrEditTest(HttpServletRequest request, HttpServletResponse response, HttpSession session,
String user_id, @RequestParam(required = false) String user_name,
@RequestParam(required = false) String user_password) {
Object data = null;
String statusMsg = "用户名已存在,请重新注册!";
Integer statusCode = 201;
String rearchName = null;
try {
rearchName = this.userService.rearchUserName(user_name);
if (!(rearchName.equals("null"))) {
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
} catch (Exception e) {
}
System.out.println("判断用户是否重复:" + rearchName);
if (user_id == null || user_id.length() == 0) {
return this.addUser(request, response, session, user_name, user_password);
} else {
return this.editUser(request, response, session, user_id, user_name, user_password);
}
}
// 添加用户
@RequestMapping(value = "/addUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse addUser(HttpServletRequest request, HttpServletResponse response, HttpSession session,
String user_name, String user_password) {
Object data = null;
String statusMsg = "";
Integer statusCode = 200;
Map paramMap = new HashMap();
paramMap.put("user_name", user_name);
paramMap.put("user_password", user_password);
data = paramMap;
if (user_name == null || "".equals(user_name.trim()) || user_password == null
|| "".equals(user_password.trim())) {
statusMsg = " 参数为空错误!!!!";
statusCode = 201;
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
if (user_name.length() > 255 || user_password.length() > 65535) {
statusMsg = " 参数长度过长错误!!!";
statusCode = 201;
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
User user = new User();
boolean isAdd = true;
return this.addOrEditUser(request, response, session, data, user, user_name, user_password, isAdd);
}
@RequestMapping(value = "/editUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse editUser(HttpServletRequest request, HttpServletResponse response, HttpSession session,
String user_id, @RequestParam(required = false) String user_name,
@RequestParam(required = false) String user_password) {
Object data = null;
String statusMsg = "";
Integer statusCode = 200;
Map paramMap = new HashMap();
paramMap.put("user_id", user_id);
paramMap.put("user_name", user_name);
paramMap.put("user_password", user_password);
data = paramMap;
if (user_id == null || "".equals(user_id.trim())) {
statusMsg = "未获得主键参数错误!!!";
statusCode = 201;
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
Integer user_idNumeri = user_id.matches("^[0-9]*$") ? Integer.parseInt(user_id) : 0;
if (user_idNumeri == 0) {
statusMsg = "主键不为数字错误!!!";
statusCode = 201;
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
User userVo = this.userService.getById(user_idNumeri);
User user = new User();
BeanUtils.copyProperties(userVo, user); // 浅复制,重要
boolean isAdd = false;
return this.addOrEditUser(request, response, session, data, user, user_name, user_password, isAdd);
}
/*
*
*/
private WebResponse addOrEditUser(HttpServletRequest request, HttpServletResponse response, HttpSession session,
Object data, User user, String user_name, String user_password, boolean isAdd) {
String statusMsg = "";
Integer statusCode = 200;
if (user_name != null && !("".equals(user_name.trim()))) {
if (user_name.length() > 255) {
statusMsg = " 参数长度过长错误,testName";
statusCode = 201;
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
// 重要,建立数据,以便后期数据库能调用数据
user.setUser_name(user_name); // 建立用户名
user.setUser_password(user_password); // 建立密码
}
if (user_password != null && !("".equals(user_password.trim()))) {
if (user_password.length() > 65535) {
statusMsg = " 参数长度过长错误,info";
statusCode = 201;
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
}
if (isAdd) {
// 插入语句,插入数据库,重要
this.userService.insert(user);
if (user.getUser_id() > 0) {
statusMsg = "成功插入!!!";
} else {
statusCode = 202;
statusMsg = "insert false";
}
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
int num = this.userService.update(user);
if (num > 0) {
statusMsg = "成功修改!!!";
} else {
statusCode = 202;
statusMsg = "update false";
}
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
@RequestMapping(value = "/getUserById", produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse getUserById(String user_id) {
Object data = user_id;
Integer statusCode = 200;
String statusMsg = "";
if (user_id == null || user_id.length() == 0 || user_id.length() > 11) {
statusMsg = "参数为空或参数过长错误!!!";
statusCode = 201;
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
Integer user_idNumNumeri = user_id.matches("^[0-9]*$") ? Integer.parseInt(user_id) : 0;
if (user_idNumNumeri == 0) {
statusMsg = "参数数字型错误!!!";
statusCode = 201;
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
User userVo = this.userService.getById(user_idNumNumeri);
if (userVo != null && userVo.getUser_id() > 0) {
data = userVo;
statusMsg = "获取单条数据成功!!!";
} else {
statusCode = 202;
statusMsg = "no record!!!";
}
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
@RequestMapping(value = "/getOneUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse getOneTest(@RequestParam(defaultValue = "正常", required = false) String tbStatus) {
LinkedHashMap condition = new LinkedHashMap();
condition.put("tb_status='" + tbStatus + "'", "");
User userVo = this.userService.getOne(condition);
Object data = null;
String statusMsg = "";
if (userVo != null && userVo.getUser_id() > 0) {
data = userVo;
statusMsg = "根据条件获取单条数据成功!!!";
} else {
statusMsg = "no record!!!";
}
return webResponse.getWebResponse(statusMsg, data);
}
@RequestMapping(value = "/getUserList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse getTestList(HttpServletRequest request, HttpServletResponse response, HttpSession session,
@RequestParam(defaultValue = "1", required = false) Integer pageNo,
@RequestParam(defaultValue = "10", required = false) Integer pageSize,
@RequestParam(defaultValue = "正常", required = false) String tbStatus,
@RequestParam(required = false) String keyword,
@RequestParam(defaultValue = "test_id", required = false) String order,
@RequestParam(defaultValue = "desc", required = false) String desc) {
Object data = null;
String statusMsg = "";
int statusCode = 200;
LinkedHashMap condition = new LinkedHashMap();
/*
* if (tbStatus != null && tbStatus.length() > 0) { condition.put("tb_status='"
* + tbStatus + "'", "and"); }
*/
if (keyword != null && keyword.length() > 0) {
StringBuffer buf = new StringBuffer();
buf.append("(");
buf.append("test_name like '%").append(keyword).append("%'");
buf.append(" or ");
buf.append("info like '%").append(keyword).append("%'");
buf.append(" or ");
buf.append("other like '%").append(keyword).append("%'");
buf.append(")");
condition.put(buf.toString(), "and");
}
String field = null;
if (condition.size() > 0) {
condition.put(condition.entrySet().iterator().next().getKey(), "");
}
int count = this.userService.getCount(condition, field);
if (order != null && order.length() > 0 & "desc".equals(desc)) {
order = order + " desc";
}
List list = this.userService.getList(condition, pageNo, pageSize, order, field);
Map
@Controller
@RequestMapping("/myMusic")
public class MyMusicController {
@Autowired
protected WebResponse webResponse;
@Resource
protected IMyMusicService myMusicService;
// @Autowired
// MyMusicService myMusicService2;
// 歌曲收藏
@RequestMapping(value = "/addMusicCollect", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse addMusicCollect(HttpServletRequest request, HttpServletResponse response, HttpSession session,
@RequestParam(required = false) String user_name) {
WebResponse webResponse = new WebResponse();
MyMusic myMusic = new MyMusic();
System.out.println("接收到的用户名:" + user_name);
Integer statusCode = 200;
// 数据库插入语句,对应xml文件的insert
this.myMusicService.insert(myMusic);
webResponse.setStatusCode(statusCode);
return webResponse;
}
// 从数据库中获取歌曲数据,在我的音乐中显示
@RequestMapping(value = "/getMyMusicList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse getMyMusicList(HttpServletRequest request, HttpServletResponse response, HttpSession session,
@RequestParam(defaultValue = "1", required = false) Integer pageNo,
@RequestParam(defaultValue = "10", required = false) Integer pageSize,
@RequestParam(defaultValue = "正常", required = false) String tbStatus,
@RequestParam(required = false) String keyword,
@RequestParam(defaultValue = "ml_id", required = false) String order,
@RequestParam(defaultValue = "desc", required = false) String desc,
@RequestParam(required = false) String user_name, @RequestParam(required = false) String user_password
) {
// System.out.println("我的音乐显示列表前收藏的歌曲id:" + song_id);
String user_Id = null;
try {
user_Id = myMusicService.getUserById(user_name, user_password);
} catch (Exception e) {
}
int userId = 0;
try {
// 判断字符串是否是数字,并且抛出异常
boolean NotisNum = (user_Id.equals("null"));
// System.out.println(NotisNum);
if (!NotisNum) {
userId = Integer.parseInt(user_Id);
}
} catch (Exception e) {
}
System.out.println("我的音乐显示列表前用户id:" + userId);
Object data = null;
String statusMsg = "";
int statusCode = 200;
LinkedHashMap condition = new LinkedHashMap();
/*
* if (tbStatus != null && tbStatus.length() > 0) { condition.put("tb_status='"
* + tbStatus + "'", "and"); }
*/
if (keyword != null && keyword.length() > 0) {
StringBuffer buf = new StringBuffer();
buf.append("(");
buf.append("test_name like '%").append(keyword).append("%'");
buf.append(" or ");
buf.append("info like '%").append(keyword).append("%'");
buf.append(" or ");
buf.append("other like '%").append(keyword).append("%'");
buf.append(")");
condition.put(buf.toString(), "and");
}
String field = null;
if (condition.size() > 0) {
condition.put(condition.entrySet().iterator().next().getKey(), "");
}
// int count = this.myMusicService.getCount(condition, field);
if (order != null && order.length() > 0 & "desc".equals(desc)) {
order = order + " desc";
}
// List list = this.myMusicService.getList(condition, pageNo, pageSize,
// order, field);
try {
List list = this.myMusicService.getMyMusicList(userId);
Map map = new HashMap();
// map.put("total", count);
int size = list.size();
if (size > 0) {
List listFont = new ArrayList();
MyMusic vo;
MyMusic voFont = new MyMusic();
for (int i = 0; i < size; i++) {
vo = list.get(i);
// 通过java反射将类中当前属性字段对应的内容复制到另外一个类中
BeanUtils.copyProperties(vo, voFont);
listFont.add(voFont);
voFont = new MyMusic();
}
map.put("list", listFont);
data = map;
statusMsg = "根据条件获取分页数据成功!!!";
} else {
map.put("list", list);
data = map;
statusCode = 202;
statusMsg = "no record!!!";
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
}catch (Exception e){
}
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
// 删除音乐
@RequestMapping(value = "/deleteMyMusic", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse deleteMyMusic(HttpServletRequest request, HttpServletResponse response, HttpSession session,
@RequestParam(required = false) Integer user_id, @RequestParam(required = false) Integer song_id) {
WebResponse webResponse = new WebResponse();
MyMusic myMusic = new MyMusic();
System.out.println("删除音乐前的用户名id:" + user_id);
Object data = null;
String statusMsg = "";
int statusCode = 201;
int del = 0;
if (user_id == null){
del = this.myMusicService.deleteMyMusic(song_id, 0);
}else {
del = this.myMusicService.deleteMyMusic(song_id, user_id);
}
if (del > 0) {
statusCode = 200;
}
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
}
@Controller
@RequestMapping("/musicLink")
public class MusicLinkController {
@Autowired
protected WebResponse webResponse;
@Resource
protected IMusicLinkService musicLinkService;
@Resource
protected MusicLinkService musicLinkService2;
/**
* //常用注解快速解释:
* @Controller :用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。
* 分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping 注解。@Controller
* 只是定义了一个控制器类,而使用@RequestMapping 注解的方法才是真正处理请求的处理器。
*
* @RequestMapping :是一个用来处理请求地址映射的注解,可用于类或方法上。
* 用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径;
* 用于方法上,表示在类的父路径下追加方法上注解中的地址将会访问到该方法
*
* @Autowired:@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。
*
*
* @Resource:@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。
*
*
* @Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文(ResponseBody)中,
* 一般在异步获取数据时使用,通常是在使用 @RequestMapping 后,返回值通常解析为跳转路径,
* 加上 @Responsebody 后返回结果不会被解析为跳转路径,而是直接写入HTTP 响应正文中。
* 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
*
*
*
*
* @RequestParam(value="aa" required=false)
* 1.可以对传入参数指定参数名
*
* // 下面的对传入参数指定为aa,如果前端不传aa参数名,会报错
* @RequestParam(value="aa") String inputStr
*
* 2、可以通过required=false或者true来要求@RequestParam配置的前端参数是否一定要传
*
* 3、如果@requestParam注解的参数是int类型,并且required=false,此时如果不传参数的话,会报错。原因是,required=false时,不传参数的话,
* 会给参数赋值null,这样就会把null赋值给了int,因此会报错。
*
*
*
*/
// 从数据库中获取歌曲数据,在榜单中显示
@RequestMapping(value = "/getMusicLinkList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse getMusicLinkList(HttpServletRequest request, HttpServletResponse response, HttpSession session,
@RequestParam(defaultValue = "1", required = false) Integer pageNo,
@RequestParam(defaultValue = "30", required = false) Integer pageSize,
@RequestParam(defaultValue = "正常", required = false) String tbStatus,
@RequestParam(required = false) String keyword,
@RequestParam(defaultValue = "ml_id", required = false) String order,
@RequestParam(defaultValue = "desc", required = false) String desc) {
Object data = null;
String statusMsg = "";
int statusCode = 200;
LinkedHashMap condition = new LinkedHashMap();
/*
* if (tbStatus != null && tbStatus.length() > 0) { condition.put("tb_status='"
* + tbStatus + "'", "and"); }
*/
if (keyword != null && keyword.length() > 0) {
StringBuffer buf = new StringBuffer();
buf.append("(");
buf.append("test_name like '%").append(keyword).append("%'");
buf.append(" or ");
buf.append("info like '%").append(keyword).append("%'");
buf.append(" or ");
buf.append("other like '%").append(keyword).append("%'");
buf.append(")");
condition.put(buf.toString(), "and");
}
String field = null;
if (condition.size() > 0) {
condition.put(condition.entrySet().iterator().next().getKey(), "");
}
int count = this.musicLinkService.getCount(condition, field);
if (order != null && order.length() > 0 & "desc".equals(desc)) {
order = order + " desc";
}
// 从数据库中获取数据,并把对象的结果集存到list列表中
List list = this.musicLinkService.getList(condition, pageNo, pageSize, order, field);
Map map = new HashMap();
map.put("total", count);
// 如果数据库有15条音乐,则list中有15个对象,则size的大小为15
int size = list.size();
if (size > 0) {
List listFont = new ArrayList();
MusicLink vo;
MusicLink voFont = new MusicLink();
// 循环将获取到的对象及结构属性克隆到一个新的voFont对象中,并保存到动态数组中
for (int i = 0; i < size; i++) {
vo = list.get(i);
// 通过java反射将类中当前属性字段对应的内容复制到另外一个类中
BeanUtils.copyProperties(vo, voFont);
listFont.add(voFont);
voFont = new MusicLink();
}
map.put("list", listFont);
// data是一个map对象
data = map;
statusMsg = "根据条件获取分页数据成功!!!";
} else {
map.put("list", list);
data = map;
statusCode = 202;
statusMsg = "no record!!!";
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
// 歌曲搜索功能
@RequestMapping(value = "/getSongRearch", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse getSongRearch(HttpServletRequest request, HttpServletResponse response, HttpSession session,
@RequestParam(required = false) String songName) {
Object data = null;
String statusMsg = "";
int statusCode = 200;
// 调用Mapper层的songRearch方法,进行数据库的操作
List list = this.musicLinkService2.songRearch(songName);
int count = list.size();
System.out.println();
System.out.println("搜索到的歌曲数:" + count);
System.out.println("结束");
Map map = new HashMap();
map.put("total", count);
int size = list.size();
if (size > 0) {
List listFont = new ArrayList();
MusicLink vo;
MusicLink voFont = new MusicLink();
for (int i = 0; i < size; i++) {
vo = list.get(i);
BeanUtils.copyProperties(vo, voFont);
listFont.add(voFont);
voFont = new MusicLink();
}
map.put("list", listFont);
data = map;
// for (int i = 0; i < map.size(); i++) {
// System.out.println(map.get(listFont).toString());
// }
//
statusMsg = "根据条件获取分页数据成功!!!";
} else {
map.put("list", list);
data = map;
statusCode = 202;
statusMsg = "no record!!!";
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
// 歌曲收藏
@RequestMapping(value = "/addMusicCollect", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public WebResponse addMusicCollect(HttpServletRequest request, HttpServletResponse response, HttpSession session,
@RequestParam(required = false) int song_id, @RequestParam(required = false) String user_name,
@RequestParam(required = false) String user_password, @RequestParam(required = false) String songName) {
WebResponse webResponse = new WebResponse();
MusicLink musicLink = new MusicLink();
User user = new User();
System.out.println("歌曲id:" + song_id + " 用户名:" + user_name + " 户用密码:" + user_password);
Object data = null;
String statusMsg = "";
int statusCode = 200;
String user_Id = null;
try {
user_Id = musicLinkService2.getUserId(user_name, user_password);
} catch (Exception e) {
}
int userId = 0;
try {
// 判断字符串是否是数字,并且抛出异常
boolean NotisNum = (user_Id.equals("null"));
// System.out.println(NotisNum);
if (!NotisNum) {
userId = Integer.parseInt(user_Id);
}
} catch (Exception e) {
}
System.out.println("歌曲名:" + songName);
String jSong = this.musicLinkService.judgeSong(songName, userId);
int my_Id = 0;
try {
// 判断字符串是否是数字,并且抛出异常
// boolean NotisNum
// =(user_Id.equals(null)||user_Id.equals("")||user_Id.equals("null"));
boolean NotisNum = (jSong.equals("null"));
if (!NotisNum) {
my_Id = Integer.parseInt(jSong);
}
} catch (Exception e) {
}
if (my_Id > 0) {
statusCode = 201;
statusMsg = "已收藏,请不要重复收藏!";
} else {
this.musicLinkService.insertSongRearch(song_id, userId);
}
System.out.println("收藏歌曲的用户id:" + userId);
return webResponse.getWebResponse(statusCode, statusMsg, data);
}
}