客户管理系统的详细编写流程
一、系统设计
1、需求分析
(系统中包含哪些数据模型、数据模型存在怎样的关系 E-R图、UML(用例图))
2、制作页面Demo
(和真实系统效果一样,给客户确认需求)
3、技术选型(环境搭建)
软件建模工具:IBM RSA、Rational Rose、Jude(日本研发 纯java编写,小巧。在此次编写中使用)
技术选型:DHTML+JSP+Servlet+C3P0+DBUtils+MySQL(MVC模式,DAO模型)+Tomcat6+Windows
4、数据库设计 ----- 新建数据库 新建用户create table customer(
id varchar(40) primary key,
name varchar(20) not null,
gender varchar(10) not null,
birthday date ,
cellphone varchar(20),
email varchar(40),
preference varchar(100),
type varchar(40) not null,
description varchar(255)
);
创建数据库新用户angle 并利用angle来实现JDBC对数据库的操作
create table customer(
id varchar(40) primary key,
name varchar(20) not null,
gender varchar(10) not null,
birthday date ,
cellphone varchar(20),
email varchar(40),
preference varchar(100),
type varchar(40) not null,
description varchar(255)
);
create user angel identified by 'angel'; --此句用来创建angle用户 密码用identified设置为angle
grant all on customer.* to angel; --此句用来限定angle账户访问的权限为customer下的一切数据
grant all on customer.* to angel; --此句用来限定angle账户访问的权限为customer下的一切数据
5、搭建开发环境
(1)创建工程
(2)导入jar包
c3p0-0.9.1.2.jar (因为要用到c3p0技术 )
commons-beanutils-1.8.3.jar commons-logging-1.1.1.jar (利用BeanUtils技术将参数快速封装入javabean中)
commons-dbutils-1.4.jar (数据层快速进行CURD操作)
mysql-connector-java-5.0.8-bin.jar (MySQL数据库驱动)
(3)创建工程包结构
web层:web
业务层:service
数据层:dao
工具类包:utils
bean数据类包:vo
(4)配置文件 c3p0
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="user">angel</property>
<property name="password">angel</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///customer</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config> <!-- This app is massive! -->
</c3p0-config>
6、编写基本类
JDBCUtils 提供连接池和连接工具类
Customer JavaBean结构和数据库对应
JDBCUtils
package utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 工具类 提供数据库连接池 和数据库连接
*
* @author seawind
*
*/
public class JDBCUtils {
private static DataSource dataSource = new ComboPooledDataSource();
public static DataSource getDataSource() {
return dataSource;
}
/**
* 当DBUtils需要手动控制事务时,调用该方法获得一个连接
*
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
package vo;
import java.sql.Date;
/**
* javabean实体类
*
* @author seawind
*
*/
public class Customer {
<span style="white-space:pre"> </span>private String id;
<span style="white-space:pre"> </span>private String name;
<span style="white-space:pre"> </span>private String gender;
<span style="white-space:pre"> </span>private Date birthday;
<span style="white-space:pre"> </span>private String cellphone;
<span style="white-space:pre"> </span>private String email;
<span style="white-space:pre"> </span>private String preference;// 爱好为复选框,不止一个爱好,所以此处会想到用数组,但是若为数组类型,这数据库封 //装不了,所以将此问题在web层进行封装时解决掉。
<span style="white-space:pre"> </span>private String type;
<span style="white-space:pre"> </span>private String description;
<span style="white-space:pre"> </span>public String getId() {
<span style="white-space:pre"> </span>return id;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setId(String id) {
<span style="white-space:pre"> </span>this.id = id;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getName() {
<span style="white-space:pre"> </span>return name;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setName(String name) {
<span style="white-space:pre"> </span>this.name = name;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getGender() {
<span style="white-space:pre"> </span>return gender;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setGender(String gender) {
<span style="white-space:pre"> </span>this.gender = gender;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public Date getBirthday() {
<span style="white-space:pre"> </span>return birthday;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setBirthday(Date birthday) {
<span style="white-space:pre"> </span>this.birthday = birthday;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getCellphone() {
<span style="white-space:pre"> </span>return cellphone;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setCellphone(String cellphone) {
<span style="white-space:pre"> </span>this.cellphone = cellphone;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getEmail() {
<span style="white-space:pre"> </span>return email;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setEmail(String email) {
<span style="white-space:pre"> </span>this.email = email;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getPreference() {
<span style="white-space:pre"> </span>return preference;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setPreference(String preference) {
<span style="white-space:pre"> </span>this.preference = preference;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getType() {
<span style="white-space:pre"> </span>return type;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setType(String type) {
<span style="white-space:pre"> </span>this.type = type;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getDescription() {
<span style="white-space:pre"> </span>return description;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setDescription(String description) {
<span style="white-space:pre"> </span>this.description = description;
<span style="white-space:pre"> </span>}
}
因为客户id为customer表的主键,不能重复,所以利用UUID和Hash码来得到不同的主键
编写CustomerUtils类来实现主键的获取
package utils;
import java.util.UUID;
/**
* 客户工具类,提供通用方法
*
* @author seawind
*
*/
public class CustomerUtils {
public static String generateId() {
<span style="color:#ff0000;">String uuid = UUID.randomUUID().toString();
return uuid.hashCode(); </span><span style="color:#33ccff;">//UUID值太长 所以取得其Hash码</span>
}
}
7、CURD
按照 : 增加 ----- 查询 ---- 删除 ---- 修改 的顺序进行编写
(1)增加数据。
进行增加数据流程的设计分析
首先编写客户端显示页面,要实现对参数的表单提交
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
</head>
<body>
<h1>客户信息增加</h1>
<form action="${pageContext.request.contextPath }/addCustomer" method="post">
<table>