SSH整合:Struts2+Spring+Hibernate

SSH整合:Struts2+Spring+Hibernate

一、导入Struts2和Spring和Hibernate的jar包
      导入log4j.properties文件


二、编写web.xml文件

   <!-- Spring集成web的Listner -->
   <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
   </context-param>
   <listener>
  <listener- class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   
   <!-- Struts的核心Filter -->
   <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>
  
三、编写acclicationContext.xml文件

    <?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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" 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/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->

<!-- 1、加载数据库连接信息的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2、数据源dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 3、SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 1、数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 2、hibernate其他配置参数 
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 3、加载映射 -->
<property name="packagesToScan">
<list>
<value>cn.itcast.domain</value>
</list>
</property>
</bean>

<!-- 4、平台事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 5、hibernate模版 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 6、组件扫描 -->
<context:component-scan base-package="cn.itcast"></context:component-scan>

<!-- 7、事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

     </beans>

四、编写数据库连接jdbc.properties文件
    jdbc.driver=com.mysql.jdbc.Driver(MySQL数据库)
    jdbc.url=jdbc:mysql:///ssh262(数据库表)
    jdbc.username=root(用户名)
    jdbc.password=root(密码)

五、编写实体类
  package cn.itcast.domain;

  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.Table;

  @Entity
  @Table(name="cst_customer")
  public class Customer {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="cust_id")
private Long custId;

@Column(name="cust_name")
private String custName;

@Column(name="cust_source")
private String custSource;

@Column(name="cust_industry")
private String custIndustry;

@Column(name="cust_level")
private String custLevel;

@Column(name="cust_address")
private String custAddress;

@Column(name="cust_phone")
private String custPhone;
生成get、set方法

六、编写Action类
  package cn.itcast.web.action;

  import java.util.List;

  import org.apache.struts2.convention.annotation.Action;
  import org.apache.struts2.convention.annotation.Result;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;

  import com.opensymphony.xwork2.ActionSupport;
  import com.opensymphony.xwork2.ModelDriven;

  import cn.itcast.domain.Customer;
  import cn.itcast.service.CustomerService;
  @Controller("customerAction")
  public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{

    private Customer customer = new Customer();
    @Autowired
    private CustomerService customerService;
    
@Override
public Customer getModel() {

return customer;
}
private List<Customer> list;
//查询客户列表
@Action(value="listCustomer",results={@Result(name="listCustomer",location="/jsp/customer/list.jsp")})
public String listCustomer(){
list = customerService.findAllCustomer(customer);
return "listCustomer";
}
public List<Customer> getList() {
return list;
}

public void setList(List<Customer> list) {
this.list = list;
}

public CustomerService getCustomerService() {
return customerService;
}

public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
  }

七、编写业务类

  public interface CustomerService {

List<Customer> findAllCustomer(Customer customer);
  }

  @Service("customerService")
  public class CustomerServiceImpl implements CustomerService {

@Autowired
private CustomerDao customerDao;

//查找客户列表
@Override
public List<Customer> findAllCustomer(Customer customer) {

return customerDao.findAllCustomer(customer);
}
  }

八、编写dao层

  public interface CustomerDao {

List<Customer> findAllCustomer(Customer customer);
  }

  @Repository("customerDao")
  public class CustomerDaoImpl implements CustomerDao{

@Autowired
private HibernateTemplate hibernateTemplate;
//查询客户列表
@Override
public List<Customer> findAllCustomer(Customer customer) {
return(List<Customer>) hibernateTemplate.find("from Customer");
}
  }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值