码迷,mamicode.com
首页 > 系统相关 > 详细

Myeclipse 6.5 整合SSH(二)

时间:2017-07-02 18:26:34      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:.so   cdata   view   工程   cep   max   support   spring   dal   

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,点击客户信息管理。

技术分享

点击新增,

技术分享

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

 技术分享

可以查询到插入的数据,

 技术分享

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

技术分享

 

Myeclipse 6.5 整合SSH(二)

标签:.so   cdata   view   工程   cep   max   support   spring   dal   

原文地址:http://www.cnblogs.com/xxw-ak/p/7106640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!