SSH框架搭建实例教程,课程设计
- 格式:doc
- 大小:404.50 KB
- 文档页数:26
SSH框架的搭建第一步:初建Web Project,项目名为demoSSH;第二步:添加Struts框架,此处添加的是struts1.2版本第三步:添加Spring框架,版本Spring1.2注意:添加类库时,别忘了Spring1.2 Web Librabries第四步,打开DB Browser,初建数据库连接jbpm第五步:添加Hibernate框架,版本Hibernate3.1注意:此处的DB Driver选择第四步创建的数据库连接jbpmExistingSSH框架支持已经全部倒入成功了。
这时候看我们的web工程会发现下图的现象:接下来,我们只要添加和去除几个包就可以解决了。
鼠标右键点击工程名称,在右键菜单中选择Properties:此时会弹出下图中的窗口,点击Java Build Path,就会变成我下图所示的样子了。
选择Liberties,将里面的asm.jar和以log4j开头版本较低的jar包删除(如果有两个log4j JAR包的话则删除版本较低的)。
然后点击你的工程进入WebRoot文件夹下的lib文件夹同样删除这两个jar包。
(如果不按照以上步骤,你也可以进入你工程的保存路径,同样找到工程下的WebRoot文件夹下的lib目录,删除以上两个jar包。
)然后进入你的MyEclipes安装路径,我的MyEclipes是安装在F:\Program Files\路径下的。
然后按照以下路径去寻找两个jar包。
MyEclipse6.6\myeclipse\eclipse\plugins\com.genuitec.eclipse.springframework_6.6.0.zmyeclipse 660200810\data\1.2\lib\dependencies\jakarta-commons这是我们需要的那两个jar包的最终路径,所需要的jar包的名称分别是:commons-dbcp.jar和commons-pool.jar。
三大框架搭建步骤:先在数据库中建立库表。
新建web project选择java EE 5.0,在src中建立相应的包,右击工程名MyEclipse->add Hibernate Capabilities...,选择Annotations&Entity Manager及Core Libraries两个jar包,点击“next”点击“next”,选择数据库连接名,点击“finish”。
打开“MyEclipse Database Explorer perspective”窗口,找到之前在数据库中建立的表,选择表,右击选择“Hibernate Reserve Engineering”点击“next”选择“sequence”。
点击“finish”。
在hibernate.cfg.xml中添加“show-sql”值为“true”。
将struts的jar包classes12.jar,commons-logging-1.0.4.jar,freemarker-2.3.8.jar,jstl.jar,ognl-2.6.11.jar,standard.jar,struts2-core-2.0.11.1.jar,struts2-spring-plugin-2.0.11.2.jar,xwork-2.0.4.jar粘贴复制到WebRoot->WEB-INF->lib中。
将lib下的web.xml打开,配置如下:<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"xmlns="/xml/ns/javaee"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/xml/ns/javaee/xml/ns/javaee/web-app_2_5.xsd"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class ></listener><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>在src下新建struts.xml,配置如下:<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""/dtds/struts-2.0.dtd"><struts><!-- struts的注入交给spring去控制--><constant name="struts.objectFactory" value="spring"/><include file="struts-default.xml"/><!-- Add packages here --><package name="struts" extends="struts-default"><action name="login" class="userAction" method="login"><result name="success">/writeDiary.jsp</result><result name="input">/login.jsp</result></action></package></struts>工程名右击:MyEclipse->Add Spring Capabilities…及Spring 2.5 J2EE Libraries和Spring 2.5 Web Libraries点击“next”点击“next”点击“finish”。
基于SSH2架构的课程设计实训教学示例项目——《客户关系管理CRM系统》——合同信息的CRUD实现和测试1.1.1合同信息的CRUD功能实现1、在项目中添加一个包装订单信息的PO类(1)类名称为ContractInfoPO,包名称为com.px1987.bluedreamcrm.dao.po(2)在PO类中添加如下的成员private int contract_ID; //合同IDprivate String contract_kindNumber; //合同类型private String contract_titleText; //合同标题private int customer_ID; //甲方签订人IDprivate String contract_secondName; //乙方签订人姓名private String contract_currentState; //合同状态private double contract_totalMoney; //合同总金额private java.sql.Date contract_signDate; //签订日期private java.sql.Date contract_deadLineDate; //截止日期private String contract_instructionText; //合同说明(3)为这些属性提供get/set方法(4)在该类中提供一个显示合同标题名称简称的方法public String getContractInfo_BriefTitleText() {//如果多于12个字符(汉字),则显示合同标题的简称if(contract_titleText.length()>12){return contract_titleText.substring(0, 8)+"...";}else{return contract_titleText;}}(5)在该类中提供如下的获得日期中的“年、月、日”数据的方法public int getContract_signDateYear() {return contract_signDate.getYear()+1900;}public int getContract_signDateMonth() {return contract_signDate.getMonth()+1;}public int getContract_signDateDate() {return contract_signDate.getDate();}public int getContract_deadLineDateYear() {return contract_deadLineDate.getYear()+1900;}public int getContract_deadLineDateMonth() {return contract_deadLineDate.getMonth()+1;}public int getContract_deadLineDateDate() {return contract_deadLineDate.getDate();}2、添加一个对产品信息进行操作的DAO接口(1)接口的名称为ContractInfoCRUDInterface,包名称为com.px1987.bluedreamcrm.dao.inter(2)设计该接口中的CRUD方法package com.px1987.bluedreamcrm.dao.inter;import java.util.List;import com.px1987.bluedreamcrm.dao.po.ContractInfoPO;import com.px1987.bluedreamcrm.dao.po.PageStatePO;import com.px1987.bluedreamcrm.exception.CRMDataAccessException;public interface ContractInfoCRUDInterface {/*** 分页查询所有的合同信息*/public List<ContractInfoPO> selectAllContractInfo(PageStatePO onePageStatePO) throws CRMDataAccessException;/*** 分页查询满足条件的合同信息*/public List<ContractInfoPO> selectSomeContractInfo(String sqlWhereString,PageStatePO onePageStatePO) throws CRMDataAccessException;/*** 查询指定编号的合同信息*/public ContractInfoPO selectOneContractInfo(int contractInfoID) throws CRMDataAccessException;/*** 查询合同信息的总数*/public int selectTotalContractInfoCounter() throws CRMDataAccessException;/*** 添加一条合同信息*/public boolean insertOneContractInfo(ContractInfoPO oneContractInfoPO) throws CRMDataAccessException;/*** 添加一条合同信息,并再次查询出该合同信息*/public ContractInfoPO insertOneContractInfoAndQueryIt(ContractInfoPO oneContractInfoPO) throws CRMDataAccessException;/*** 修改一条合同信息*/public boolean updateOneContractInfo(ContractInfoPO oneUpdatedContractInfoPO) throws CRMDataAccessException;/*** 删除一条合同信息*/public boolean deleteOneContractInfo(int contractInfoID) throws CRMDataAccessException;/*** 批量删除一批合同信息*/public boolean batchDeleteContractInfo(List<Integer> deletedContractInfoIDs) throws CRMDataAccessException;}3、为该ContractInfoCRUDInterface接口提供一个实现类(1)类名称为ContractInfoCRUDImple,包名称为com.px1987.bluedreamcrm.dao.imple,并且实现com.px1987.bluedreamcrm.dao.inter.ContractInfoCRUDInterface接口(2)编程该实现类package com.px1987.bluedreamcrm.dao.imple; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;import com.px1987.bluedreamcrm.dao.inter.ConnectDBInterface;import com.px1987.bluedreamcrm.dao.inter.ContractInfoCRUDInterface;import com.px1987.bluedreamcrm.dao.po.ContractInfoPO;import com.px1987.bluedreamcrm.dao.po.PageStatePO;import com.px1987.bluedreamcrm.exception.CRMDataAccessException;public class ContractInfoCRUDImple implements ContractInfoCRUDInterface { private ConnectDBInterface oneDBCPConnectDBBean=null;private Connection oneJDBCConnection=null;private Logger logger = Logger.getLogger(this.getClass().getName());public ContractInfoCRUDImple() {oneDBCPConnectDBBean=new MySQLConnectDBBean();}@Overridepublic boolean batchDeleteContractInfo(List<Integer> deletedContractInfoIDs)throws CRMDataAccessException {PreparedStatement onePreparedStatement=null;String deleteContractInfoSql="delete from contract_info where contract_ID=?";boolean returnResult=true;oneJDBCConnection=oneDBCPConnectDBBean.getConnection();try{try {oneJDBCConnection.setAutoCommit(false); //开始事务onePreparedStatement=oneJDBCConnection.prepareStatement(deleteContractInfoSql);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能创建SQL语句对象,可能是SQL 语句有错误!");}try {Iterator<Integer> allDeleteedContractInfoIDs=deletedContractInfoIDs.iterator();while(allDeleteedContractInfoIDs.hasNext()){int contractInfoIDToDeleted=((Integer)allDeleteedContractInfoIDs.next()).intValue();onePreparedStatement.setInt(1, contractInfoIDToDeleted);onePreparedStatement.addBatch();}} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象中的参数赋值,可能是参数不正确!");}try {onePreparedStatement.executeBatch();mit();} catch (SQLException e) {try {oneJDBCConnection.rollback();} catch (SQLException e1) {logger.log(, e.getMessage());throw new CRMDataAccessException("在批处理删除回滚时出现了错误!");}logger.log(, e.getMessage());throw new CRMDataAccessException("在执行批处理删除时出现了错误!");}try {onePreparedStatement.close();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能正确地关闭SQL语句对象!");}}finally{oneDBCPConnectDBBean.closeDBConnection();}return returnResult;}@Overridepublic boolean deleteOneContractInfo(int contractInfoID)throws CRMDataAccessException {PreparedStatement onePreparedStatement=null;int returnResult=0;oneJDBCConnection=oneDBCPConnectDBBean.getConnection();String sqlDeleteCustomerStatement="delete from contract_info where contract_ID=?";try{try {onePreparedStatement=oneJDBCConnection.prepareStatement(sqlDeleteCustomerStatement);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能创建SQL语句对象,可能是SQL 语句有错误!");}try {onePreparedStatement.setInt(1,contractInfoID);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象中的参数赋值,可能是参数不正确!");}try {returnResult=onePreparedStatement.executeUpdate();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象进行删除操作,可能是SQL不正确!");}try {onePreparedStatement.close();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能正确地关闭SQL语句对象!");}}finally{oneDBCPConnectDBBean.closeDBConnection();}if(returnResult==0){return false;}else{return true;}}@Overridepublic boolean insertOneContractInfo(ContractInfoPO oneContractInfoPO)throws CRMDataAccessException {PreparedStatement onePreparedStatement=null;int returnResult=0;oneJDBCConnection=oneDBCPConnectDBBean.getConnection();try{StringBuffer sqlInsertContractInfoStatement=new StringBuffer();sqlInsertContractInfoStatement.append("insert into contract_info(contract_kindNumber,contract_titleText,");sqlInsertContractInfoStatement.append("customer_ID,contract_secondName,contract_curren tState,contract_totalMoney,");sqlInsertContractInfoStatement.append("contract_signDate,contract_deadLineDate,contract_ instructionText,contract_ID)");sqlInsertContractInfoStatement.append(" values(?,?,?,?,?,?,?,?,?,?)");try {onePreparedStatement=oneJDBCConnection.prepareStatement(sqlInsertContractInfoStatement.toString());} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能创建SQL语句对象,可能是SQL语句有错误!");}try {setInsertOrUpdateSQLParameterByContractInfoPO(onePreparedStatement,oneContractInfo PO);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象中的参数赋值,可能是参数不正确!");}try {returnResult=onePreparedStatement.executeUpdate();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象进行插入操作,可能是SQL不正确!");}try {onePreparedStatement.close();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能正确地关闭SQL语句对象!");}}finally{oneDBCPConnectDBBean.closeDBConnection();}if(returnResult==0){return false;}else{return true;}}@Overridepublic ContractInfoPO insertOneContractInfoAndQueryIt(ContractInfoPO oneContractInfoPO) throws CRMDataAccessException { PreparedStatement onePreparedStatement=null;int returnResult=0;ResultSet oneResultSet=null;ContractInfoPO returnInsertedOneContractInfoPO=null;oneJDBCConnection=oneDBCPConnectDBBean.getConnection();//***************** 下面首先插入数据********************************** try{StringBuffer sqlInsertContractInfoStatement=new StringBuffer();sqlInsertContractInfoStatement.append("insert into contract_info(contract_kindNumber,contract_titleText,");sqlInsertContractInfoStatement.append("customer_ID,contract_secondName,contract_curren tState,contract_totalMoney,");sqlInsertContractInfoStatement.append("contract_signDate,contract_deadLineDate,contract_ instructionText,contract_ID)");sqlInsertContractInfoStatement.append(" values(?,?,?,?,?,?,?,?,?,?)");try {onePreparedStatement=oneJDBCConnection.prepareStatement(sqlInsertContractInfoStatement.toString());} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能创建SQL语句对象,可能是SQL 语句有错误!");}try {setInsertOrUpdateSQLParameterByContractInfoPO(onePreparedStatement,oneContractInfo PO);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象中的参数赋值,可能是参数不正确!");}try {returnResult=onePreparedStatement.executeUpdate();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象进行插入操作,可能是SQL不正确!");}if(returnResult==0){throw new CRMDataAccessException("该合同信息没有正确地插入到数据库表中,可能是数据库系统出现了异常!");}//******************** 插入成功后再读取该数据*******************String sqlSelectContractInfoStatement="select * from contract_info where contract_ID=?";try {onePreparedStatement=oneJDBCConnection.prepareStatement(sqlSelectContractInfoStatement,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能创建SQL语句对象,可能是SQL 语句有错误!");}try {onePreparedStatement.setInt(1,oneContractInfoPO.getContract_ID());} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象中的参数赋值,可能是参数不正确!");}try {oneResultSet=onePreparedStatement.executeQuery();if(!oneResultSet.next()){return null;}} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象进行查询操作,可能是SQL不正确!");}returnInsertedOneContractInfoPO=new ContractInfoPO();try {contractInfoPO_ORMapping(returnInsertedOneContractInfoPO,oneResultSet);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能从结果集中获得所需要的数据值,可能是字段名称不正确!");}try {onePreparedStatement.close();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能正确地关闭SQL语句对象!");}}finally{oneDBCPConnectDBBean.closeDBConnection();}return returnInsertedOneContractInfoPO;}private void setInsertOrUpdateSQLParameterByContractInfoPO(PreparedStatement onePreparedStatement,ContractInfoPO oneContractInfoPO) throws SQLException{onePreparedStatement.setString(1,oneContractInfoPO.getContract_kindNumber());onePreparedStatement.setString(2,oneContractInfoPO.getContract_titleText());onePreparedStatement.setInt(3,oneContractInfoPO.getCustomer_ID());onePreparedStatement.setString(4,oneContractInfoPO.getContract_secondName());onePreparedStatement.setString(5,oneContractInfoPO.getContract_currentState());onePreparedStatement.setDouble(6,oneContractInfoPO.getContract_totalMoney());onePreparedStatement.setDate(7,oneContractInfoPO.getContract_signDate());onePreparedStatement.setDate(8,oneContractInfoPO.getContract_deadLineDate());onePreparedStatement.setString(9,oneContractInfoPO.getContract_instructionText());onePreparedStatement.setInt(10,oneContractInfoPO.getContract_ID());}public void contractInfoPO_ORMapping(ContractInfoPO oneContractInfoPO,ResultSet oneResultSet) throws SQLException{oneContractInfoPO.setContract_ID(oneResultSet.getInt("contract_ID"));oneContractInfoPO.setContract_kindNumber(oneResultSet.getString("contract_kindNumber "));oneContractInfoPO.setContract_titleText(oneResultSet.getString("contract_titleText"));oneContractInfoPO.setCustomer_ID(oneResultSet.getInt("customer_ID"));oneContractInfoPO.setContract_secondName(oneResultSet.getString("contract_secondName "));oneContractInfoPO.setContract_currentState(oneResultSet.getString("contract_currentState" ));oneContractInfoPO.setContract_totalMoney(oneResultSet.getDouble("contract_totalMoney" ));oneContractInfoPO.setContract_signDate(oneResultSet.getDate("contract_signDate"));oneContractInfoPO.setContract_deadLineDate(oneResultSet.getDate("contract_deadLineDat e"));oneContractInfoPO.setContract_instructionText(oneResultSet.getString("contract_instruction Text"));}@Overridepublic List<ContractInfoPO> selectAllContractInfo(PageStatePO onePageStatePO)throws CRMDataAccessException {PreparedStatement onePreparedStatement=null;ResultSet oneResultSet=null;ContractInfoPO oneContractInfoPO=null;int totalNumberOfElements;List<ContractInfoPO> allContractInfoPOs=new ArrayList<ContractInfoPO>();oneJDBCConnection=oneDBCPConnectDBBean.getConnection();try{//***************** 首先获得满足条件的总记录数************************ String getTotalCounterSQLStatement="select count(*) from contract_info";try {onePreparedStatement=oneJDBCConnection.prepareStatement(getTotalCounterSQLStatement,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能创建SQL语句对象,可能是SQL 语句有错误!");}try {oneResultSet=onePreparedStatement.executeQuery();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象进行查询操作,可能是SQL不正确!");}try {oneResultSet.next();totalNumberOfElements = oneResultSet.getInt(1);} catch (SQLException e) {throw new CRMDataAccessException("不能从结果集中获得所需要的数据值,可能是字段名称不正确!");}onePageStatePO.setPageStatePOMemberProperty(totalNumberOfElements); //获得分页中的其它状态数据//*************** 然后再获得满足条件的具体记录数据******************* StringBuffer sqlSelectContractInfoStatement=new StringBuffer();sqlSelectContractInfoStatement.append("select * from contract_info limit ");sqlSelectContractInfoStatement.append(onePageStatePO.getThisPageFirstElementNumber()) ;sqlSelectContractInfoStatement.append(",");sqlSelectContractInfoStatement.append(onePageStatePO.getOnePageSize());try {onePreparedStatement=oneJDBCConnection.prepareStatement(sqlSelectContractInfoStatement.toString(), ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能创建SQL语句对象,可能是SQL 语句有错误!");}try {oneResultSet=onePreparedStatement.executeQuery();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象进行查询操作,可能是SQL不正确!");}try{while(oneResultSet.next()){oneContractInfoPO=new ContractInfoPO();contractInfoPO_ORMapping(oneContractInfoPO,oneResultSet);allContractInfoPOs.add(oneContractInfoPO);}}catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能从结果集中获得所需要的数据值,可能是字段名称不正确!");}try {oneResultSet.close();onePreparedStatement.close();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能正确地关闭SQL语句对象!");}}finally{oneDBCPConnectDBBean.closeDBConnection();}return allContractInfoPOs;}@Overridepublic ContractInfoPO selectOneContractInfo(int contractInfoID)throws CRMDataAccessException {ContractInfoPO oneContractInfoPO=null;PreparedStatement onePreparedStatement=null;ResultSet oneResultSet=null;oneJDBCConnection=oneDBCPConnectDBBean.getConnection();String sqlSelectContractInfoStatement="select * from contract_info where contract_ID=?";try{try {onePreparedStatement=oneJDBCConnection.prepareStatement(sqlSelectContractInfoStatement,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能创建SQL语句对象,可能是SQL 语句有错误!");}try {onePreparedStatement.setInt(1,contractInfoID);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象中的参数赋值,可能是参数不正确!");}try {oneResultSet=onePreparedStatement.executeQuery();if(!oneResultSet.next()){return null;}} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象进行查询操作,可能是SQL不正确!");}oneContractInfoPO=new ContractInfoPO();try {contractInfoPO_ORMapping(oneContractInfoPO,oneResultSet);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能从结果集中获得所需要的数据值,可能是字段名称不正确!");}try {oneResultSet.close();onePreparedStatement.close();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能正确地关闭SQL语句对象!");}}finally{oneDBCPConnectDBBean.closeDBConnection();}return oneContractInfoPO;}@Overridepublic List<ContractInfoPO> selectSomeContractInfo(String sqlWhereString,PageStatePO onePageStatePO) throws CRMDataAccessException {PreparedStatement onePreparedStatement=null;ResultSet oneResultSet=null;ContractInfoPO oneContractInfoPO=null;int totalNumberOfElements;List<ContractInfoPO> allContractInfoPOs=new ArrayList<ContractInfoPO>();oneJDBCConnection=oneDBCPConnectDBBean.getConnection();try{//***************** 首先获得满足条件的总记录数*********************String getTotalCounterSQLStatement="select count(*) from contract_info "+sqlWhereString;try {onePreparedStatement=oneJDBCConnection.prepareStatement(getTotalCounterSQLStatement,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能创建SQL语句对象,可能是SQL 语句有错误!");}try {oneResultSet=onePreparedStatement.executeQuery();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象进行查询操作,可能是SQL不正确!");}try {oneResultSet.next();totalNumberOfElements = oneResultSet.getInt(1);} catch (SQLException e) {throw new CRMDataAccessException("不能从结果集中获得所需要的数据值,可能是字段名称不正确!");}onePageStatePO.setPageStatePOMemberProperty(totalNumberOfElements); //获得分页中的其它状态数据//**************** 然后再获得满足条件的具体记录数据*****************StringBuffer sqlSelectContractInfoStatement=new StringBuffer();sqlSelectContractInfoStatement.append("select * from contract_info ");sqlSelectContractInfoStatement.append(sqlWhereString);sqlSelectContractInfoStatement.append(" limit ");sqlSelectContractInfoStatement.append(onePageStatePO.getThisPageFirstElementNumber()) ;sqlSelectContractInfoStatement.append(",");sqlSelectContractInfoStatement.append(onePageStatePO.getOnePageSize());try {onePreparedStatement=oneJDBCConnection.prepareStatement(sqlSelectContractInfoStatement.toString(), ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能创建SQL语句对象,可能是SQL 语句有错误!");}try {oneResultSet=onePreparedStatement.executeQuery();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象进行查询操作,可能是SQL不正确!");}try{while(oneResultSet.next()){oneContractInfoPO=new ContractInfoPO();contractInfoPO_ORMapping(oneContractInfoPO,oneResultSet);allContractInfoPOs.add(oneContractInfoPO);}}catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能从结果集中获得所需要的数据值,可能是字段名称不正确!");}try {oneResultSet.close();onePreparedStatement.close();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能正确地关闭SQL语句对象!");}}finally{oneDBCPConnectDBBean.closeDBConnection();}return allContractInfoPOs;}@Overridepublic int selectTotalContractInfoCounter() throws CRMDataAccessException { ResultSet oneResultSet=null;int totalContractInfoCounter;String select_SqlStatement="select count(*) from contract_info";oneJDBCConnection=oneDBCPConnectDBBean.getConnection();java.sql.PreparedStatement onePreparedStatement = null;try{try {onePreparedStatement =oneJDBCConnection.prepareStatement(select_SqlStatement,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("SQL语句可能不正确!");}try {oneResultSet = onePreparedStatement.executeQuery();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("在统计合同信息总数时出现错误!");}try{oneResultSet.next();totalContractInfoCounter = oneResultSet.getInt(1);}catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能从结果集中获得所需要的数据值,可能是字段名称不正确!");}try {oneResultSet.close();onePreparedStatement.close();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能正确地关闭SQL语句对象!");}}finally{oneDBCPConnectDBBean.closeDBConnection();}return totalContractInfoCounter;}@Overridepublic boolean updateOneContractInfo(ContractInfoPO oneUpdatedContractInfoPO) throws CRMDataAccessException {int returnResult=0;StringBuffer sqlUpdateContractInfoStatement=new StringBuffer();sqlUpdateContractInfoStatement.append("update contract_info set contract_kindNumber=?,contract_titleText=?,customer_ID=?,contract_secondName=?,");sqlUpdateContractInfoStatement.append("contract_currentState=?,contract_totalMoney=?,co ntract_signDate=?,contract_deadLineDate=?,");sqlUpdateContractInfoStatement.append("contract_instructionText=? where contract_ID=?");oneJDBCConnection=oneDBCPConnectDBBean.getConnection();PreparedStatement onePreparedStatement =null;try{try {onePreparedStatement = oneJDBCConnection.prepareStatement(sqlUpdateContractInfoStatement.toString());} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("SQL语句可能不正确!");}try {setInsertOrUpdateSQLParameterByContractInfoPO(onePreparedStatement,oneUpdatedCont ractInfoPO);} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能对SQL语句对象中的参数赋值,可能是参数不正确!");}try {returnResult=onePreparedStatement.executeUpdate();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("在修改合同信息数据库表时出现错误!");}try {onePreparedStatement.close();} catch (SQLException e) {logger.log(, e.getMessage());throw new CRMDataAccessException("不能正确地关闭SQL语句对象!");}}finally{oneDBCPConnectDBBean.closeDBConnection();}if(returnResult==0){return false;}else{return true;}} }。
搭建SSH框架步骤一、建数据库二、建Web工程打开Eclipse在工程栏—>鼠标右击New—>选择Web project—>project Name输入Demo;勾起Java 5.0单选按钮—>完成—>在Src文件夹下创建dal、bll、entity、、bll.action三个包以及两个子包。
三、添加框架1. 添加Struts选中所建的Web工程—>鼠标右击选择MyEclipse—>选择Add StrutsCapabilites…—>Struts specification选择Struts1.2;Base package fornew class单击Brouse选择文件路径为dal包;勾掉多选按钮Install StrutsTLDs —>完成2. 添加Spring选中所建的Web工程—>鼠标右击选择MyEclipse—>选择Add SpringCapabilites…—>勾起多选按钮Spring 2.5 Aop Libraries;勾起多选按钮Spring 2.5 Core Libraries;勾起多选按钮Spring 2.5 Persistence Core;勾起多选按钮Spring 2.5 Web Libraries;勾起单选按钮Copy checked Librarycontents…—>单击Browse选择路径为WEB-INF包—>完成3. 添加Hibernate选中所建的Web工程—>鼠标右击选择MyEclipse—>选择Add HibernateCapabilites…—>勾起单选按钮Copy checked Library Jars to…—>选择Spring configuration file—>选择Existing Spring configuration file;SessionFactory Id 输入sessionFactory —>BeanId输入DemoBean;DBDriver选中自己所建的数据库—>勾掉Create Session Factory class? —>完成4. 将SSH架包粘贴到lib文件夹下5. 展开工程中的Referenced Libraries 文件夹(架包文件夹)—>在展开的文件中找到asm-2.23.jar文件—>右击Build path—>选择Remove from Build path —>删除文件asm-2.23.jar四、创建数据单击Eclipse右上角的MyEclipse Hibernate 按钮—>鼠标右键New—> Driver template 下拉选择Microsofe SQLServer 2005;Drever name输入DemoDB;Connection URL 输入jdbc:sqlserver://localhost:1433;User name 输入sa; password输入123456;单击Add JARs 按钮导入jdbc 包;勾起Save password多选按钮—>勾起Display the selected schemas 单选按钮;单击Add按钮导入数据库—>完成—>鼠标右击数据栏刚才所建的数据—>单击Open Connection —>选择dbo —>TABLE—>找到对应的表—>右击Hibernate Reverse Euginnering…—>单击Java src folder 的Brouse按钮选择包entity;勾Creat POJO <>DB…;勾起Java Data Object…;勾起Java Data Access Object…;勾掉Java Data Object…下面的Create abstract class—>在Id Generator 下框中选择native—>勾起Include referenced tables(AB;勾起Include referencing tables(AB;可以给表或者其中的属性起相应的名字,当然也可以不起让其自动生成—>完成五、配置XML文件1.配置Struts-config.xml文件在下面添加标签注:找到Referenced Libraries 包下面的spring-webmvc-struts . jar包复制文件即可。
SSH框架搭建详细图⽂教程⼀、什么是SSH?SSH是JavaEE中三种框架(Struts+Spring+Hibernate)的集成框架,是⽬前⽐较流⾏的⼀种Java Web开源框架。
SSH主要⽤于Java Web的开发。
现在SSH有SSH1和SSH2两种,区别在于Struts的版本是struts1.x还是2.x。
本⽂介绍的是SSH1。
⼆、Struts Spring Hibernate各起什么作⽤?Struts:控制逻辑关系。
Spring:解耦。
Hibernate:操作数据库。
三、学习SSH的⼀些建议SSH适合开发⼀些中、⼤型的企业级⽹站。
功能强⼤,性能也⾼。
但是学习成本也⾼,⿇烦也不少,不擅长做⼩型⽹站或便捷开发。
如果你熟悉Java并准备做动态⽹站的开发,SSH值得学习,如果你不熟悉Java,或没有⼀定的编程经验,只想做个简单的个⼈⽹站或者论坛。
PHP也许更适合你。
四、准备⼯作俗话说:“⼯欲善其事必先利其器”。
让我们看看在搭建SSH前需要准备哪些⼯具吧。
1.JDK[] [] []做Java开发第⼀步当然是Java开发环境了,JDK是必备的,本⽂中⽤的是jdk-8u111。
2.MyEclipse[] [] []搭建SSH最好⽤的开发⼯具了,本⽂中⽤的是MyEclipse 10。
3.MySQL[] [] []完整的动态⽹站需要⼀个数据库。
注意安装时设置的密码千万别忘了。
本⽂中⽤的是MySQL 5.5.53。
4.Navicat for MySQL[] [] []MySQL可视化开发⼯具,主要⽤他来操作数据库。
本⽂中⽤的是Navicat for MySQL 10.1.75.JDBC[] []数据库连接池,这是Java连接数据库必备的jar包。
本⽂中⽤的是mysql-connector-java-5.0.8-bin.jar。
安装顺序:JDK > MyEclipse > MySQL > Navicat for MySQL⽹站结构和开发⼯具作⽤如下图:五、搭建SSH步骤开发⼯具下载安装完毕后,正式进⼊搭建步骤。
搭建SSH框架一、首先打开MyEclipse 7.5编辑环境,通过File>>>New>>>web Project,单击新建一个web Project工程,取名为ssh,在默认情况下,J2EE Specification Level 选中的是J2EE 1.4选项,在这里为了提高版本的兼容性,这里选择J2EE 5.5,最后点击Finish就OK. 如图例1-1:图1-1二、在新建好Web Project之后,接下来就得配置好需要的jar包,选中SSH按单击右键,选择build Path Configure Bulid Path,出现如图2-1:三、图2-1在出现图2-1的界面之后,点击add Library按钮,然后在弹出的对话框中选中User Library选项,点击Next按钮,如图2-2所示:图2-2完成上一步之后,便会弹出如下对话框,如图2-3所示,在弹出的对话框中再点击User Libraries按钮。
图2-3选择User Libraries后,点击对话框右边的new按钮,以此来创建你自己的一个库,然后弹出如图2-4,然后在弹出的小对话框中给你的库文件取个名字。
在这里我们为自己库取名为sshlibrary。
为库取好名字之后,点击对话框右边的Add JARs按钮,把相关的jar文件从磁盘中倒入到你的刚建立的库中。
如图2-5,然后一直点击完成动作,直到对话框被关闭完,这样,我们配置好我们所需要的jar包了。
四、在完成上面两步之后,右键点击项目ssh,在右键菜单myeclipse中选中addspring capabilities,进入注入spring对话框,如图3-1:图3-1在弹出的对话框中,我们选择Spring版本比较高的Spring2.5,然后去掉默认选中的MyEclipse Librariese选项,选择User Libraries选项,并选中刚上一步我们创建的库sshlibrary,然后点击下一步,出现如图3-2所示对话框:图3-2在出现对话框后,我们需要改动applicationContext.xml的存放目录,也就是Folder 选项,我们把它的存放目录改为WebRoot/WEB-INF下,点击Finish按钮,完成Spring注入。
SSH架构的搭建过程1准备工作a)安装Oracl数据库建立Collecting_S 空间建立Collecting_U用户创建用户数据表B_Userb)安装javac)安装Tomcatd)安装Myeclipse2绑定Tomcat和Java2.1设定字符集a)点击Window菜单,选择Preferences进行参数设置b)选择Myeclipse 的Files and JSP选择Encoding:ISO 10646 (UTF-8)c)选择Myeclipse 的Files and PHP选择Encoding:ISO 10646 (UTF-8)d)设置当前工程中的字符集当工程建立后,选中工程,点击右键,选择2.2绑定Javaa)点击Window菜单,选择Preferences进行参数设置b)先择Java的Installed JREsc)点击Addd)选择Standard VMe)点击Directoryf)选择路径C:\Program Files\Java\jdk1.7.0_04g)移除原来的Java包2.3绑定Tomcata)点击服务器图标,选择Configure Serverb)选择Servers的Tomcat 7.xc)选择Enable,浏览Tomcat home Directory:C:\apache-tomcat-7.0.23d)点击Apply,查看Tomcat的JDK是否正常3建立数据源a)点击Window的Show View的Otherb)选择Myeclipse Database下的DB Browserc)在DB Browser 窗口下点击鼠标右键,选择New项d)填写Driver Template :Oracl (Thin Driver)e)填写Driver Name :如ORCL_123f)填写Connection URL :jdbc:oracle:thin:@10.255.8.123:1521:ORCLg)填写User Name :Collecting_Uh)填写Password : Collecting_Pi)点击Add JARs:E:\app\Legend8045dfeo\product\11.2.0\client_1\jdbc\lib\ ojdbc6.jar j)Driver Name:oracle.jdbc.OracleDriver4添加调试显示在工程的初始包src路径下创建一个文件log4j.properties内容为:# Configure logging for testing: optionally with log filelog4j.rootLogger=Debug, stdout# log4j.rootLogger=WARN, stdout, logfile#print to screenlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderyout=org.apache.log4j.PatternLayoutyout.ConversionPattern=%d %p [%c] - %m%n#write to log filelog4j.appender.logfile=org.apache.log4j.FileAppenderlog4j.appender.logfile.File=target/spring.logyout=org.apache.log4j.PatternLayoutyout.ConversionPattern=%d %p [%c] - %m%n5清除Tomcat中无用的工程(加快速度)6建立工程,搭建HHS1框架a)点击File的New的Web Projectb)填写Project Name:pCollectingS1c)选择use Defeat JRE(Currently’JDK1.7.0_04)d)发布测试点击发布图标点击Add按钮选择Server为Tomcat 7.X打开IE浏览,输入http://localhost:8080/pCollectingS1/显示This is my JSP page6.1添加Spring 框架a)选中工程pCollecting 点击鼠标右键,选择Myeclipse 的Add Spring Capabilitiesb)选择Spring 3.0c)选择数据包Spring 3.0 AOP LibrariesSpring 3.0 Core LibrariesSpring 3.0 Persistence Core LibrariesSpring 3.0 Persistence JDBC LibrariesSpring 3.0 WEB Librariesd)选择copy checked Liberty Contents to Project Folder:/WebRoot/WEB-INF/Lib7添加Hibernate框架8添加Struts 框架9反向工程:根据数据库中的表来生成POJO类、DAO接口和实现类、hibernate 配置文件等a)进入Database Browserb)连接到数据库c)找到Table 的U_User数据表d)点击右键选择Hibernate Reverse Engineering10创建工作路径包a)创建接口er.Service在该包下创建接口IUserLogin修改为public interface IUserLogin {Object Login(String strUserNo, String strPassword);}创建对象(实现)包com.pCollecting.DB.U_User.Service.Impl创建是实现类UserLoginImplmport java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.pCollecting.DB.U_User.POJO.UUserDAO;import com.pCollecting.DB.U_User.Service.IU_UserLogin;public class U_UserLogin extends UUserDAO implements IU_UserLogin {private static final Logger log =LoggerFactory.getLogger(UUserDAO.class);// 为了输出日志文件public Object Login(String userNo, String strPWD) {log.debug("finding the UUser for login");String queryString = "from UUser where userNo = '" + userNo + "' and pwd = '" + strPWD + "'";List<?> lst = (List<?>)getHibernateTemplate().find(queryString);if (lst.size() != 0) {return lst.get(0);}return null;}}修改ApplicationContext.xml将dao改为接口<bean id="UUserDAO"class="er.POJO.UUserDAO"> <property name="sessionFactory"><ref bean ="sessionFactory" /></</bean ></ <bean id="class="er.修改public class UserLoginImpl 类public class UserLoginImpl extends UUserDAO implements IUserLogin {private static final Logger log = LoggerFactory.getLogger (UUserDAO.class );// 为了输出日志文件public Object Login(String userNo, String strPWD) {log .debug("finding the UUser for login");try {String queryString = "from UUser where userNo = '" + userNo+ "' and pwd = '" + strPWD + "'";List<?> lst = (List<?>) getHibernateTemplate().find(queryString);if (lst.size() != 0) {return lst.get(0);}} catch (RuntimeException re) {log .error("find the UUser for login failed", re);throw re;}return null ; } 11 配置侦听在Web.XML 文件中添加侦听(前面)<!-- 根据实际spring 配置文件目录进行配置--><!-- 如果spring 配置文件被命名为applicationContext.xml ,并且放在WEB-INF 目录下,则不需要配置<context-param>--><!-- 因为ContextLoaderListener 默认在WEB-INF 目录下寻找名为applicationContext.xml 的文件--><context-param ><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/application*.xml</param-value> </context-param><!-- 载入Spring ApplicationContext --><listener><listener-class>org.springframework.web.context.ContextLoaderList ener</listener-class></listener><!-- 支持session scope的Spring bean --><listener><listener-class>org.springframework.web.context.request.RequestContextListener</l istener-class></listener>测试建立main类进行测试public class Test {/*** @param args*/public static void main(String[] args) {ApplicationContext acx = new FileSystemXmlApplicationContext( "/WebRoot/WEB-INF/applicationContext.xml");IUserLolgin ul = (IUserLolgin) acx.getBean("userlogin");BUser bUser = (BUser) erLogin("1001", "123456");if (bUser == null) {System.out.println("error");return;}System.out.println("Success UserNo=" + bUser.getUserNo());}添加添加登录jsp添加登录正确jsp添加登录错误jsp添加类package com.pCollecting.struts.action;import org.apache.struts.actions.DispatchAction;import org.springframework.web.context.WebApplicationContext;importorg.springframework.web.context.support.WebApplicationContextUtils;public class SuperAction extends DispatchAction {protected Object getBean(String id) {WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.servlet.getServletContext());return ctx.getBean(id);}}修改UserLoginActionpublic class UserLoginAction extends SuperAction {/** Generated Methods*//*** Method execute* @param mapping* @param form* @param request* @param response* @return ActionForward*/public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserLoginForm userLoginForm = (UserLoginForm) form;IUserLolgin userLogin = (IUserLolgin)getBean("userlogin");BUser user = (BUser)erLogin(userLoginForm.getUserno(),userLoginForm.getPassword());if (user == null){// forward to errorrequest.setAttribute("errorInfo", userLoginForm.getUserno() + ";" + userLoginForm.getPassword());return mapping.findForward("err");} else {// forward to successrequest.setAttribute("LoginInfo", userLoginForm.getUserno() + ";" + userLoginForm.getPassword());request.setAttribute("message", "亲爱的"+ user.getName() + ":欢迎您!");return mapping.findForward("succ");}}}。
SSH框架的搭建详细图⽂教程转载-6.在MySQL中新建⼀个表,⽤来测试数据我在这⾥准备做⼀个注册登录功能,所以在test⾥建了⼀个user表,表⾥的字段有id、username、password、nickname四个字段。
nickname⽤来测试中⽂是否乱码。
SQL语句:CREATE TABLE `user` (`id` bigint(20) unsigned NOT NULL auto_increment COMMENT 'ID',`username` varchar(40) character set utf8 NOT NULL COMMENT '⽤户名',`password` varchar(40) collate utf8_unicode_ci NOT NULL COMMENT '密码',`nickname` varchar(40) character set utf8 NOT NULL COMMENT '昵称',PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ciCOMMENT='⽤户信息表';SSH框架搭建步骤:1. 新建⼀个项⽬。
打开MyEclipse,新建⼀个Web项⽬,起名就叫SSH,点击完成,若弹出提⽰点Yes即可。
2.添加Struts框架。
右键这个项⽬,选择MyEclipse->Add StrutsCapabilities。
在弹出的菜单中把包的路径改⼀下, Base package for new classes选项中的路径com.yourcompany.struts改成com.ssh.struts, 点击完成。
展开WebRoot中的WEB-INF⽂件夹,看到⽬录结构如下图,证明Struts框架添加完成。
.SSH的理解及其应用实践1.SSH是什么 (3)2 Spring 介绍理解: (3)2.1简单介绍 (3)2.2各种特性 (3)2.2.1轻量 (3)2.2.2控制反转 (4)2.2.3面向切面 (4)2 .2.4容器 (4)2.2.5框架 (4)2.3总结 (4)3.Hibernate介绍理解: (4)3.1简单介绍 (4)3.2核心接口 (5).3.2.1 Session接口 (5)3.2.2 .SessionFactory接口 (5)3.2.3.Configuration接口 (5)3.2.4.Transaction接口 (5)3.2.5 Query和Criteria接口 (5)4. Struts (6)4.1什么是Struts框架 (6)4.2 Struts 概览 (6)4.2.1Client browser(客户浏览器) (6)4.4 Struts中的Controller(控制器)命令设计模式的实现 (7)4.5 在Struts框架中控制器组件的三个构成部分 (7)4.7 Struts中的Model(模型) (8)5.SSH整合步骤 (8)5.1安装所需软件环境: (8)5.1.1、首先安装JDK,配置Java环境变量 (8)5.1.2安装MyEelipse (8)5.1.3 数据库 (9)5.1.4、 (9)5.2环境配置好了就可以做SSH整合的项目 (9)6.搭建框架并简单应用 (11)6.1准备工作 (11)6.2(建立项目,包结构,导入所需jar文件) (12)6.3撰写资源文件 (15)6.4在m 包下添加下列文件 (19)6.5添加hibernate DAO 模板 (20)6.6以上的工作还不够,我们还需要进行WEB方面的配置 (20)7.测试: (23).ssh.model 包下建立User.java (23)7.2 com.ssh.service 包下建立UserService.java (24)7.3com.ssh.test 下建立Test.java (25)7.4结果 (26)8.结束语 (26)1.SSH是什么新的MVC软件开发模式, SSH(Struts,Spring,Hibernate) Struts进行流程控制,Spring进行业务流转,Hibernate进行数据库操作的封装,这种新的开发模式让我们的开发更加方便、快捷、思路清晰!2 Spring 介绍理解:2.1简单介绍Spring是一个开源框架,它由Rod Johnson创建。
它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring 的用途不仅限于服务器端的开发。
从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
2.2各种特性2.2.1轻量从大小与开销两方面而言Spring都是轻量的。
完整的Spring框架可以在一个大小只有1MB多的JAR文件里发布。
并且Spring所需的处理开销也是微不足道的。
此外,Spring是非侵入式的:典型地,Spring应用中的对象不依赖于Spring的特定类。
2.2.2控制反转Spring通过一种称作控制反转(IoC)的技术促进了松耦合。
当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象。
你可以认为IoC与JNDI相反——不是对象从容器中查找依赖,而是容器在对象初始化时不等对象请求就主动将依赖传递给它。
2.2.3面向切面Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。
应用对象只实现它们应该做的——完成业务逻辑——仅此而已。
它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。
2 .2.4容器Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例——以及它们是如何相互关联的。
然而,Spring不应该被混同于传统的重量级的EJB容器,它们经常是庞大与笨重的,难以使用。
2.2.5框架Spring可以将简单的组件配置、组合成为复杂的应用。
在Spring中,应用对象被声明式地组合,典型地是在一个XML文件里。
Spring也提供了很多基础功能(事务管理、持久化框架集成等等),将应用逻辑的开发留给了你。
2.3总结所有Spring的这些特征使你能够编写更干净、更可管理、并且更易于测试的代码。
它们也为Spring中的各种模块提供了基础支持。
3.Hibernate介绍理解:3.1简单介绍Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP 的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。
3.2核心接口Hibernate的核心接口一共有5个,分别为:Session、SessionFactory、Transaction、Query和Configuration。
这5个核心接口在任何开发中都会用到。
通过这些接口,不仅可以对持久化对象进行存取,还能够进行事务控制。
下面对这五个核心接口分别加以介绍。
·3.2.1 Session接口Session接口负责执行被持久化对象的CRUD操作(CRUD的任务是完成与数据库的交流,包含了很多常见的SQL语句。
)。
但需要注意的是Session对象是非线程安全的。
同时,Hibernate 的session不同于JSP应用中的HttpSession。
这里当使用session这个术语时,其实指的是Hibernate中的session,而以后会将HttpSesion对象称为用户session。
3.2.2 ·SessionFactory接口SessionFactory接口负责初始化Hibernate。
它充当数据存储源的代理,并负责创建Session 对象。
这里用到了工厂模式。
需要注意的是SessionFactory并不是轻量级的,因为一般情况下,一个项目通常只需要一个SessionFactory就够,当需要操作多个数据库时,可以为每个数据库指定一个SessionFactory。
3.2.3·Configuration接口Configuration接口负责配置并启动Hibernate,创建SessionFactory对象。
在Hibernate 的启动的过程中,Configuration类的实例首先定位映射文档位置、读取配置,然后创建SessionFactory对象。
3.2.4·Transaction接口Transaction接口负责事务相关的操作。
它是可选的,开发人员也可以设计编写自己的底层事务处理代码。
3.2.5 Query和Criteria接口Query和Criteria接口负责执行各种数据库查询。
它可以使用HQL语言或SQL语句两种表达方式。
4. Struts4.1什么是Struts框架Struts 是一组相互协作的类、servlet 和 JSP 标记组成的一个可重用的 MVC 2 设计。
这个定义表示 Struts 是一个框架,而不是一个库,但 Struts 也包含了丰富的标记库和独立于该框架工作的实用程序类。
4.2 Struts 概览4.2.1Client browser(客户浏览器)来自客户浏览器的每个 HTTP 请求创建一个事件。
Web 容器将用一个 HTTP 响应作出响应。
4.2.2 Controller(控制器)控制器接收来自浏览器的请求,并决定将这个请求发往何处。
就 Struts 而言,控制器是以servlet 实现的一个命令设计模式。
struts-config.xml 文件配置控制器命令。
4.3.3 业务逻辑业务逻辑更新模型的状态,并帮助控制应用程序的流程。
就 Struts 而言,这是通过作为实际业务逻辑“瘦”包装的 Action 类完成的。
4.3.4 Model(模型)的状态模型表示应用程序的状态。
业务对象更新应用程序的状态。
ActionForm bean 在会话级或请求级表示模型的状态,而不是在持久级 JSP 文件使用 JSP 标记读取来ActionForm bean 的信息。
4.3. 5 View(视图)视图就是一个 JSP 文件。
其中没有流程逻辑,没有业务逻辑,也没有模型信息 -- 只有标记。
标记是使 Struts 有别于其他框架(如 Velocity)的因素之一。
4.4 Struts中的Controller(控制器)命令设计模式的实现Struts 的控制器将事件(事件通常是 HTTP post)映射到类的一个 servlet.ActionServlet (Command)创建并使用 Action 、 ActionForm 和ActionForward . 通过struts-config.xml 文件配置该 Command.从而扩展 Action 和ActionForm 来解决特定的问题。
可以通过扩展 ActionServlet 来添加 Command 功能。
4.5 在Struts框架中控制器组件的三个构成部分1,消息控制模块;用户所有的提交请求都发往消息控制模块,在由消息总线模块根据视图映射模块找到消息处理模块来执行处理逻辑。
消息控制模块接收所有用户的请求,并根据请求视图找到处理。
Strtus中通过ActionServlet和RequestProcessor类来完成。
2,消息处理模块:该模块执行该消息的逻辑处理,因此通常由用户扩展实现。
Struts 中通过Action类来实现。
3,视图映射模块;控制器逻辑和视图逻辑之间的结合是通过配置模块和配置文件建立其的链接。
Struts中配置模块通过ModuleConfig类来完成,而配置文件由struts-config.xml文件提供。
4.6 Struts中的View(视图)视图组件通过JSP组件的实现机制。
Struts框架对视图组件进行了扩展和封装。
Struts对视图的扩展提供了一组扩展服务器标签用来提供视图显示以及传递数据到处理缓冲,而处理缓冲由ActionForm的扩展类来实现。