客户关系管理系统
导入原型(只有页面,但没有功能的一个项目,功能都是直接跳转)
只加入src和WebRoot 带点的是Eclipse维护项目的 也就是包含jar包之类的路径 与个人所用的可能不同 很容易出错
功能分析
添加用户
查询所有用户
编译客户
加载这个客户到表单中显示
修改客户
删除客户
多条件组合查询
创建表
创建包 公司名、项目名、分层
my.cstm.domain:Customer 与表单和t_customer表对应
my.cstm.dao:CustomerDao
my.cstm.service:CustomerService:没有业务,不存在也可以
my.web.servlet:CustomerServlet
导包
mysql驱动
c3p0两个 一个配置文件
dbutils
自己的工具jdbcUtils
添加客户
add.jsp->CustomerServlet#add()->显示添加成功
top.jsp(查询客户)->customerServlet->list.jsp(循环显示)
编辑客户
编辑分为两步
1、通过cid查询
list.jsp(编辑连接)->CustomerServlet#preEdit()->edit.jsp(把查询出的结果显示到表单中)
edit.jsp(表单页面)->CustomerServlet#edit()->msg.jsp(显示成功信息)
删除用户
list.jsp(删除连接)->CustomerServlet#delete()->msg.jsp
多条件组合查询
query.jsp->CustomerServlet#query()->list.jsp
1、功能
添加、修改、删除、查看
CREATE TABLE t_customer(
cid CHAR(32) PRIMARY KEY,
cname VARCHAR(40) NOT NULL,
gender VARCHAR(6) NOT NULL,
birthday CHAR(10),
cellphone VARCHAR(15) NOT NULL,
email VARCHAR(40),
description VARCHAR(500)
);
index.jsp
<body>
<jsp:forward page="/frame1.jsp"></jsp:forward>
</body>
frame1.jsp
<frameset rows="20%,*">
<frame src="<c:url value='/top.jsp'/>" name="top"/>
<frame src="<c:url value='/welcome.jsp'/>" name="main"/>
</frameset>
top.jsp
<!-- 作用是为本页面所有的表单和超链接指定内容显示的框架 -->
<base target="main">
<body style="text-align:center">
<h1>客户管理系统</h1>
<a href="<c:url value='/add.jsp'/>" >添加用户</a>
<a href="<c:url value='/CustomerServlet?method=findAll'/>" >查询用户</a>
<a href="<c:url value='/query.jsp'/>" >高级搜索</a>
</body>
welcome.jsp
<h1>欢迎</h1><br>
add.jsp
<%-- <script type="text/javascript" src="<c:url value='/jquery-ui.js'/>"></script>
<script type="text/javascript" src="<c:url value='/jquery-3.3.1.js'/>"></script> --%>
<!-- <script type="text/javascript">
$(function(){
$("#birthday").datepick({dateFormat:"yy-mm-dd"});
});
function add(){
$(".error").text("");
var bool = true;
if(!$(":text[name=cname]").val()){
$("#cnameError").text("客户名称不能为空空");
bool=false;
}
if(!$("#male").attr("checked")&& !$("#females").attr("checked")){
$("#genderError").text("客户性别不能为空空");
bool=false;
}
/* if(!$(":text[name=email]").val()){
$("#emailError").text("email不能为空空");
bool.false;
}
if(!$(":text[name=cellphone]").val()){
$("#cellphoneError").text("手机不能为空空");
bool.false;
} */
if(bool){
$("form").submit();
}
};
</script> -->
<style type="text/css">
.error{color:red;}
</style>
</head>
<body>
<h3 align="center">添加客户</h3>
<form method="post" action="<c:url value='/CustomerServlet'/>" >
<!-- 向Servlet传递一个名为method的参数,其值表示要调用servlet的那个方法 隐藏起来不让用户看见-->
<input type="hidden" name="method" value="add"/>
<table border="0" align="center" width="40%" style="margin-left: 100px;">
<tr>
<td width="100px">客户名称</td>
<td width="40%">
<input type="text" name="cname"/>
</td>
<td align="left">
<label id="cnameError" class="error"> </label>
</td>
</tr>
<tr>
<td>客户性别</td>
<td>
<input type="radio" name="gender" value="男" id="male"/>
<label for="male">男</label>
<input type="radio" name="gender" value="女" id="female"/>
<label for="female">女</label>
</td>
<td>
<label id="genderError" class="error"> </label>
</td>
</tr>
<!-- <tr>
<td>客户生日</td>
<td>
<input type="text" name="birthday" id="birthday" readonly="readonly"/>
</td>
<td>
<label id="birthdayError" class="error"> </label>
</td>
</tr> -->
<!-- <tr>
<td>手机</td>
<td>
<input type="text" name="cellphone" />
</td>
<td>
<label id="cellphoneError" class="error"> </label>
</td>
</tr> -->
</table>
<input type="submit" value="提交" align="center" style="margin-left: 100px;"/>
</form>
</body>
query.jsp
<body>
<form action="<c:url value='/CustomerServlet'/>" method="post">
<input type="hidden" name="method" value="query" />
<table border="0" align="center" width="40%" style="margin-left:100px">
<tr>
<td width="100px">客户名称</td>
<td width ="40%" >
<input type="text" name="cname"/>
</td>
</tr>
<tr>
<td >客户性别</td>
<td>
<select name="gender">
<option value="男">男</option>
<option value="女">女</option>
</select>
</td>
</tr>
</table>
<input type="submit" value="提交"/>
</form>>
</body>
list.jsp
<body>
<h3 align="center">客户列表</h3>
<table border="1" width="70%" align="center">
<tr>
<th>客户姓名</th>
<th>性别</th>
</tr>
<c:forEach items="${requestScope.cstmList}" var="cstm">
<tr>
<td>${cstm.cname }</td>
<td>${cstm.gender }</td>
<td>
<a href="<c:url value='/CustomerServlet?method=preEdit&cid=${cstm.cid }'/>">编辑</a>
<a href="<c:url value='/msg.jsp'/>">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
dao
public class CustomerDao {
private QueryRunner qr = new TxQueryRunner();
/**
* 添加客户
*/
public void add(Customer c){
try {
String sql = "insert into t_customer values(?,?,?)";
Object[] params = {c.getCid(),c.getCname(),c.getGender(),
};
qr.update(sql,params);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 查询所有
* @param c
* @return
*/
public List<Customer> findAll(){
try {
String sql = "select * from t_customer";
return qr.query(sql, new BeanListHandler<Customer>(Customer.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 加载客户
* @param cid
* @return
*/
public Customer load(String cid) {
try {
String sql = "select * from t_customer where cid=?";
return qr.query(sql, new BeanHandler<Customer>(Customer.class), cid);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 编辑客户
* @param c
*/
public void edit(Customer c) {
try {
String sql="update t_customer set cname=?,gender=? where cid=?";
Object[] params = {c.getCname(),c.getGender(),c.getCid()
};
qr.update(sql,params);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 多条件组合查询
* @param criteria
* @return
*/
public List<Customer> query(Customer criteria) {
try{
/*
* 1、给出sql模版
* 2、给出参数
* 3、调用query方法,使用结果集处理器:BeanListHandler
*/
/*
*一、给出sql模版
*二、给出参数
*/
/*
* 1、给出sql语句的前半部 用废条件占where 可以在以后直接用and
*/
StringBuilder sql = new StringBuilder("select * from t_customer where 1=1 ");
/*
* 2、判断条件,完成向sql中追加where子句
*/
/*
* 3、创建一个ArrayList,用来装载参数值
*/
List<Object> params = new ArrayList<Object>();
String cname = criteria.getCname();
if(cname!=null && !cname.trim().isEmpty()){
//模糊查询 更全面
sql.append("and cname like ?");
params.add("%"+cname+"%");
}
String gender = criteria.getGender();
if(cname!=null && !gender.trim().isEmpty()){
sql.append("and gender=?");
params.add(gender);
}
/*
* 三、执行query
*/
return qr.query(sql.toString(),
new BeanListHandler<Customer>(Customer.class),
params.toArray());
} catch(SQLException e){
throw new RuntimeException(e);
}
}
}
service
/**
* 业务层
* @author Administrator
*
*/
public class CustomerService {
private CustomerDao customerDao = new CustomerDao();
/**
* 添加客户
* @param c
*/
public void add(Customer c){
customerDao.add(c);
}
public List<Customer> finaAll(){
return customerDao.findAll();
}
/**
* 加载客户
* @param cid
* @return
*/
public Customer load(String cid) {
return customerDao.load(cid);
}
public void edit(Customer c) {
customerDao.edit(c);
}
/**
* 多条件组合查询
* @param criteria
* @return
*/
public List<Customer> query(Customer criteria) {
return customerDao.query(criteria);
}
}
servlet
/**
* web层
* @author Administrator
*
*/
public class CustomerServlet extends BaseServlet {
private CustomerService customerService = new CustomerService();
/**
* 添加客户
*/
public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
*1、封装表单数据到Customer对象
*2、补全:cid,使用uuid
*3、使用service方法完成添加
*4、向request域中保存成功信息
*5、转发到msg.jsp
*/
req.setAttribute("yanzheng2", req.getCharacterEncoding());
Customer c = CommonUtils.toBean(req.getParameterMap(), Customer.class);
c.setCid(CommonUtils.uuid());
customerService.add(c);
req.setAttribute("msg", "添加成功");
req.setAttribute("yanzheng", req.getCharacterEncoding());
req.setAttribute("yanzheng1", c.getGender());
return "f:/msg.jsp";
}
/**
* 查询所有
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
*1、调用service得到所有游湖
*2、保存到request域
*3、转发到list.jsp
*/
req.setAttribute("cstmList", customerService.finaAll());
return "f:/list.jsp";
}
/**
* 编辑之前的加载工资
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String preEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
*1、获取cid
*2、使用cid 来调用service方法,得到Customer对象
*3、把Customer保存到request域中
*4、转发到edit.jsp显示在表单中
*/
String cid = req.getParameter("cid");
Customer cstm = customerService.load(cid);
req.setAttribute("cstm", cstm);
return "f:/edit.jsp";
}
/**
* 编辑方法
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
* 1、封装表单数据到Customer对象中
* 2、调用service()方法完成修改
* 3、保存成功信息到request域
* 4、转发到msg.jsp显示成功信息
*/
Customer c = CommonUtils.toBean(req.getParameterMap(), Customer.class);
customerService.edit(c);
req.setAttribute("msg", "编辑成功");
return "f:/msg.jsp";
}
/**
*
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
* 1、封装表单数据到Customer对象中,只有2个属性(cname,gender)
* 就是一个条件
* 2、使用Customer调用service方法 ,得到List<Customer>
* 3、保存到request域中
* 4、转发到list.jsp
*/
//Query By Criteria
Customer criteria = CommonUtils.toBean(req.getParameterMap(), Customer.class);
List<Customer> cstmList = customerService.query(criteria);
req.setAttribute("cstmList", cstmList);
return "/list.jsp";
}
}
这里写代码片