首页 > 代码库 > 小学期

小学期

首先通过新建web project来进行操作,首先建立com.crm.bean,在目录下建立Cust.java

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

然后连接数据库,在连接数据库有些问题,由于代码在手打过程中出了些错误,第一次没有成功连上

然后根据数据库建立的大小来编写Custhnm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC     
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    
    "http://www.hibernate.org/dtd/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="80"/>
        <property name="sex" type="string" column="sex" length="1"/>
        <!--<property name="age" type="int" column="age"/>
        <property name="telephone" type="string" column="telephone" length="20"/>
        <property name="position" type="string" column="position" length="80"/>
        <property name="logindate" type="string" column="logindate" length="10"/>-->
    </class>
</hibernate-mapping>

再编写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);
    /**
     * 查询所有客户信息
     * @return
     */
    public List<Cust> findAllCust();
    public List<Cust> findCustByCondition(Cust cust);

}

CustService.java和CustDao.java基本一致

package com.crm.service;

import java.util.List;

import com.crm.bean.Cust;

public interface CustService {
    public void saveCustomer(Cust cust) ;
    /****
     * 
     * 保存客户信息
     */
    public void removeCustomer(Cust cust) ;
    /****
     * 
     * 删除客户信息
     */
    public Cust findCustomerById(Integer id) ;
    /**
     * 
     * 查询客户信息
     */
    public List<Cust> findAllCust() ;
    /***
     * 查询所有客户信息
     * @return 
     */
    public List<Cust> findCustByCondition(Cust cust);
}

CustSaveAction.java

package com.crm.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.crm.bean.Cust;
import com.crm.service.CustService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CustSaveAction extends ActionSupport {

    private CustService service;
    private Cust cust;
    List strList = new ArrayList();
    public CustService getsevice() {
        return service;
    }
    public Cust getCust() {
        return cust;
    }

    public void setCust(Cust cust) {
        this.cust = cust;
    }

    public void setService(CustService service) {
        this.service = service;
    }

    public List getStrList() {
        return strList;
    }

    public void setStrList(List strList) {
        this.strList = strList;
    }

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        this.service.saveCustomer(cust);
        return SUCCESS;
    }

}

里面好多代码都需要细心的去打,很容易少一个字母或者多一个字母造成运行错误,或者运行没错而操作的时候出现了找不到配置的错误,或者找不到路径的错误。

小学期