zoukankan      html  css  js  c++  java
  • 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<Customer> 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<Customer> findAllCustomer() {
                return customerDao.findAllCustomer();
            }
        }
    
        2.2.4编写持久层接口及实现类
        /**
         * 客户的持久层接口
         */
        public interface ICustomerDao extends IBaseDao<Customer>{
                
            /**
             * 查询所有客户
             * @return
             */
            List<Customer> 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<Customer> findAllCustomer() {
                return (List<Customer>) hibernateTemplate.find("from Customer ");
            }
        }
        2.2.5编写spring的配置
    
        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
                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">
            
            <!-- 配置spring运行要扫描的包 -->
            <context:component-scan base-package="com.baidu"></context:component-scan>
            
            <!-- 开启spring对注解事务的支持 -->
            <tx:annotation-driven transaction-manager="transactionManager"/>
            
            <!-- 配置HibernateTemplate -->
            <bean id="hibernateTemplate" 
                        class="org.springframework.orm.hibernate5.HibernateTemplate">
                <!-- 注入SessionFactory -->
                <property name="sessionFactory" ref="sessionFactory"></property>
            </bean>
            
            <!-- 配置事务管理器 -->
            <bean id="transactionManager" 
                class="org.springframework.orm.hibernate5.HibernateTransactionManager">
                <!-- 注入SessionFactory -->
                <property name="sessionFactory" ref="sessionFactory"></property>
            </bean>
            
            
            <!-- 配置SessionFactory -->
            <bean id="sessionFactory" 
                    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
                <!-- 1、连接数据库的 -->
                <property name="dataSource" ref="dataSource"></property>
                <!-- 2、hibernate基本配置的 -->
                <property name="hibernateProperties">
                    <props>
                        <!-- 数据库的方言-->
                        <prop key="hibernate.dialect">
                            org.hibernate.dialect.MySQLDialect
                        </prop>
                        <!-- 是否显示sql语句-->
                        <prop key="hibernate.show_sql">true</prop>
                        <!-- 是否格式化sql语句-->
                        <prop key="hibernate.format_sql">false</prop>
                        <!-- 采用何种方式生成数据库表结构 -->
                        <prop key="hibernate.hbm2ddl.auto">update</prop>
                        <!-- 是spring把sesion绑定到当前线程上的配置 -->
                        <prop key="hibernate.current_session_context_class">
                            org.springframework.orm.hibernate5.SpringSessionContext
                        </prop>
                    </props>
                </property>
                <!-- 3、指定扫描映射注解的包-->
                <property name="packagesToScan">
                    <array>
                        <value>com.baidu.domain</value>
                    </array>
                </property>
            </bean>
            
            <!-- 配置数据源 -->
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
                <property name="jdbcUrl" value="jdbc:mysql:///crm"></property>
                <property name="user" value="root"></property>
                <property name="password" value="1234"></property>
            </bean>
        </beans>
        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<Customer> {
            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<Customer> 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<Customer> getCustomers() {
                return customers;
            }
    
            public void setCustomers(List<Customer> customers) {
                this.customers = customers;
            }
        }
        2.2.11改造jsp页面
        略。设计的jsp有:menu.jsp  add.jsp   list.jsp
        2.2.12编写web.xml
        <?xml version="1.0" encoding="UTF-8"?>
        <web-app version="3.0" 
            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">
          <display-name></display-name>    
          <!-- 配置监听器 -->
            <listener>
                <listener-class>
                    org.springframework.web.context.ContextLoaderListener
                </listener-class>
            </listener>
            
            <!-- 手动指定spring配置文件的位置 -->
            <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    classpath:config/spring/applicationContext.xml
                </param-value>
            </context-param>
            
            <!-- 配置解决Nosession问题的过滤器 -->
            <filter>
                <filter-name>OpenSessionInViewFilter</filter-name>
                <filter-class>
                    org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
                </filter-class>
            </filter>
            <filter-mapping>
                <filter-name>OpenSessionInViewFilter</filter-name>
                <url-pattern>/*</url-pattern>
            </filter-mapping>
    
            <!-- 配置struts2的核心控制,同时需要手动指定struts2的配置文件位置 -->
            <filter>
                <filter-name>struts2</filter-name>
                <filter-class>
                   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
                </filter-class>
                <!-- 手动指定struts2配置文件的位置 -->
                <init-param>
                    <param-name>config</param-name>
                    <param-value>
                        struts-default.xml,struts-plugin.xml,config/struts/struts.xml
                    </param-value>
                </init-param>
            </filter>
    
            <filter-mapping>
                <filter-name>struts2</filter-name>
                <url-pattern>/*</url-pattern>
            </filter-mapping>
    
            <welcome-file-list>
                <welcome-file>index.html</welcome-file>
                <welcome-file>index.htm</welcome-file>
                <welcome-file>index.jsp</welcome-file>
                <welcome-file>default.html</welcome-file>
                <welcome-file>default.htm</welcome-file>
                <welcome-file>default.jsp</welcome-file>
            </welcome-file-list>
        </web-app>
    
        2.2.13测试环境搭建是否成功
        我们可以通过浏览器地址栏输入网址来查看是否搭建成功:
            http://localhost:8080/写自己的项目名称
  • 相关阅读:
    sql 行转列
    wm_concat函数 用法
    PL/SQL如何调试Oracle存储过程
    Oracle&SQLServer中实现跨库查询
    Oracle 中 decode 函数用法
    Oracle中给用户赋予debug权限
    Oracle中的NVL函数
    oracle 触发器 pragma autonomous_transaction
    ORACLE中%TYPE和%ROWTYPE的使用
    A complete example using RAISE_APPLICATION_ERROR : raise_application_error
  • 原文地址:https://www.cnblogs.com/haizai/p/11611699.html
Copyright © 2011-2022 走看看