码迷,mamicode.com
首页 > 其他好文 > 详细

ssh整合

时间:2020-03-21 15:00:04      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:findall   contex   显示   c3p0配置   apache   ==   cts   tin   联系   

一. 步骤

 1.加入Spring

  1.1加入jar包

  1.2配置web.xml配置文件

  

<!-- 配置Spring的核心监听器 -->
  <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>            

 

  1.3加入spring的配置文件

 

2.加入hibernate

  2.1加入jar包

  2.2在类路径下加入hibernate.cfg.xml配置文件,在其中配置hibernate 的基本属性(方言等)

   注:该cfg.xml是直接写在applicationContext.xml配置文件中的

     <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 注入连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置Hibernate的相关属性 -->
        <property name="hibernateProperties">
            <props>
                <!--方言-->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <!--显示和格式化SQL-->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <!--生成数据表的策略-->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        
        <!-- 引入映射文件 -->
        <property name="mappingResources">
            <list>
                <value>com/itheima/crm/domain/User.hbm.xml</value>
                <value>com/itheima/crm/domain/Customer.hbm.xml</value>
                <value>com/itheima/crm/domain/BaseDict.hbm.xml</value>
                <value>com/itheima/crm/domain/LinkMan.hbm.xml</value>
                <value>com/itheima/crm/domain/SaleVisit.hbm.xml</value>
            </list>
        </property>
    </bean>

 

  2.3建立持久化类和对应的.hbm.xml配置文件

   联系人类以及对应的.hbm.xml配置文件

public class LinkMan {
    private Long lkm_id;
    private String lkm_name;
    private String lkm_gender;
    private String lkm_phone;
    private String lkm_mobile;
    private String lkm_email;
    private String lkm_qq;
    private String lkm_position;
    private String lkm_memo;
    // 在联系人方放置客户的对象:
    private Customer customer;
    public Long getLkm_id() {
        return lkm_id;
    }
    public void setLkm_id(Long lkm_id) {
        this.lkm_id = lkm_id;
    }
    public String getLkm_name() {
        return lkm_name;
    }
    public void setLkm_name(String lkm_name) {
        this.lkm_name = lkm_name;
    }
    public String getLkm_gender() {
        return lkm_gender;
    }
    public void setLkm_gender(String lkm_gender) {
        this.lkm_gender = lkm_gender;
    }
    public String getLkm_phone() {
        return lkm_phone;
    }
    public void setLkm_phone(String lkm_phone) {
        this.lkm_phone = lkm_phone;
    }
    public String getLkm_mobile() {
        return lkm_mobile;
    }
    public void setLkm_mobile(String lkm_mobile) {
        this.lkm_mobile = lkm_mobile;
    }
    public String getLkm_email() {
        return lkm_email;
    }
    public void setLkm_email(String lkm_email) {
        this.lkm_email = lkm_email;
    }
    public String getLkm_qq() {
        return lkm_qq;
    }
    public void setLkm_qq(String lkm_qq) {
        this.lkm_qq = lkm_qq;
    }
    public String getLkm_position() {
        return lkm_position;
    }
    public void setLkm_position(String lkm_position) {
        this.lkm_position = lkm_position;
    }
    public String getLkm_memo() {
        return lkm_memo;
    }
    public void setLkm_memo(String lkm_memo) {
        this.lkm_memo = lkm_memo;
    }
    public Customer getCustomer() {
        return customer;
    }
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    
    
}

    

<hibernate-mapping>
    <class name="com.itheima.crm.domain.LinkMan" table="cst_linkman">
        <id name="lkm_id" column="lkm_id">
            <generator class="native"/>
        </id>
        
        <property name="lkm_name" column="lkm_name"/>
        <property name="lkm_gender" column="lkm_gender"/>
        <property name="lkm_phone" column="lkm_phone"/>
        <property name="lkm_mobile" column="lkm_mobile"/>
        <property name="lkm_email" column="lkm_email"/>
        <property name="lkm_qq" column="lkm_qq"/>
        <property name="lkm_position" column="lkm_position"/>
        <property name="lkm_memo" column="lkm_memo"/>
        
        <many-to-one name="customer" class="com.itheima.crm.domain.Customer" column="lkm_cust_id"/>
    </class>
</hibernate-mapping>

    顾客类以及对应的.hbm.xml配置文件

public class Customer {
    private Long cust_id;
    private String cust_name;
    private String cust_phone;
    private String cust_mobile;
    private String cust_image; // 客户资质图片
    /**
     * 客户和字典表示多对一:需要在多的一方放的是一的一方的对象
     */
    private BaseDict baseDictSource;
    private BaseDict baseDictIndustry;
    private BaseDict baseDictLevel;
    //一个客户有多个联系人:
    private Set<LinkMan> linkMans = new HashSet<LinkMan>();
    
    public Long getCust_id() {
        return cust_id;
    }
    public void setCust_id(Long cust_id) {
        this.cust_id = cust_id;
    }
    public String getCust_name() {
        return cust_name;
    }
    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }
    public String getCust_phone() {
        return cust_phone;
    }
    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }
    public String getCust_mobile() {
        return cust_mobile;
    }
    public void setCust_mobile(String cust_mobile) {
        this.cust_mobile = cust_mobile;
    }
    public BaseDict getBaseDictSource() {
        return baseDictSource;
    }
    public void setBaseDictSource(BaseDict baseDictSource) {
        this.baseDictSource = baseDictSource;
    }
    public BaseDict getBaseDictIndustry() {
        return baseDictIndustry;
    }
    public void setBaseDictIndustry(BaseDict baseDictIndustry) {
        this.baseDictIndustry = baseDictIndustry;
    }
    public BaseDict getBaseDictLevel() {
        return baseDictLevel;
    }
    public void setBaseDictLevel(BaseDict baseDictLevel) {
        this.baseDictLevel = baseDictLevel;
    }
    public String getCust_image() {
        return cust_image;
    }
    public void setCust_image(String cust_image) {
        this.cust_image = cust_image;
    }
    public Set<LinkMan> getLinkMans() {
        return linkMans;
    }
    public void setLinkMans(Set<LinkMan> linkMans) {
        this.linkMans = linkMans;
    }
    
}

            

<hibernate-mapping>
    <class name="com.itheima.crm.domain.Customer" table="cst_customer">
        <id name="cust_id" column="cust_id">
            <generator class="native"/>
        </id>
        
        <property name="cust_name" column="cust_name"/>
        <property name="cust_phone" column="cust_phone"/>
        <property name="cust_mobile" column="cust_mobile"/>
        <property name="cust_image" column="cust_image"/>
        
        <!-- 配置客户与字典的多对一的映射 -->
        <many-to-one name="baseDictSource" class="com.itheima.crm.domain.BaseDict" column="cust_source"/>
        <many-to-one name="baseDictIndustry" class="com.itheima.crm.domain.BaseDict" column="cust_industry"/>
        <many-to-one name="baseDictLevel" class="com.itheima.crm.domain.BaseDict" column="cust_level"/>
        
        <!-- 配置与联系人的关系映射 -->
        <set name="linkMans" cascade="delete" inverse="true">
            <key column="lkm_cust_id"/>
            <one-to-many class="com.itheima.crm.domain.LinkMan"/>
        </set>
    </class>
</hibernate-mapping>


  2.4与spring整合

    2.4.1加入c3p0和MySQL的驱动

      c3p0配置文件:

<!-- 引入外部属性文件=============================== -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <!-- 配置C3P0连接池=============================== -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

      jdbc.properties:

        

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///crm1
jdbc.username=root
jdbc.password=123456

    2.4.2在spring的配置文件中配置:数据源,session factory,声明式事务

    注:session factory的配置在上面.cfg.xml配置文件中,数据源的配置文件如上面的c3p0配置文件一样。

<!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

3. 加入struts2

  3.1加入jar包

  3.2在web.xml配置struts2的filter

 <!-- Struts2的核心过滤器 -->
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

  3.3在spring的配置文件中正常配置,注意action的scope为prototype

<!-- 配置客户管理的相关的类 -->
    <bean id="customerAction" class="com.itheima.crm.web.action.CustomerAction" scope="prototype">
        <property name="customerService" ref="customerService"/>
    </bean>
    
    <bean id="customerService" class="com.itheima.crm.service.impl.CustomerServiceImpl">
        <property name="customerDao" ref="customerDao"/>
    </bean>
    
    <bean id="customerDao" class="com.itheima.crm.dao.impl.CustomerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
<!-- 配置联系人的相关的类 -->
    <bean id="linkManAction" class="com.itheima.crm.web.action.LinkManAction" scope="prototype">
        <property name="linkManService" ref="linkManService"/>
        <property name="customerService" ref="customerService"/>
    </bean>
    
    <bean id="linkManService" class="com.itheima.crm.service.impl.LinkManServiceImpl">
        <property name="linkManDao" ref="linkManDao"/>
    </bean> 
    
    <bean id="linkManDao" class="com.itheima.crm.dao.impl.LinkManDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

  3.4 在struts2的配置文件中配置action的时候,class属性指向该action在ioc中的id

<struts>
    <!-- 配置Struts2的常量 -->
    <constant name="struts.action.extension" value="action"/>
    <!-- 配置Struts2中所上传的文件的总大小 -->
    <constant name="struts.multipart.maxSize" value="5242880"/>
    <package name="crm" extends="struts-default" namespace="/">
        <!-- 配置客户管理的Action -->
        <action name="customer_*" class="customerAction" method="{1}">
            <result name="saveUI">/jsp/customer/add.jsp</result>
            <result name="findAll">/jsp/customer/list.jsp</result>
            <result name="editSuccess">/jsp/customer/edit.jsp</result>
            <result name="saveSuccess" type="redirectAction">customer_findAll.action</result>
            <result name="deleteSuccess" type="redirectAction">customer_findAll.action</result>
            <result name="updateSuccess" type="redirectAction">customer_findAll.action</result>
            <result name="input">/jsp/customer/add.jsp</result>
            
            <interceptor-ref name="privilegeInterceptor"/>
            <interceptor-ref name="defaultStack">
                <param name="fileUpload.maximumSize">2097152</param>
                <param name="fileUpload.allowedExtensions">.jpg</param>
            </interceptor-ref>
        </action>
        <!-- 联系人的Action的配置 -->
        <action name="linkMan_*" class="linkManAction" method="{1}">
            <result name="findAll">/jsp/linkman/list.jsp</result>
            <result name="saveUI">/jsp/linkman/add.jsp</result>
            <result name="saveSuccess" type="redirectAction">linkMan_findAll.action</result>
            <result name="editSuccess">/jsp/linkman/edit.jsp</result>
            <result name="updateSuccess" type="redirectAction">linkMan_findAll.action</result>
            <result name="deleteSuccess" type="redirectAction">linkMan_findAll.action</result>
            
            <interceptor-ref name="privilegeInterceptor"/>
            <interceptor-ref name="defaultStack"/>
        </action>
  </package>
</struts>    

ssh整合

标签:findall   contex   显示   c3p0配置   apache   ==   cts   tin   联系   

原文地址:https://www.cnblogs.com/duij/p/12539162.html

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