CRM第一篇

2.1搭建前提
    我们在搭建CRM开发环境之前,需要明确2件事情:
        a、我们搭建环境采用基于注解的配置。
        b、搭建环境需要测试,我们以客户的保存和列表查询作为测试功能。
    2.2搭建步骤
    2.2.1导入Spring和Hibernate的jar包
    Hibernate基本jar包(包括了数据库驱动)

    C3P0的jar包

    Spring的IoC,AOP和事务控制必备jar包



    2.2.2创建实体类并使用注解映射
    /**
     * 客户的实体类
     * 
     * 明确使用的注解都是JPA规范的
     * 所以导包都要导入javax.persistence包下的
     *
     */
    @Entity//表示当前类是一个实体类
    @Table(name="cst_customer")//建立当前实体类和表之间的对应关系
    public class Customer implements Serializable {
        
        @Id//表明当前私有属性是主键
        @GeneratedValue(strategy=GenerationType.IDENTITY)//指定主键的生成策略
        @Column(name="cust_id")//指定和数据库表中的cust_id列对应
        private Long custId;
        @Column(name="cust_name")//指定和数据库表中的cust_name列对应
        private String custName;
        
        @Column(name="cust_industry")//指定和数据库表中的cust_industry列对应
        private String custIndustry;
        @Column(name="cust_source")
        private String custSource;
        @Column(name="cust_level")
        private String custLevel;
        @Column(name="cust_address")//指定和数据库表中的cust_address列对应
        private String custAddress;
        @Column(name="cust_phone")//指定和数据库表中的cust_phone列对应
        private String custPhone;
            
        public Long getCustId() {
            return custId;
        }
        public void setCustId(Long custId) {
            this.custId = custId;
        }
        public String getCustName() {
            return custName;
        }
        public void setCustName(String custName) {
            this.custName = custName;
        }
        public String getCustIndustry() {
            return custIndustry;
        }
        public void setCustIndustry(String custIndustry) {
            this.custIndustry = custIndustry;
        }
        public String getCustAddress() {
            return custAddress;
        }
        public void setCustAddress(String custAddress) {
            this.custAddress = custAddress;
        }
        public String getCustPhone() {
            return custPhone;
        }
        public void setCustPhone(String custPhone) {
            this.custPhone = custPhone;
        }
        public String getCustSource() {
            return custSource;
        }
        public void setCustSource(String custSource) {
            this.custSource = custSource;
        }
        public String getCustLevel() {
            return custLevel;
        }
        public void setCustLevel(String custLevel) {
            this.custLevel = custLevel;
        }
        @Override
        public String toString() {
            return "Customer [custId=" + custId + ", custName=" + custName
                    + ", custIndustry=" + custIndustry + ", custAddress="
                    + custAddress + ", custPhone=" + custPhone + "]";
        }
    }

    2.2.3编写业务层接口及实现类
    /**
     * 客户的业务层接口
     * 一个功能模块一个service
     */
    public interface ICustomerService {

        /**
         * 保存客户
         * @param customer
         */
        void saveCustomer(Customer customer);
        
        /**
         * 查询所有客户信息
         * @return
         */
        List findAllCustomer();
    }

    /**
     * 客户的业务层实现类
     */
    @Service("customerService")
    @Transactional(propagation=Propagation.REQUIRED,readOnly=false)
    public class CustomerServiceImpl implements ICustomerService {

        @Autowired
        private ICustomerDao customerDao;
        
        @Override
        public void saveCustomer(Customer customer) {
            customerDao.save(customer);
        }

        @Override
        @Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
        public List findAllCustomer() {
            return customerDao.findAllCustomer();
        }
    }

    2.2.4编写持久层接口及实现类
    /**
     * 客户的持久层接口
     */
    public interface ICustomerDao extends IBaseDao{
            
        /**
         * 查询所有客户
         * @return
         */
        List findAllCustomer();

        /**
         * 保存客户
         * @param customer
         * @return
         */
        void saveCustomer(Customer customer);
    }

    /**
     * 客户的持久层实现类
     */
    @Repository("customerDao")
    public class CustomerDaoImpl implements ICustomerDao {

        @Autowired
        private HibernateTemplate hibernateTemplate;
        
        @Override
        public void saveCustomer(Customer customer) {
            hibernateTemplate.save(cusotmer);
        }
        
        @Override
        public List findAllCustomer() {
            return (List) hibernateTemplate.find("from Customer ");
        }
    }
    2.2.5编写spring的配置

    
    
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:aop="http://www.springframework.org/schema/aop"
            xmlns:tx="http://www.springframework.org/schema/tx" 
            xmlns:context="http://www.springframework.org/schema/context" 
            xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx.xsd
                    http://www.springframework.org/schema/aop 
                    http://www.springframework.org/schema/aop/spring-aop.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context.xsd">
        
        
        package="com.baidu">
        
        
        
        
        
        class="org.springframework.orm.hibernate5.HibernateTemplate">
            
            
        
        
        
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            
            
        
        
        
        
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            
            
            
            
                
                    
                    
                        org.hibernate.dialect.MySQLDialect
                    
                    
                    true
                    
                    false
                    
                    update
                    
                    
                        org.springframework.orm.hibernate5.SpringSessionContext
                    
                
            
            
            
                
                    com.baidu.domain
                
            
        
        
        
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
            
            
            
            
        
    
    2.2.6导入junit的jar包
     
    2.2.7配置spring整合junit的测试类并测试
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:config/spring/applicationContext.xml"})
    public class CustomerServiceTest {

        @Autowired
        private ICustomerService customerService;
        
        @Test
        public void testFindAll(){
            customerService.findAllCustomer();
        }
        
        @Test
        public void testSave(){
            Customer c = new Customer();
            c.setCustName("传智学院 ");    
            customerService.saveCustomer(c);
        }
    }

    2.2.8导入jsp页面

    2.2.9导入struts2的jar包



    2.2.10编写Action并配置
    /**
     * 客户的动作类
     *
     */
    @Controller("customerAction")
    @Scope("prototype")
    @ParentPackage("struts-default")
    @Namespace("/customer")
    public class CustomerAction extends ActionSupport implements ModelDriven {
        private Customer customer = new Customer();
        
        @Autowired
        private ICustomerService customerService ;

        @Override
        public Customer getModel() {
            return customer;
        }
        
        /**
         * 添加客户
         * @return
         */
        @Action(value="addCustomer",results={
            @Result(name="addCustomer",type="redirect",location="/jsp/success.jsp")
        })
        public String addCustomer(){
            customerService.saveCustomer(customer);
            return "addCustomer";
        }
        
        
        /**
         * 获取添加客户页面
         * @return
         */
        @Action(value="addUICustomer",results={            @Result(name="addUICustomer",type="dispatcher",location="/jsp/customer/add.jsp")
        })
        public String addUICustomer(){
            return "addUICustomer";
        }
        

        private List customers;
        @Action(value="findAllCustomer",results={        @Result(name="findAllCustomer",type="dispatcher",location="/jsp/customer/list.jsp")
        })
        public String findAllCustomer(){
            page = customerService.findAllCustomer(dCriteria,num);
            return "findAllCustomer";
        }

        public List getCustomers() {
            return customers;
        }

        public void setCustomers(List customers) {
            this.customers = customers;
        }
    }
    2.2.11改造jsp页面
    略。设计的jsp有:menu.jsp  add.jsp   list.jsp
    2.2.12编写web.xml
    
     
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
          
      
        
            class>
                org.springframework.web.context.ContextLoaderListener
            class>
        
        
        
        
            contextConfigLocation
            
                classpath:config/spring/applicationContext.xml
            
        
        
        
        
            OpenSessionInViewFilter
            class>
                org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
            class>
        
        
            OpenSessionInViewFilter
            /*
        

        
        
            struts2
            
               org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
            
            
            
                config
                
                    struts-default.xml,struts-plugin.xml,config/struts/struts.xml
                
            
        

        
            struts2
            /*
        

        
            index.html
            index.htm
            index.jsp
            default.html
            default.htm
            default.jsp
        
    

    2.2.13测试环境搭建是否成功
    我们可以通过浏览器地址栏输入网址来查看是否搭建成功:
        http://localhost:8080/写自己的项目名称

 

你可能感兴趣的:(CRM第一篇)