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

原创 2015年07月07日 23:26:27


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驱动
        固定模板:

        未建数据源:
package com.jplus.util;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class Util {
private static String driver = "com.mysql.jdbc.Driver";
private static String user = "root";
private static String url = "jdbc:mysql://localhost:3306/0829backman";
 
private static PreparedStatement pst = null;
private static Connection connection = null;
private static ResultSet rs = null;
 
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
 
private static Connection getConn() {
try {
return DriverManager.getConnection(url, user, user);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
 
public static int update(String sql, Object... params) {
try {
connection = getConn();
pst = connection.prepareStatement(sql);
for (int i = 0; params.length != 0 && i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
return pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
 
public static ResultSet query(String sql, Object... params) {
try {
connection = getConn();
pst = connection.prepareStatement(sql);
for (int i = 0; params.length != 0 && i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
return pst.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
 
public static void close() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
        
        若已搭建好数据源,则模板如下:
package cn.itcast.util;
 
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
 
public class JdbcUtil {
private static String driverClassName;
private static String url;
private static String user;
private static String password;
static{
try {
InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
Properties props = new Properties();
props.load(in);
driverClassName = props.getProperty("driverClassName");
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
Class.forName(driverClassName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
return DriverManager.getConnection(url, user, password);
}
public static void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}



4、构建bean,建立对象,封装简单的方法
        
package com.jplus.domain;
 
public class User {
private int id;
private String username;
private String password;
private String gender;
 
public int getId() {
return id;
}
 
public void setId(int id) {
this.id = id;
}
 
public String getUsername() {
return username;
}
 
public void setUsername(String username) {
this.username = username;
}
 
public String getPassword() {
return password;
}
 
public void setPassword(String password) {
this.password = password;
}
 
public String getGender() {
return gender;
}
 
public void setGender(String gender) {
this.gender = gender;
}
 
}




5、构建DAO层,封装对数据操作的方法:增删改查
            有需要的话,编写一定的接口imple,并在DAO编写 servlet 实现 这些接口。
           固定模板:
package com.jplus.dao;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
 
import com.jplus.bean.User;
import com.jplus.exception.DaoException;
import com.jplus.exception.IdIsNullException;
import com.jplus.util.UserInforUtil;
 
 
public class UserInforDao {
public void add(User user) {
if(user==null)
throw new IllegalArgumentException();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("insert into userinfor (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(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
public void delById(String userId) {
if(userId==null)
throw new IllegalArgumentException();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("delete from userinfor where id=?");
stmt.setString(1, userId);
stmt.executeUpdate();
}catch(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
public List<User> findAll() {
throw new AbstractMethodError("Can not invoke the abstract method!");
}
public User findById(String userId) {
if(userId==null)
throw new IllegalArgumentException();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("select id,username,password,gender from userinfor where id=?");
stmt.setString(1, userId);
rs = stmt.executeQuery();
if(rs.next()){
User user = new User();
user.setId(rs.getString("id"));
user.setUsername(rs.getString("username"));
user.setUsername(rs.getString("password"));
user.setGender(rs.getString("gender"));
return user;
}else
return null;
}catch(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
public void update(User user) throws IdIsNullException {
if(user==null)
throw new IllegalArgumentException();
if(user.getId()==null)
throw new IdIsNullException("The user's id can not be null");
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("update userinfor set username=?,password=?,gender=? where id=?");
stmt.setString(1, user.getUsername());
stmt.setString(1, user.getPassword());
stmt.setString(2, user.getGender());
stmt.executeUpdate();
}catch(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
public List<User> findPageRecords(int startIndex, int pagesize) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<User> cs = new ArrayList<User>();
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("select id,username,password,gender from userinfor limit ?,?");
stmt.setInt(1, startIndex);
stmt.setInt(2, pagesize);
rs = stmt.executeQuery();
while(rs.next()){
User user = new User();
user.setId(rs.getString("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setGender(rs.getString("gender"));
cs.add(user);
}
return cs;
}catch(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
public int getTotalRecords() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("select count(*) from userinfor");
rs = stmt.executeQuery();
if(rs.next()){
return rs.getInt(1);
}else
return 0;
}catch(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
}





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>
访问路径允许配置多个,两个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>
两个Servlet文件名必须保持一致


       3.Exception 问题:
package cn.itcast.exception;
 
public class DaoException extends RuntimeException {
 
public DaoException() {
}
 
public DaoException(String message) {
super(message);
}
 
public DaoException(Throwable cause) {
super(cause);
}
 
public DaoException(String message, Throwable cause) {
super(message, cause);
}
 
}

package cn.itcast.exception;
 
public class IdIsNullException extends Exception {
 
public IdIsNullException() {
}
 
public IdIsNullException(String message) {
super(message);
}
 
public IdIsNullException(Throwable cause) {
super(cause);
}
 
public IdIsNullException(String message, Throwable cause) {
super(message, cause);
}
 
}

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

 


            4、随机id号:(varchar类型)
                import java.util.UUID;
        user.setId(UUID.randomUUID().toString());
                



  



*************************************************************************************
#模板项目:客户管理系统    <压缩文件>
            
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驱动
        固定模板:

        未建数据源:
package com.jplus.util;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class Util {
private static String driver = "com.mysql.jdbc.Driver";
private static String user = "root";
private static String url = "jdbc:mysql://localhost:3306/0829backman";
 
private static PreparedStatement pst = null;
private static Connection connection = null;
private static ResultSet rs = null;
 
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
 
private static Connection getConn() {
try {
return DriverManager.getConnection(url, user, user);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
 
public static int update(String sql, Object... params) {
try {
connection = getConn();
pst = connection.prepareStatement(sql);
for (int i = 0; params.length != 0 && i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
return pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
 
public static ResultSet query(String sql, Object... params) {
try {
connection = getConn();
pst = connection.prepareStatement(sql);
for (int i = 0; params.length != 0 && i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
return pst.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
 
public static void close() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
        
        若已搭建好数据源,则模板如下:
package cn.itcast.util;
 
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
 
public class JdbcUtil {
private static String driverClassName;
private static String url;
private static String user;
private static String password;
static{
try {
InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
Properties props = new Properties();
props.load(in);
driverClassName = props.getProperty("driverClassName");
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
Class.forName(driverClassName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
return DriverManager.getConnection(url, user, password);
}
public static void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}



4、构建bean,建立对象,封装简单的方法
        
package com.jplus.domain;
 
public class User {
private int id;
private String username;
private String password;
private String gender;
 
public int getId() {
return id;
}
 
public void setId(int id) {
this.id = id;
}
 
public String getUsername() {
return username;
}
 
public void setUsername(String username) {
this.username = username;
}
 
public String getPassword() {
return password;
}
 
public void setPassword(String password) {
this.password = password;
}
 
public String getGender() {
return gender;
}
 
public void setGender(String gender) {
this.gender = gender;
}
 
}




5、构建DAO层,封装对数据操作的方法:增删改查
            有需要的话,编写一定的接口imple,并在DAO编写 servlet 实现 这些接口。
           固定模板:
package com.jplus.dao;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
 
import com.jplus.bean.User;
import com.jplus.exception.DaoException;
import com.jplus.exception.IdIsNullException;
import com.jplus.util.UserInforUtil;
 
 
public class UserInforDao {
public void add(User user) {
if(user==null)
throw new IllegalArgumentException();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("insert into userinfor (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(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
public void delById(String userId) {
if(userId==null)
throw new IllegalArgumentException();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("delete from userinfor where id=?");
stmt.setString(1, userId);
stmt.executeUpdate();
}catch(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
public List<User> findAll() {
throw new AbstractMethodError("Can not invoke the abstract method!");
}
public User findById(String userId) {
if(userId==null)
throw new IllegalArgumentException();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("select id,username,password,gender from userinfor where id=?");
stmt.setString(1, userId);
rs = stmt.executeQuery();
if(rs.next()){
User user = new User();
user.setId(rs.getString("id"));
user.setUsername(rs.getString("username"));
user.setUsername(rs.getString("password"));
user.setGender(rs.getString("gender"));
return user;
}else
return null;
}catch(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
public void update(User user) throws IdIsNullException {
if(user==null)
throw new IllegalArgumentException();
if(user.getId()==null)
throw new IdIsNullException("The user's id can not be null");
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("update userinfor set username=?,password=?,gender=? where id=?");
stmt.setString(1, user.getUsername());
stmt.setString(1, user.getPassword());
stmt.setString(2, user.getGender());
stmt.executeUpdate();
}catch(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
public List<User> findPageRecords(int startIndex, int pagesize) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<User> cs = new ArrayList<User>();
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("select id,username,password,gender from userinfor limit ?,?");
stmt.setInt(1, startIndex);
stmt.setInt(2, pagesize);
rs = stmt.executeQuery();
while(rs.next()){
User user = new User();
user.setId(rs.getString("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setGender(rs.getString("gender"));
cs.add(user);
}
return cs;
}catch(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
public int getTotalRecords() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = UserInforUtil.getConnection();
stmt = conn.prepareStatement("select count(*) from userinfor");
rs = stmt.executeQuery();
if(rs.next()){
return rs.getInt(1);
}else
return 0;
}catch(Exception e){
throw new DaoException(e);
}finally{
UserInforUtil.release(rs, stmt, conn);
}
}
}





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>
访问路径允许配置多个,两个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>
两个Servlet文件名必须保持一致


       3.Exception 问题:
package cn.itcast.exception;
 
public class DaoException extends RuntimeException {
 
public DaoException() {
}
 
public DaoException(String message) {
super(message);
}
 
public DaoException(Throwable cause) {
super(cause);
}
 
public DaoException(String message, Throwable cause) {
super(message, cause);
}
 
}

package cn.itcast.exception;
 
public class IdIsNullException extends Exception {
 
public IdIsNullException() {
}
 
public IdIsNullException(String message) {
super(message);
}
 
public IdIsNullException(Throwable cause) {
super(cause);
}
 
public IdIsNullException(String message, Throwable cause) {
super(message, cause);
}
 
}

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

 


            4、随机id号:(varchar类型)
                import java.util.UUID;
        user.setId(UUID.randomUUID().toString());
                



  



*************************************************************************************
#模板项目:客户管理系统    <压缩文件>
            
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

基于C# 三层结构(MVC)扩展

  • 2009-12-02 16:20
  • 2.38MB
  • 下载

谈 三层结构与MVC模式的区别

转自:http://www.cnblogs.com/wisdomqq/archive/2009/04/29/1446579.html 在CSDN和园子里有朋友谈到三层与MVC的区别,以前也有...

C# mvc三层结构

  • 2008-08-02 22:16
  • 82KB
  • 下载

java web笔记——软件三层结构&MVC模式实现注册登陆案例

Servlet+JSP+JavaBean(MVC)模式适合开发较复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp负责数据显示,javaBean负责封装数据,各个模块之间层次清晰。...

Java中的mvc和三层结构究竟是什么关系

一件事,要知其然往往很简单,要知其所以然通常不是那么容易,就如最近重新巩固spring的过程中,就觉得还有许多问题其实并不是十分明了。屈指一算,手头上做过的正式项目也有了四五六七个了,不管用的数据库和...

根据mysql数据库自动生成mvc三层代码及jsp页,极速开发srpingmvc+mybatis+bootstrap项目。

最近开发web总感觉增删改查,mvc三层重复开发的套路代码太多,于是有了一个想法:能不能一键生成增删改查各层代码,于是开发了这个项目,项目主要的功能:根据mysql数据库(表和字段要有注释)自动生成m...

Java web学习——Spring MVC项目实例,三层架构通过JDBC链接SQLServer2012

Spring MVC架构原理?原理篇 1、新建项目 File-New-Other,选择Dynamic web project项目建好后的目录结构如下: 2、导入jar包 导入spring mv...

C#三层架构的MVC项目源码

  • 2011-10-04 17:14
  • 3.43MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)