Myeclipse 6.5 整合SSH(二)

SSH构架的配置

上次已经说了工程的创建,这次说说为工程添加配置的过程。

一、数据库-配置数据连接池

2、 在applicationContext.xml文件中添加配置文件:

<!--数据库-配置数据连接池 -->

<bean id="dataSource"        

       class="org.apache.commons.dbcp.BasicDataSource">

       <property name="driverClassName"

           value="com.mysql.jdbc.Driver">

       </property>

       <property name="url"

           value="jdbc:mysql://localhost:3306/dbssh">

       </property>

       <property name="username" value="root"></property>

       <property name="password" value="123456"></property>

       <property name="maxActive" value="100"></property>

       <property name="maxWait" value="500"></property>

       <property name="defaultAutoCommit" value="true"></property>

    </bean>

2、将老师给的数据库工具mysql-5.1.49-win32放到D盘,将驱动包mysql-connector-java-5.1.41-bin拷到lib文件夹下。(或者其他盘也可以,只要注意改写脚本路径即可)。

cd D:\mysql-5.1.49-win32\bin

 pushd D:

 mysql.exe

 pause

打开startmysql启动数据库,单击连接->MySQl ,输入名字密码测试连接,成功后点击OK。在双击admin -> dbss,即可创建表格。这我创建的table是cust。

过程如下图:

3、再在applicationContext.xml添加如下配置文件:

<!--sessionFactory配置与管理  -->

    <bean id="sessionFactory"

       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <property name="dataSource" ref="dataSource"></property>

       <property name="hibernateProperties">

           <props>

              <prop key="hibernate.dialect">

                  org.hibernate.dialect.MySQLDialect

              </prop>

              <prop key="hibernate.show_sql">true</prop>

           </props>

       </property>

       <property name="mappingResources">

           <list>

              <value>com/crm/bean/Cust.hbm.xml</value>

           </list>

       </property>

    </bean>

三、

前话:在小学期老师带着做的工程中,添加的动作有有保存、删除、查询、条件查询、修改预览、修改等,当然少不了其中还有各个活动间的链接(跳转)活动操作。这里直接给出的是关于“客户信息维护”的包含各种操作的完整代码的。但在做的过程中是一点点添加的。同时不要忘记在applicationContext.xml和struts.xml中添加相应的配置文件。

1、

1、为了让Web容器能够初始化Spring,我们需要修改web.xml文件,增加以下内容:

<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>

  <listener>

    <listener-class>

       org.springframework.web.context.ContextLoaderListener

    </listener-class>

  </listener>

2、在src文件下新建com.crm.bean包,并在其下新建Class文件Cust.Java和Cust.hbm.xml文件

package com.crm.bean;

public class Cust {

    private int id;
    private String custno;
    private String custname;
    private String sex;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getCustno() {
        return custno;
    }
    public void setCustno(String custno) {
        this.custno = custno;
    }
    public String getCustname() {
        return custname;
    }
    public void setCustname(String custname) {
        this.custname = custname;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   

<hibernate-mapping>
    <class name="com.crm.bean.Cust" table="cust">
        <id name="id" type="java.lang.Integer" column="id">
            <generator class="increment"></generator>
        </id>
        <property name="custno" type="string" column="custno" length="20"/>
        <property name="custname" type="string" column="custname" length="100"/>
        <property name="sex" type="string" column="sex" length="2"/>
    </class>
</hibernate-mapping>

3、在src文件下新建com.crm.dao包,并在其下新建Class文件CustDao.Java

package com.crm.dao;

import java.util.List;

import com.crm.bean.Cust;

public interface CustDao {

    /**
     * 保存客户信息
     * @param cust
     */
    public void saveCustomer(Cust cust);

    /**
     * 删除客户信息
     * @param cust
     */
    public void removeCustomer(Cust cust);

    /**
     * 查询客户信息
     * @param id
     * @return
     */
    public Cust findCustomerById(Integer id);

    /**
     * 查询所有客户信息
     * @param id
     * @return
     */
    public List<Cust> findAllCust();

    /**
     * 通过条件查询客户信息
     * @param cust
     * @return
     */
    public List<Cust> findCustByCondition(Cust cust);

    /**
     * 修改客户信息
     * @param cust
     */
    public void updateCustomer(Cust cust);
}

4、在src文件下新建名为struts.xml的文件。

5、在src文件下新建com.crm.impl包,并在其下新建Class文件CustDaoImpl.Java

package com.crm.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.crm.bean.Cust;
import com.crm.dao.CustDao;

public class CustDaoImpl extends HibernateDaoSupport implements CustDao{

    @SuppressWarnings("unchecked")
    public List<Cust> findAllCust() {      //
        // TODO Auto-generated method stub
        String hql = "from Cust cust order by cust.id desc";
        return (List<Cust>)this.getHibernateTemplate().find(hql);
    }

    public Cust findCustomerById(Integer id) {
        // TODO Auto-generated method stub
        Cust cust = (Cust)this.getHibernateTemplate().get(Cust.class,id);
        return cust;
    }

    public void removeCustomer(Cust cust) {
        // TODO Auto-generated method stub
        this.getHibernateTemplate().delete(cust);
    }

    public void saveCustomer(Cust cust) {
        // TODO Auto-generated method stub
        this.getHibernateTemplate().save(cust);
    }

    @SuppressWarnings("unchecked")
    public List<Cust> findCustByCondition(Cust cust) {
        // TODO Auto-generated method stub
        StringBuffer strBuffer = new StringBuffer();
        String hql = "from Cust cust where 1=1 ";
        strBuffer.append(hql);
        if(cust == null){
            throw new NullPointerException("查询条件不能为空!");
        }
        if(!"".equals(cust.getCustname())){
            String custname = " and custname = ‘"+cust.getCustname()+"‘";
            strBuffer.append(custname);
        }
        if(!"".equals(cust.getCustno())){
            String custname = " and custno = ‘"+cust.getCustno()+"‘";
            strBuffer.append(custname);
        }
        String orderBy = " order by cust.id desc";
        strBuffer.append(orderBy);
        List<Cust> custList = this.getHibernateTemplate().find(strBuffer.toString());
        //return (List<Cust>)this.getHibernateTemplate().find(strBuffer.toString());
        return custList; //返回调用
    }

    public void updateCustomer(Cust cust) {
        // TODO Auto-generated method stub
        this.getHibernateTemplate().update(cust);
    }

}

6、在src文件下新建com.crm.service包,并在其下新建Class文件CustService.Java

注:将CustDao.Java中的

public interface CustDao

改为

public interface CustService

即可。

7、在src文件下新建com.crm.service包,并在其下新建Class文件CustServiceImpl.Java

同上,将com.crm.impl中的

public class CustDaoImpl extends HibernateDaoSupport implements CustDao{

改为

public class CustServiceImpl implements CustService{

8、在src文件下新建com.crm.action包,并在其下新建Class文件(根据添加的操作新建class文件)。例子中添加的动作有:

  1. 保存。
package com.crm.action;

import java.util.ArrayList;

import java.util.List;

import com.crm.bean.Cust;

import com.crm.service.CustService;

import com.opensymphony.xwork2.ActionSupport; 

public class CustSaveAction extends ActionSupport{ 

         private CustService service;

         private Cust cust;

         List strList = new ArrayList();

         public List getStrList() {

                  return strList;
         } 

         public void setStrList(List strList) {

                  this.strList = strList;
         } 

         public Cust getCust() {

                  return cust;
         } 

         public void setCust(Cust cust) {

                  this.cust = cust;

         }
        public CustService getService() {

                  return service;
         }

         public void setService(CustService service) {

                  this.service = service;
         }

         @Override

         public String execute() throws Exception {

                  // TODO Auto-generated method stub

                  this.service.saveCustomer(cust);
                  return SUCCESS;
         }
}
  1. 链接。
package com.crm.action;

import java.util.Map;

import com.crm.service.CustService;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")

public class ListCustomerAcion extends ActionSupport{

         private CustService listAllService;

         public void setListAllService(CustService listAllService) {

                  this.listAllService = listAllService;

         }

         @SuppressWarnings("unchecked")

         @Override

         public String execute() throws Exception {

                  Map map = (Map)ActionContext.getContext().get("request");

                  map.put("list", this.listAllService.findAllCust());

                  return SUCCESS;

         }

}
  1. 删除。
package com.crm.action;

import com.crm.bean.Cust;

import com.crm.service.CustService;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")

public class RemoveCustomerAcion extends ActionSupport{

         private Cust customer;

         private CustService removeService;

         public Cust getCustomer() {

                  return customer;

         }

         public void setCustomer(Cust customer) {

                  this.customer = customer;

         }

         public void setRemoveService(CustService removeService) {

                  this.removeService = removeService;

         }

         @SuppressWarnings("unchecked")

         @Override

         public String execute() throws Exception {

                  this.removeService.removeCustomer(customer);

                  return SUCCESS;

         }

}
  1. 查询。
//ID查询:

package com.crm.action;

 ort com.crm.bean.Cust;

import com.crm.service.CustService;

import com.opensymphony.xwork2.ActionSupport;

public class CustFindByIdAction extends ActionSupport {

         private CustService custFindByIdService;

         private Cust customer;

         public void setCustFindByIdService(CustService custFindByIdService) {

                  this.custFindByIdService = custFindByIdService;
         }
         public Cust getCustomer() {

                  return customer;

         }
         public void setCustomer(Cust customer) {

                  this.customer = customer;

         }

         public CustService getCustFindByIdService() {

                 return custFindByIdService;
         }

      public String execute() throws Exception {

// TODO Auto-generated method stub

this.custFindByIdService.findCustomerById(customer.getId());

return SUCCESS;

}

}


条件查询:

package com.crm.action;

import java.util.Map; 

import com.crm.bean.Cust;

import com.crm.service.CustService;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FindCustByCdtAcion extends ActionSupport{ 

         private Cust cust;

         private CustService findCdtService;

         public Cust getCust() {

                  return cust;

         }

         public void setCust(Cust cust) {

                  this.cust = cust;

         }

         public CustService getFindCdtService() {

                  return findCdtService;

         }

         public void setFindCdtService(CustService findCdtService) {

                  this.findCdtService = findCdtService;

         }

         @SuppressWarnings({ "unchecked", "unchecked" })

         @Override

         public String execute() throws Exception {

                  // TODO Auto-generated method stub

                  Map map = (Map)ActionContext.getContext().get("request");

                  map.put("list", this.findCdtService.findCustByCondition(cust));

                  return SUCCESS;

         }

}
  1. 修改。
//修改预览:

package com.crm.action;

import com.crm.bean.Cust;

import com.crm.service.CustService;

import com.opensymphony.xwork2.ActionSupport;

public class UpdatePreviewCustAction extends ActionSupport {  

         private CustService updatePreviewCustService;

         private Cust customer;

         public Cust getCustomer() {

                  return customer;

         }

         public void setCustomer(Cust customer) {

                  this.customer = customer;

         } 

         public CustService getUpdatePreviewCustService() {

                  return updatePreviewCustService;

         } 

         public void setUpdatePreviewCustService(CustService updatePreviewCustService) {

                  this.updatePreviewCustService = updatePreviewCustService;
         } 

         @Override

         public String execute() throws Exception {

                  // TODO Auto-generated method stub

                  customer = this.updatePreviewCustService.findCustomerById(customer

                                   .getId());
                  return SUCCESS;

         } 

}
//修改:

package com.crm.action;

import java.util.ArrayList;

import java.util.List;

import com.crm.bean.Cust;

import com.crm.service.CustService;

import com.opensymphony.xwork2.ActionSupport;

public class UpdateCustAction extends ActionSupport {

         private CustService updateCustService;

         private Cust customer;

         List strList = new ArrayList();

         public List getStrList() {

                  return strList;

         }

         public void setStrList(List strList) {

                  this.strList = strList;

         }

         public CustService getUpdateCustService() {

                  return updateCustService;

         }

         public void setUpdateCustService(CustService updateCustService) {

                  this.updateCustService = updateCustService;

         } 

         public Cust getCustomer() {

                  return customer;

         }

         public void setCustomer(Cust customer) {

                  this.customer = customer;

         }

         @Override

         public String execute() throws Exception {

                  // TODO Auto-generated method stub

                  this.customer.setSex(this.strList.get(0).toString());

                  this.updateCustService.updateCustomer(customer);

                  return SUCCESS;
         }
}

网页的效果界面:

进入http://127.0.0.1:8080/sshtest,点击客户信息管理。

点击新增,

在弹出的界面输入信息,点击保存。

可以查询到插入的数据,

数据中也会更新插入的数据:

时间: 2024-10-04 03:55:14

Myeclipse 6.5 整合SSH(二)的相关文章

Myeclipse 6.5 整合SSH(一)

Myeclipse 6.5 整合SSH(一) 最近小学期正在学习基于Myeclipse 的SSH整合,期间出现了各种各样的问题.问题太多,后面会整理后陆续另开博文写.因为发现老师教我们的过程与网上的有所不同,所以这里主要介绍MyEclipse的初始设置过程. 实验环境 MyEclipse 6.5 Tomcat 6.0 绿色版 Navicat Premium mysql-5.1.49-win32 环境安装的配置过程略. 1. 新建工程 打开MyEclipse,单击File –>New –> We

Myeclipse 6.5 整合SSH(三)

现在,关于"客户信息维护"的SSH框架已经结束,涉及到的后端操作包括增.删.查.改.生成Excel表格等.现在主要说说在创建过程中我经常遇到其中的几个问题. 问题一:  出现 严重:Exception sending context initialized event to listenerinstance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.f

使用MyEclipse整合ssh(Struts、Spring、Hibernate)三大框架(环境搭载+实例源码下载)

前言 SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框架的系统从职责上分为四层:表示层.业务逻辑层.数据持久层和域模块层(实体层). Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持.Spring一方面作为一个轻量级的IoC容器,负责查找.定位.创建和管理对象及

我是如何整合SSH的(二)

这个周在初步了解SSH整合流程,同时以项目为驱动继续学习SSH.在网上找了一个CRM系统来临摹,同时辅助学习SVN版本控制器.因为之前并没有很多版本控制的概念,所以在接触SVN的时候,遇到了很多问题.CRM系统目前已经完成了登录功能,之后会通过CRM再熟悉SSH的CRUD. 新知识点 spring配置中,可以在applicationContext.xml中通过来引入applicationContext-user.xml配置文件. struts2配置中,指定package的name="common

SSH三大框架注解整合(二)

5.使用spring注解注入service,DAO action: @ParentPackage(value = "struts-default") @Namespace("/") @Controller @Scope("prototype") public class BookAction extends ActionSupport implements ModelDriven<Book>{ //模型驱动 public Book b

【j2ee spring】12、整合SSH框架(终结版)

[j2ee spring]12.整合SSH框架(终结版) 最后,我们把整个项目的截图,代码发一下,大家不想下载那个项目的话,可以在这里看到所有的代码(因为那个项目需要一个下载积分,真不多= =,我觉得我搞了那么久,收点积分应该不过分吧...嘿嘿) 这里,我尽量用截图来搞,免得复制粘贴,怪烦的 一.项目整体截图 二.开始全部代码 Person.java Person.hbm.xml PersonService.java package cn.cutter_point.service; import

Struts2,Spring3,Hibernate4整合--SSH框架

Struts2,Spring3,Hibernate4整合--SSH框架(学习中) 一.包的导入 1.Spring包 2.Hibernate 包 3.struts 包 4.数据库方面的包及junt4的包 二.配置文件 1.beans.xml (具体要注意的已经注释到 xml 中了,目前整合了Spring 与 hibernate4 ) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h

整合SSH遇到的错误

错误一: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [spring.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor 解决办法

.net到Java那些事儿--整合SSH

一.介绍       整体介绍分成两个部分,第一.net转到Java的原因,第二开发SSH时候的环境介绍:       .net到Java的原因: .net开发也将近快3年的样子,加上现在的老东家换过的公司也有4家了,中间的心酸故事我也不想说,我就说下中间遇到一些事和我想做的一些事,这些可能促使我进行了转行,当然中间也犹犹豫豫过,比如Core的到来.还有Xamarin这些东西的涌入使我坚持将近一年的样子,但是在6月份的时候我还是下定决心,先后购入Java核心卷.Spring实战.JVM虚拟机.J