您的位置:首页 > 业界新闻

MVC三层结构 设计项目步骤总结

2015-07-07 23:26 609 查看
2014年8月29日10:42:35

​//MVC三层结构设计项目步骤总结:

​1、构建数据库:

SQL指令自己查API

​#构建数据源:最好布置在src目录下

固定:



2、向MyEclipse中导入相关联的jar包和js库等

如:
jquery-1.9.1.min.js



mysql-connector-java-5.1.27.jar



beanutils.jar


commons-logging.jar


jstl.jar


standard.jar


​3、构建util层,连接数据库,安装jdbc驱动

固定模板:

未建数据源:

packagecom.jplus.util;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
publicclassUtil{
	privatestaticStringdriver="com.mysql.jdbc.Driver";
	privatestaticStringuser="root";
	privatestaticStringurl="jdbc:mysql://localhost:3306/0829backman";
	privatestaticPreparedStatementpst=null;
	privatestaticConnectionconnection=null;
	privatestaticResultSetrs=null;
static{
try{
Class.forName(driver);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
}
	privatestaticConnectiongetConn(){
try{
returnDriverManager.getConnection(url,user,user);
}catch(SQLExceptione){
e.printStackTrace();
}
		returnnull;
}
	publicstaticintupdate(Stringsql,Object...params){
try{
connection=getConn();
pst=connection.prepareStatement(sql);
for(inti=0;params.length!=0&&i<params.length;i++){
	pst.setObject(i+1,params[i]);
}
returnpst.executeUpdate();
}catch(SQLExceptione){
e.printStackTrace();
}
		return0;
}
	publicstaticResultSetquery(Stringsql,Object...params){
try{
connection=getConn();
pst=connection.prepareStatement(sql);
for(inti=0;params.length!=0&&i<params.length;i++){
	pst.setObject(i+1,params[i]);
}
returnpst.executeQuery();
}catch(SQLExceptione){
e.printStackTrace();
}
		returnnull;
}
	publicstaticvoidclose(){
if(rs!=null){
	try{
rs.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
}
		if(pst!=null){
	try{
pst.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
}
		if(connection!=null){
	try{
connection.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
}
}
}
[/code]

若已搭建好数据源,则模板如下:

packagecn.itcast.util;
importjava.io.InputStream;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.Properties;
publicclassJdbcUtil{
	privatestaticStringdriverClassName;
	privatestaticStringurl;
	privatestaticStringuser;
	privatestaticStringpassword;
static{
try{
InputStreamin=JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
Propertiesprops=newProperties();
props.load(in);
driverClassName=props.getProperty("driverClassName");
url=props.getProperty("url");
user=props.getProperty("user");
password=props.getProperty("password");
Class.forName(driverClassName);
}catch(Exceptione){
e.printStackTrace();
}
}
	publicstaticConnectiongetConnection()throwsException{
		returnDriverManager.getConnection(url,user,password);
}
	publicstaticvoidrelease(ResultSetrs,Statementstmt,Connectionconn){
if(rs!=null){
	try{
rs.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
	try{
stmt.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
stmt=null;
}
if(conn!=null){
	try{
conn.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
conn=null;
}
}
}
[/code]

​4、构建bean,建立对象,封装简单的方法

packagecom.jplus.domain;
publicclassUser{
	privateintid;
	privateStringusername;
	privateStringpassword;
	privateStringgender;
	publicintgetId(){
		returnid;
}
	publicvoidsetId(intid){
		this.id=id;
}
	publicStringgetUsername(){
		returnusername;
}
	publicvoidsetUsername(Stringusername){
		this.username=username;
}
	publicStringgetPassword(){
		returnpassword;
}
	publicvoidsetPassword(Stringpassword){
		this.password=password;
}
	publicStringgetGender(){
		returngender;
}
	publicvoidsetGender(Stringgender){
		this.gender=gender;
}
}
[/code]

5、构建DAO层,封装对数据操作的方法:增删改查

有需要的话,编写一定的接口imple,并在DAO编写
servlet实现这些接口。

固定模板:

[code]packagecom.jplus.dao;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.util.ArrayList;
importjava.util.List;
importcom.jplus.bean.User;
importcom.jplus.exception.DaoException;
importcom.jplus.exception.IdIsNullException;
importcom.jplus.util.UserInforUtil;
publicclassUserInforDao{
	publicvoidadd(Useruser){
if(user==null)
thrownewIllegalArgumentException();
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("insertintouserinfor(id,username,password,gender)values(?,?,?)");
stmt.setString(1,user.getId());
stmt.setString(2,user.getUsername());
stmt.setString(2,user.getPassword());
stmt.setString(3,user.getGender());
stmt.executeUpdate();
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
	publicvoiddelById(StringuserId){
if(userId==null)
thrownewIllegalArgumentException();
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("deletefromuserinforwhereid=?");
stmt.setString(1,userId);
stmt.executeUpdate();
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
	publicList<User>findAll(){
		thrownewAbstractMethodError("Cannotinvoketheabstractmethod!");
}
	publicUserfindById(StringuserId){
if(userId==null)
thrownewIllegalArgumentException();
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("selectid,username,password,genderfromuserinforwhereid=?");
stmt.setString(1,userId);
rs=stmt.executeQuery();
if(rs.next()){
	Useruser=newUser();
	user.setId(rs.getString("id"));
	user.setUsername(rs.getString("username"));
	user.setUsername(rs.getString("password"));
	user.setGender(rs.getString("gender"));
	returnuser;
}else
	returnnull;
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
	publicvoidupdate(Useruser)throwsIdIsNullException{
if(user==null)
thrownewIllegalArgumentException();
if(user.getId()==null)
thrownewIdIsNullException("Theuser'sidcannotbenull");
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("updateuserinforsetusername=?,password=?,gender=?whereid=?");
stmt.setString(1,user.getUsername());
stmt.setString(1,user.getPassword());
stmt.setString(2,user.getGender());
stmt.executeUpdate();
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
	publicList<User>findPageRecords(intstartIndex,intpagesize){
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
		List<User>cs=newArrayList<User>();
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("selectid,username,password,genderfromuserinforlimit?,?");
stmt.setInt(1,startIndex);
stmt.setInt(2,pagesize);
rs=stmt.executeQuery();
while(rs.next()){
	Useruser=newUser();
	user.setId(rs.getString("id"));
	user.setUsername(rs.getString("username"));
	user.setPassword(rs.getString("password"));
	user.setGender(rs.getString("gender"));
	cs.add(user);
}
returncs;
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
	publicintgetTotalRecords(){
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("selectcount(*)fromuserinfor");
rs=stmt.executeQuery();
if(rs.next()){
	returnrs.getInt(1);
}else
	return0;
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
}
[/code]


8、构建前台
JSP或者HTML页面:所有前台页面在这一步中实现

包括特效等,具体以js和jQuery
为主。

尽量少用jsp中嵌入java代码

9、构建
Action数据操作层:贯连整个Web项目,项目能否正常运作,在这里是关键

就是将前台和后台连接起来的servlet程序集

​10、整体测试项目系统的功能实现:

#################################################################################################

​#需要注意的问题:

1.jsp的配置问题:

<servlet>
<servlet-name>jsp文件名</servlet-name>
<jsp-file>/jsp相对路径</jsp-file>
</servlet>

<servlet-mapping><servlet-name>jsp文件名</servlet-name><url-pattern>/jsp访问路径名</url-pattern></servlet-mapping>[/code]

访问路径允许配置多个,两个jsp文件名必须保持一致

2.servlet的配置问题:

<servlet>
<servlet-name>Servlet文件名</servlet-name>
<servlet-class>com.jplus.web.action.……类似路径名</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>Servlet文件名</servlet-name>
<url-pattern>/servlet访问路径名</url-pattern>
</servlet-mapping>
[/code]

两个Servlet文件名必须保持一致

3.Exception问题:

packagecn.itcast.exception;
publicclassDaoExceptionextendsRuntimeException{
	publicDaoException(){
}
	publicDaoException(Stringmessage){
super(message);
}
	publicDaoException(Throwablecause){
super(cause);
}
	publicDaoException(Stringmessage,Throwablecause){
		super(message,cause);
}
}
[/code]

packagecn.itcast.exception;
publicclassIdIsNullExceptionextendsException{
	publicIdIsNullException(){
}
	publicIdIsNullException(Stringmessage){
super(message);
}
	publicIdIsNullException(Throwablecause){
super(cause);
}
	publicIdIsNullException(Stringmessage,Throwablecause){
		super(message,cause);
}
}
[/code]

*将所有的错误放在Exception类中,调用时:





4、随机id号:(varchar类型)

importjava.util.UUID;

user.setId(UUID.randomUUID().toString());

*************************************************************************************
#模板项目:客户管理系统

<压缩文件>

2014年8月29日10:42:35

​//MVC三层结构设计项目步骤总结:

​1、构建数据库:

SQL指令自己查API

​#构建数据源:最好布置在src目录下

固定:



2、向MyEclipse中导入相关联的ja
492d9
r包和js库等

如:
jquery-1.9.1.min.js



mysql-connector-java-5.1.27.jar



beanutils.jar


commons-logging.jar


jstl.jar


standard.jar


​3、构建util层,连接数据库,安装jdbc驱动

固定模板:

未建数据源:

packagecom.jplus.util;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
publicclassUtil{
	privatestaticStringdriver="com.mysql.jdbc.Driver";
	privatestaticStringuser="root";
	privatestaticStringurl="jdbc:mysql://localhost:3306/0829backman";
	privatestaticPreparedStatementpst=null;
	privatestaticConnectionconnection=null;
	privatestaticResultSetrs=null;
static{
try{
Class.forName(driver);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
}
	privatestaticConnectiongetConn(){
try{
returnDriverManager.getConnection(url,user,user);
}catch(SQLExceptione){
e.printStackTrace();
}
		returnnull;
}
	publicstaticintupdate(Stringsql,Object...params){
try{
connection=getConn();
pst=connection.prepareStatement(sql);
for(inti=0;params.length!=0&&i<params.length;i++){
	pst.setObject(i+1,params[i]);
}
returnpst.executeUpdate();
}catch(SQLExceptione){
e.printStackTrace();
}
		return0;
}
	publicstaticResultSetquery(Stringsql,Object...params){
try{
connection=getConn();
pst=connection.prepareStatement(sql);
for(inti=0;params.length!=0&&i<params.length;i++){
	pst.setObject(i+1,params[i]);
}
returnpst.executeQuery();
}catch(SQLExceptione){
e.printStackTrace();
}
		returnnull;
}
	publicstaticvoidclose(){
if(rs!=null){
	try{
rs.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
}
		if(pst!=null){
	try{
pst.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
}
		if(connection!=null){
	try{
connection.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
}
}
}
[/code]

若已搭建好数据源,则模板如下:

packagecn.itcast.util;
importjava.io.InputStream;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.Properties;
publicclassJdbcUtil{
	privatestaticStringdriverClassName;
	privatestaticStringurl;
	privatestaticStringuser;
	privatestaticStringpassword;
static{
try{
InputStreamin=JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
Propertiesprops=newProperties();
props.load(in);
driverClassName=props.getProperty("driverClassName");
url=props.getProperty("url");
user=props.getProperty("user");
password=props.getProperty("password");
Class.forName(driverClassName);
}catch(Exceptione){
e.printStackTrace();
}
}
	publicstaticConnectiongetConnection()throwsException{
		returnDriverManager.getConnection(url,user,password);
}
	publicstaticvoidrelease(ResultSetrs,Statementstmt,Connectionconn){
if(rs!=null){
	try{
rs.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
	try{
stmt.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
stmt=null;
}
if(conn!=null){
	try{
conn.close();
}catch(SQLExceptione){
	e.printStackTrace();
}
conn=null;
}
}
}
[/code]

​4、构建bean,建立对象,封装简单的方法

packagecom.jplus.domain;
publicclassUser{
	privateintid;
	privateStringusername;
	privateStringpassword;
	privateStringgender;
	publicintgetId(){
		returnid;
}
	publicvoidsetId(intid){
		this.id=id;
}
	publicStringgetUsername(){
		returnusername;
}
	publicvoidsetUsername(Stringusername){
		this.username=username;
}
	publicStringgetPassword(){
		returnpassword;
}
	publicvoidsetPassword(Stringpassword){
		this.password=password;
}
	publicStringgetGender(){
		returngender;
}
	publicvoidsetGender(Stringgender){
		this.gender=gender;
}
}
[/code]

5、构建DAO层,封装对数据操作的方法:增删改查

有需要的话,编写一定的接口imple,并在DAO编写
servlet实现这些接口。

固定模板:

[code]packagecom.jplus.dao;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.util.ArrayList;
importjava.util.List;
importcom.jplus.bean.User;
importcom.jplus.exception.DaoException;
importcom.jplus.exception.IdIsNullException;
importcom.jplus.util.UserInforUtil;
publicclassUserInforDao{
	publicvoidadd(Useruser){
if(user==null)
thrownewIllegalArgumentException();
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("insertintouserinfor(id,username,password,gender)values(?,?,?)");
stmt.setString(1,user.getId());
stmt.setString(2,user.getUsername());
stmt.setString(2,user.getPassword());
stmt.setString(3,user.getGender());
stmt.executeUpdate();
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
	publicvoiddelById(StringuserId){
if(userId==null)
thrownewIllegalArgumentException();
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("deletefromuserinforwhereid=?");
stmt.setString(1,userId);
stmt.executeUpdate();
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
	publicList<User>findAll(){
		thrownewAbstractMethodError("Cannotinvoketheabstractmethod!");
}
	publicUserfindById(StringuserId){
if(userId==null)
thrownewIllegalArgumentException();
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("selectid,username,password,genderfromuserinforwhereid=?");
stmt.setString(1,userId);
rs=stmt.executeQuery();
if(rs.next()){
	Useruser=newUser();
	user.setId(rs.getString("id"));
	user.setUsername(rs.getString("username"));
	user.setUsername(rs.getString("password"));
	user.setGender(rs.getString("gender"));
	returnuser;
}else
	returnnull;
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
	publicvoidupdate(Useruser)throwsIdIsNullException{
if(user==null)
thrownewIllegalArgumentException();
if(user.getId()==null)
thrownewIdIsNullException("Theuser'sidcannotbenull");
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("updateuserinforsetusername=?,password=?,gender=?whereid=?");
stmt.setString(1,user.getUsername());
stmt.setString(1,user.getPassword());
stmt.setString(2,user.getGender());
stmt.executeUpdate();
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
	publicList<User>findPageRecords(intstartIndex,intpagesize){
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
		List<User>cs=newArrayList<User>();
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("selectid,username,password,genderfromuserinforlimit?,?");
stmt.setInt(1,startIndex);
stmt.setInt(2,pagesize);
rs=stmt.executeQuery();
while(rs.next()){
	Useruser=newUser();
	user.setId(rs.getString("id"));
	user.setUsername(rs.getString("username"));
	user.setPassword(rs.getString("password"));
	user.setGender(rs.getString("gender"));
	cs.add(user);
}
returncs;
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
	publicintgetTotalRecords(){
		Connectionconn=null;
		PreparedStatementstmt=null;
		ResultSetrs=null;
try{
conn=UserInforUtil.getConnection();
stmt=conn.prepareStatement("selectcount(*)fromuserinfor");
rs=stmt.executeQuery();
if(rs.next()){
	returnrs.getInt(1);
}else
	return0;
}catch(Exceptione){
thrownewDaoException(e);
}finally{
UserInforUtil.release(rs,stmt,conn);
}
}
}
[/code]


8、构建前台
JSP或者HTML页面:所有前台页面在这一步中实现

包括特效等,具体以js和jQuery
为主。

尽量少用jsp中嵌入java代码

9、构建
Action数据操作层:贯连整个Web项目,项目能否正常运作,在这里是关键

就是将前台和后台连接起来的servlet程序集

​10、整体测试项目系统的功能实现:

#################################################################################################

​#需要注意的问题:

1.jsp的配置问题:

<servlet>
<servlet-name>jsp文件名</servlet-name>
<jsp-file>/jsp相对路径</jsp-file>
</servlet>

<servlet-mapping><servlet-name>jsp文件名</servlet-name><url-pattern>/jsp访问路径名</url-pattern></servlet-mapping>[/code]

访问路径允许配置多个,两个jsp文件名必须保持一致

2.servlet的配置问题:

<servlet>
<servlet-name>Servlet文件名</servlet-name>
<servlet-class>com.jplus.web.action.……类似路径名</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>Servlet文件名</servlet-name>
<url-pattern>/servlet访问路径名</url-pattern>
</servlet-mapping>
[/code]

两个Servlet文件名必须保持一致

3.Exception问题:

packagecn.itcast.exception;
publicclassDaoExceptionextendsRuntimeException{
	publicDaoException(){
}
	publicDaoException(Stringmessage){
super(message);
}
	publicDaoException(Throwablecause){
super(cause);
}
	publicDaoException(Stringmessage,Throwablecause){
		super(message,cause);
}
}
[/code]

packagecn.itcast.exception;
publicclassIdIsNullExceptionextendsException{
	publicIdIsNullException(){
}
	publicIdIsNullException(Stringmessage){
super(message);
}
	publicIdIsNullException(Throwablecause){
super(cause);
}
	publicIdIsNullException(Stringmessage,Throwablecause){
		super(message,cause);
}
}
[/code]

*将所有的错误放在Exception类中,调用时:





4、随机id号:(varchar类型)

importjava.util.UUID;

user.setId(UUID.randomUUID().toString());

*************************************************************************************
#模板项目:客户管理系统

<压缩文件>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  互联网 mvc JSP servlet