SSM项目之在线通讯录
受疫情影响,大三下在家中度过,学习了JAVA企业级应用开发课程,我们小组耗费一个星期时间编写了SSM项目之在线通讯录项目,其中马宽对系统的帮助很大,通过这次实践,跟着他学到了很多。
项目包括:拦截器、controller类、service层、逆向工程、Ajax、js、分页、通讯录的CUED功能
运行环境:Myeclipse+MySQL+Tomcat+JDK1.8
框架技术:spring+springMVC+Mybatis
运行界面截图:
登录界面:
注册界面:
主界面:
增加界面:
删除界面:
修改界面:
查询界面:
分页界面:由于个人用户信息量较少,必须使用admin进入管理员界面才可实现分页功能
。
二:代码结构及关键代码实现
ContactCURD.java
package cn.ma.spring.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import cn.ma.spring.mapper.ContactsMapper;
import cn.ma.spring.po.Contacts;
import cn.ma.spring.po.User;
import cn.ma.spring.utils.Service;
import cn.ma.spring.vo.ContactsVO;
@Controller
@RequestMapping("/Do")
public class ContactCURD {
// 1.跳转删除页,2.希望在点击的一瞬间把所有联系人放到删除页
@RequestMapping("/toDel")
public String toDel(HttpSession session, Model m) {
// 1.拿到session中的uid
User logUser = (User) session.getAttribute("user");
// 2.通讯录初始化查询
Service service = new Service();
List<Contacts> clist = service.selectAllc(logUser.getUid());
m.addAttribute("msg", logUser);// 把session中的用户传到页面
m.addAttribute("clist", clist);// 把查询到的信息放到将要跳转的删除业务界面
return "DeleteCon";
}
// 删除业务
@RequestMapping("/Del")
public String Delete(Integer[] cid, Model m) {
for (Integer id : cid) {
Service service = new Service();
int a = service.deleteById(id);
System.out.println("编号为" + id + "》》记录被删除");
if (a > 0) {
m.addAttribute("delSucc", "删除成功");
}
}
return "DeleteCon";
}
// 跳转添加页面
@RequestMapping("/toInsert")
public String toInsert(HttpSession session,Model m) {
User u=(User) session.getAttribute("user");
m.addAttribute("msg", u);
return "insert";
}
// 添加业务
@RequestMapping("/Insert")
public String Insert(Contacts contacts, HttpSession session, Model m) {
// 1.先把session用户的uid拿出来 与前台的数据 拼装
User logedUser = (User) session.getAttribute("user");
int uid = logedUser.getUid();
contacts.setUid(uid);
System.out.println(contacts);
// 2.拼装完成后进行插入
// 根据用户名密码 来查询 登陆功能
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
// Mapper是一个接口类 里面定义了很多方法
ContactsMapper cm = (ContactsMapper) act.getBean(ContactsMapper.class);
// 这个方法要与mapper.xml中的id保持一致
int a = cm.insert(contacts);// 如果大于0插入成功
if (a > 0) {
m.addAttribute("succ", "插入记录成功");
m.addAttribute("newcon", contacts);
m.addAttribute("all", "查看全部");
}
return "insert";
}
// 刷新业务
@RequestMapping(value = "/toSel")
public String toSel(HttpSession session, Model m) {
// 1.拿到session中的uid
User logUser = (User) session.getAttribute("user");
// 2.通讯录初始化查询
Service service = new Service();
List<Contacts> clist = service.selectAllc(logUser.getUid());
m.addAttribute("msg", logUser);
m.addAttribute("clist", clist);
return "scanner";// 跳转查询
}
@RequestMapping(value = "/toSearch")
public String toSearch(Model m,HttpSession session) {
User u= (User) session.getAttribute("user");
m.addAttribute("msg",u);
return "Search";// 跳转查询
}
// 跳转查询业务
@RequestMapping(value = "/Search")
public String Search(String cname,Model m,HttpSession session) {
User u= (User) session.getAttribute("user");
m.addAttribute("msg",u);
Contacts contacts=new Contacts();
contacts.setCname(cname);
contacts.setUid(u.getUid());
//根据Cname查询 一般用户功能
Service sevice=new Service();
List<Contacts> clist =sevice.selectAllByName(contacts);
m.addAttribute("clist", clist);
return "Search";
}
@RequestMapping(value = "/SearchCon")//条件查询
public String Search(String cname,String caddress,String cphone,Model m,HttpSession session) {
User u= (User) session.getAttribute("user");
m.addAttribute("msg",u);
Contacts contacts=new Contacts();
contacts.setCaddress(caddress);
contacts.setCphone(cphone);
contacts.setCname(cname);
contacts.setUid(u.getUid());
//根据Cname查询 一般用户功能
Service sevice=new Service();
List<Contacts> clist =sevice.selectAllByCondition(contacts);
m.addAttribute("clist", clist);
return "Search";
}
//写一个ajax请求 实时查询 一般用户请求
@RequestMapping(value = "/SearchAjax")
public void SearchAjax(String cname,HttpSession session,HttpServletResponse response) throws Exception {
User u= (User) session.getAttribute("user");
Contacts contacts=new Contacts();
contacts.setCname(cname);
contacts.setUid(u.getUid());
//m.addAttribute("clist", clist);
System.out.println(cname+"ajax获取的cnme");
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
ContactsMapper um = (ContactsMapper) act.getBean(ContactsMapper.class);
List<Contacts> clist= um.selectByUidAndCname(contacts);
System.out.println(clist);
response.setCharacterEncoding("UTF-8");
response.getWriter().print(JSON.toJSONString(clist));
}
// 1.用于跳转修改页面 2.希望在跳转是同时拿出所有的数据
@RequestMapping("/toUpdateCon")
public String UpdateCon(HttpSession session, Model m) {
// 1.拿到session中的uid
User logUser = (User) session.getAttribute("user");
// 2.通讯录初始化查询
Service service = new Service();
List<Contacts> clist = service.selectAllc(logUser.getUid());
m.addAttribute("msg", logUser);// 把session中的用户传到页面
m.addAttribute("clist", clist);// 把查询到的信息放到将要跳转的删除业务界面
return "updateCon";
}
// 1.更新业务
@RequestMapping("/UpdateCon")
public String UpdateCon(ContactsVO conList,Model m) {
// 用于修改操作
List<Contacts> cts = conList.getCts();// cts要与vo保持一致
//System.out.println("观察记录"+cts);//前台选择的数据
List<Contacts> tmp = new ArrayList<Contacts>();//创建临时变量被选中的才存进去
//页面被选中的cid肯定不为空,则存入临时变量
for (Contacts contacts : cts) {
if(contacts.getCid()!=null){
tmp.add(contacts);
}
}
//System.out.println(tmp);//打印被选中的list
//把list传到前台吧
m.addAttribute("msgofupdate","更新成功您修改的记录如下");
m.addAttribute("tmp", tmp);//更新的东西放前端展示
//2.更新业务 更新被在选择的数据
Service se=new Service();
//循环更新
for (Contacts c : tmp) {
se.updateByCidSelective(c);
}
return "Objectscanner";
}
}
LogAndReg.java
package cn.ma.spring.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.ma.spring.mapper.UserMapper;
import cn.ma.spring.po.Contacts;
import cn.ma.spring.po.User;
import cn.ma.spring.utils.CreateUid;
import cn.ma.spring.utils.Service;
@Controller
@RequestMapping(value = "/LAR")
public class LogAndReg {
@RequestMapping(value = "/toLog")
public String tolog() {
return "log";// 跳转主页 == 登陆页面
}
@RequestMapping(value = "/toReg")
public String toReg() {
return "reg";// 跳转注册
}
@RequestMapping(value = "/Reg")
public String Reg(User user,Model m) {
//生成6位随机uid
CreateUid createUid=new CreateUid();
String uid=createUid.getUid();
int uuid=Integer.valueOf(uid);//转换为int
user.setUid(uuid);
//插入业务
Service service=new Service();
int a =service.addNewUser(user);
if(a>0){
m.addAttribute("user", user);
m.addAttribute("msg", "注册成功");
m.addAttribute("msg2", "你的ID为");
return "log";// 跳转自己
}else{
m.addAttribute("msg", "注册失败");
return "reg";
}
}
@RequestMapping(value = "/valLog")
public String valLog(User user, Model m, HttpSession session) {
System.out.println("前台传参==》" + user.getUsername());
// 根据用户名密码 来查询 登陆功能
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
// StuMapper是一个接口类 里面定义了很多方法
UserMapper sm = (UserMapper) act.getBean(UserMapper.class);
// 这个方法要与mapper.xml中的id保持一致
User logUser = sm.selectByUsernameAndPassword(user);
if (logUser == null) {
m.addAttribute("msg", "用户名密码错误");
return "log";
} else {
// 1.把用户存进session
session.setAttribute("user", logUser);
User uu = (User) session.getAttribute("user");
String uname = uu.getUsername();
System.out.println(uname + "正在会话中");
// 2.通讯录初始化查询 //进入后有数据 ***在这里进行分页升级
Service service = new Service();
List<Contacts> clist = service.selectAllc(logUser.getUid());
m.addAttribute("msg", logUser);
m.addAttribute("clist", clist);
return "scanner";//sacnner 作为主业面
}
}
@RequestMapping(value = "/LogOut")
public String logOut(HttpSession session) {
session.invalidate();//将当前用户登录状态释放
return "logOut";
}
}
Service.java
package cn.ma.spring.utils;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.ma.spring.mapper.ContactsMapper;
import cn.ma.spring.mapper.UserMapper;
import cn.ma.spring.po.Contacts;
import cn.ma.spring.po.User;
public class Service {
public List<Contacts> selectAllc(Integer uid){
//根据Cid查询 一般用户功能 用于查询用户自己的通讯练习人
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
// Mapper是一个接口类 里面定义了很多方法
ContactsMapper um = (ContactsMapper) act.getBean(ContactsMapper.class);
//将uid为指定值的用户都查出来
List<Contacts> clist=um.selectByUid(uid);
return clist;
}
public int AllCNumber(){
//根据Cid查询 一般用户功能 用于查询用户自己的通讯练习人
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
// Mapper是一个接口类 里面定义了很多方法
ContactsMapper um = (ContactsMapper) act.getBean(ContactsMapper.class);
//将uid为指定值的用户都查出来
int num=um.AllNumber();
return num;
}
public List<Contacts> selectAllByName(Contacts contacts){//名字 联系人
//根据Cid查询 一般用户功能 用于查询用户自己的通讯练习人
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
// Mapper是一个接口类 里面定义了很多方法
ContactsMapper cm = (ContactsMapper) act.getBean(ContactsMapper.class);
//将uid为指定值的用户都查出来
List<Contacts> clist=cm.selectByUidAndCname(contacts);
return clist;
}
public List<Contacts> selectAllByCondition(Contacts contacts){//条件查询 联系人
//根据Cid查询 一般用户功能 用于查询用户自己的通讯练习人
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
// Mapper是一个接口类 里面定义了很多方法
ContactsMapper cm = (ContactsMapper) act.getBean(ContactsMapper.class);
//将uid为指定值的用户都查出来
List<Contacts> clist=cm.selectByCondition(contacts);
return clist;
}
public int deleteById(Integer cid){
//根据id删除 一般用户功能 用于删除用户自己的通讯练习人
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
// Mapper是一个接口类 里面定义了很多方法
ContactsMapper um = (ContactsMapper) act.getBean(ContactsMapper.class);
//将uid为指定值的用户都查出来
int a= um.deleteByPrimaryKey(cid);
return a;
}
public int addNewUser(User user) {
//根据对象插入用户表
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
// Mapper是一个接口类 里面定义了很多方法
UserMapper um = (UserMapper) act.getBean(UserMapper.class);
//插入
int a=um.insert(user);
return a;
}
//更新业务
public int updateByCidSelective(Contacts record){
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
// Mapper是一个接口类 里面定义了很多方法
ContactsMapper um = (ContactsMapper) act.getBean(ContactsMapper.class);
//更新
int a=um.updateByPrimaryKeySelective(record);
return a;
}
public List<Contacts> AdminSelectALL(){
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
// Mapper是一个接口类 里面定义了很多方法
ContactsMapper um = (ContactsMapper) act.getBean(ContactsMapper.class);
//更新
List<Contacts> clist=um.selectAll();
return clist;
}
//分页
public Page<Contacts> PageSelect(Integer start,Integer size){
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
// Mapper是一个接口类 里面定义了很多方法
ContactsMapper um = (ContactsMapper) act.getBean(ContactsMapper.class);
int tal=um.AllNumber();//总记录条数
Page<Contacts> pages=new Page<Contacts>();
pages.setPage(start);//起始页
pages.setSize(size);
pages.setTotal(tal);
Contacts contacts=new Contacts();
int a=(start-1)*size;
contacts.setStart(a);
contacts.setSize(size);
List<Contacts> clist=um.selectAllPage(contacts);
pages.setRows(clist);
return pages;
}
}
AdminSearch.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="itheima" uri="http://itheima.com/common/"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>客户管理-BootCRM</title>
<!-- 引入css样式文件 -->
<!-- Bootstrap Core CSS -->
<link href="<%=basePath%>css/bootstrap.min.css" rel="stylesheet" />
<!-- MetisMenu CSS -->
<link href="<%=basePath%>css/metisMenu.min.css" rel="stylesheet" />
<!-- DataTables CSS -->
<link href="<%=basePath%>css/dataTables.bootstrap.css" rel="stylesheet" />
<!-- Custom CSS -->
<link href="<%=basePath%>css/sb-admin-2.css" rel="stylesheet" />
<!-- Custom Fonts -->
<link href="<%=basePath%>css/font-awesome.min.css" rel="stylesheet"
type="text/css" />
<link href="<%=basePath%>css/boot-crm.css" rel="stylesheet"
type="text/css" />
</head>
<body
Style="width:100%;
height: 100%;
background-image: url(images/bg2.jpg);
background-repeat: no-repeat;
background-size: 100% 100%;">
<div id="page-wrapper"width:100%;>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<font color="red">${msg.username}</font>已登陆《——》<br>
<p align="right">
<img alt="" src="images/refresh.png"> <a href="AdminIndex">刷新</a> 
<img alt="" src="images/exit.png"> <a href="LogOut">退出</a>  
</p>
<!-- 可以不用form因为写了事件 -->
<form action="Do/SearchCon" method="post">
<center>
管理员查看</a><br />
</center>
</form>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>电话</th>
<th>地址</th>
</tr>
</thead>
<tbody>
<c:forEach items="${page.rows}" var="con" varStatus="a">
<tr>
<td>${a.count}</td>
<td>${con.cname}</td>
<td>${con.cphone}</td>
<td>${con.caddress}</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="col-md-12 text-right">
<itheima:page
url="${pageContext.request.contextPath }/AdminDo/AdminIndex" />
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
</div>
<jsp:include page="/WEB-INF/jsp/down.jsp" flush="true" />
</body>
</html>