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



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());
                   



  



*************************************************************************************
#模板项目:客户管理系统      <压缩文件>
            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值